c++ string_view
-
I am new to C++17, and am trying to figure out how to use string_view. I have three functions that parse a string of comma separated values: beforecomma reports the string before the first comma, aftercomma reports the string after the first comma, and slicecomma returns the string before the first comma and updates the original string to that part after the first comma. These functions all work properly in their current forms using just string. Now, I am trying to update them as beforecomma2, aftercomma2, and slicecomma2, to take advantage of string_view. However, I am getting some unexpected results. I am hoping someone out there can tell me why this is happening and what I am doing wrong. First, the functions:
std::string_view beforecomma2 (std::string_view line)
{
auto found = line.find(',');
if (found!=std::string::npos)
{return line.substr(0,found);}
else return line;
}string_view aftercomma2 (string_view line)
{
auto found = line.find(',');
if ((found==std::string::npos) || (found==line.size()-1)) return "";
else
{
std::string_view out=line.substr(found+1,line.size());
while ((out[0]==' ') && (out.size()>0)) out=out.substr(1,out.size());
return out;
}
}string_view slicecomma2 (string& line)
{
auto found=line.find(',');
if (found==std::string::npos)
{
return line;
}
else
{
string_view out=beforecomma2(line);
line=aftercomma2(line);
return out;
}}
I run these functions with the following main, that just calls the functions beforecomma2 and aftercomma2.
int main (int argc, char* argv[])
{
string test="01234, 5678, 90, 78902, 999999, 333333";cout << test << endl;
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);return 1;
}I get the expected output of: 01234, 5678, 90, 78902, 999999, 333333 01234 5678 90 78902 999999 333333 So far, so good. Now I try to combine these two functions into a single function, slicecomma2, implemented as:
int main (int argc, char* argv[])
{
string test="01234, 5678, 90, 78902, 999999, 333333";PrintString(t
-
I am new to C++17, and am trying to figure out how to use string_view. I have three functions that parse a string of comma separated values: beforecomma reports the string before the first comma, aftercomma reports the string after the first comma, and slicecomma returns the string before the first comma and updates the original string to that part after the first comma. These functions all work properly in their current forms using just string. Now, I am trying to update them as beforecomma2, aftercomma2, and slicecomma2, to take advantage of string_view. However, I am getting some unexpected results. I am hoping someone out there can tell me why this is happening and what I am doing wrong. First, the functions:
std::string_view beforecomma2 (std::string_view line)
{
auto found = line.find(',');
if (found!=std::string::npos)
{return line.substr(0,found);}
else return line;
}string_view aftercomma2 (string_view line)
{
auto found = line.find(',');
if ((found==std::string::npos) || (found==line.size()-1)) return "";
else
{
std::string_view out=line.substr(found+1,line.size());
while ((out[0]==' ') && (out.size()>0)) out=out.substr(1,out.size());
return out;
}
}string_view slicecomma2 (string& line)
{
auto found=line.find(',');
if (found==std::string::npos)
{
return line;
}
else
{
string_view out=beforecomma2(line);
line=aftercomma2(line);
return out;
}}
I run these functions with the following main, that just calls the functions beforecomma2 and aftercomma2.
int main (int argc, char* argv[])
{
string test="01234, 5678, 90, 78902, 999999, 333333";cout << test << endl;
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);
cout << beforecomma2(test) << endl; test=aftercomma2(test);return 1;
}I get the expected output of: 01234, 5678, 90, 78902, 999999, 333333 01234 5678 90 78902 999999 333333 So far, so good. Now I try to combine these two functions into a single function, slicecomma2, implemented as:
int main (int argc, char* argv[])
{
string test="01234, 5678, 90, 78902, 999999, 333333";PrintString(t
The string_view is a view into an existing string. The string it is looking at must still be available as long as you want to use the string_view. Your
slicecomma2
function is creating a string_view from theline
string but then it is changing theline
string, making the contents of the string_view invalid. -
The string_view is a view into an existing string. The string it is looking at must still be available as long as you want to use the string_view. Your
slicecomma2
function is creating a string_view from theline
string but then it is changing theline
string, making the contents of the string_view invalid.Of course, I see that now. I knew I was making some type of stupid mistake, but those are always the hardest for me to catch and correct. Thank you for your response.