I have the following strings:
std::string str1 = "1234567890";
std::string str2 = "B-XXXX_XXX_V-XX_X";
I want to loop through str2 and replace every occurrence of X with the subsequent value from str1, resulting in: B-1234_567_V-89_0.
I have a semblance of a solution below, but it's not very efficient (it worked at one point). In brief, I tried to loop through the characters in str2, and if the character equaled 'X', replace that character with an incrementing index from str1:
int ind = 0;
std::string pattern_char;
for (int i = 0; i < str2.size(); i++) {
pattern_char = str2[i];
if (pattern_char == "X") {
str2[i] = str1[x_ind];
x_ind++;
}
}
What is the most efficient way to perform this operation?