简体   繁体   中英

C++ Replace String Character with Character from Specific Index of Vector?

I am trying to replace each instance of a specific character of a string with a specific index character of a vector. Is this possible?

Not sure I follow what you mean, but if I understand correctly, why not?

int main() {
    std::string str = "hello";
    std::vector<char> vec;
    vec.push_back('c');
    vec.push_back('b');
    char specific_char = 'l';
    for (int i = 0; i < str.size(); i++) {
         if (str[i] == specific_char) {
             str[i] = vec[1];
        }
     }  
     std::cout << str << std::endl; // hebbo
     return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM