Quote:
Originally Posted by weaknessforcats
The STL solution is a different approach. Check this out:
-
//Concatenate a string and an int into a string
-
stringstream ss;
-
string str("The magic number is: ");
-
int data = 123;
-
ss << str << data;
-
string result;
-
string token;
-
while (!ss.eof())
-
{
-
ss >> token;
-
result += token;
-
result += ' ';
-
}
-
cout << result << endl;
-
Here a stringstream is used and you just insert your data into the stream. Everything is converted to char since all of the templates are specialzed on char (or wchar_t but's that a different story). Then you just use the extractor of the stream to fetch the tokens. You build the result by appending the token and a space.
The type of code you are trying to get working is a duplication of STL features.
agree stringstream does allow us to use "<<" operator to write(append) and read back into string. But I find it to be confusing and it eats up the spaces in a string for example:
if str = " Hello here are _sp_ _sp_ _sp_ many spaces in ";// _sp_ to represent a single white space
even if I use your logic ( as above ) would still lose the multiple spaces.