It seems you are not initializing start_pos, so you will need to change this line:
std::size_t start_pos = 0;
undefined , .
, string:: size_type, .
:
main.cpp
#include <string>
#include <iostream>
using namespace std;
string myreplace(const string &line)
{
string::size_type start_pos = 0;
string tmp(line);
while ((start_pos = tmp.find(";", start_pos)) != string::npos)
{
tmp.replace(start_pos, 1, " ");
start_pos += 1;
}
return tmp;
}
int main()
{
string test_str1 = "word1 word2 word3;";
string test_str2 = "word1 word2 word3; word1 word2 word3;";
string test_str3 = "word1 word2 word3; word1 word2 word3;....";
cout << myreplace(test_str1) << endl;
cout << myreplace(test_str2) << endl;
cout << myreplace(test_str3) << endl;
return 0;
}
word1 word2 word3
word1 word2 word3 word1 word2 word3
word1 word2 word3 word1 word2 word3 ....
=============================================== ===
std :
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string test_str1 = "word1 word2 word3;";
string test_str2 = "word1 word2 word3; word1 word2 word3;";
string test_str3 = "word1 word2 word3; word1 word2 word3;....";
string out_str1 = replace(test_str1.begin(), test_str1.end(), ';', ' ');
string out_str2 = replace(test_str2.begin(), test_str2.end(), ';', ' ');
string out_str3 = replace(test_str3.begin(), test_str3.end(), ';', ' ');
cout << out_str1 << endl;
cout << out_str2 << endl;
cout << out_str3 << endl;
return 0;
}
word1 word2 word3
word1 word2 word3 word1 word2 word3
word1 word2 word3 word1 word2 word3 ....