how to correctly use stringstream class multiple times [duplicate] - c++

This question already has an answer here:
How to clear stringstream? [duplicate]
(1 answer)
Closed 8 years ago.
Stringstream is very convenient in C++ to convert between strings and other num types. I am trying to use a stringstream multiple times in a program like this.
stringstream ss;
string s1="stack",s2="overflow";
string s3,s4;
ss<<s1;
ss>>s3;
ss.str("");
cout<<s3<<endl;
ss<<s2;
ss>>s4;
cout<<s4<<endl;
The program will correctly output "stack", but will not output "overflow". Any explanations for that?

call ss.clear()
stringstream ss;
string s1="stack",s2="overflow";
string s3,s4;
ss<<s1;
ss>>s3;
ss.str("");
cout<<s3<<endl;
ss.clear()
ss<<s2;
ss>>s4;
cout<<s4<<endl;

Related

How can I parse integer values into separate int variables from a comma separated string? [duplicate]

This question already has answers here:
Parsing a comma-delimited std::string [duplicate]
(18 answers)
Closed 3 years ago.
I have a string that contains 3 numbers separated by commas (for example "10,32,52") and I would like to store each number in 3 different int variables, but I just know how to store the first one using the code below. Can you tell me please how can I store the next two?
Thanks in advance.
string numbers= "10,32,52";
string first_number_s= numbers.substr(0,2);
int first_number_n= stoi(first_number_s);
You could stream the input string into a string stream and use std::getline to extract strings separated by a delimiter, in this case comma, converting each one to an integer:
#include <sstream>
#include <string>
std::string numbers = "10,32,52";
std::istringstream sstream(numbers);
std::string temp;
std::vector<int> nums;
while (std::getline(sstream, temp, ','))
{
nums.push_back(std::stoi(temp));
}

c++: Foreach line in string (not file) [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 8 years ago.
In c++, how can I iterate through each line in a string? There have been plenty of questions regarding reading a file line by line, but how can I do this with a std::string?
For example, if I have the following string:
1051
2232
5152
3821
0021
3258
How would I iterate through each number?
In c++, you can use string exactly as files, using the classes defined in the sstream header:
#include <sstream>
//...
std::string str=...; // your string
std::istrstream in(str); // an istream, just like ifstream and cin
std::string line;
while(std::getline(in,line)){
//do stuff with line
}
This is a bit simplistic, but you get the idea.
You can use in just as you would use cin, e.g. in>>x etc. Hence the solutions from How do I iterate over cin line by line in C++? are relevant here too - you might want to look at them for the "real" answer (just replace cin with your own istream
Edit:
As a side note, you can create strings in the same way you print to the screen, using the ostream mechanism (like cout):
std::ostringstream out;
out << header << "_" << 3.5<<".txt";
std::string filename=out.str();
Use a tokenizer and let '\n' or '\r\n' or the appropriate newline for your OS be the token splitter..
Or if you were using a buffered file stream reader, just create a stringstream from this new string and read from the string stream instead of the file stream.
In short nothing changes except that you aren't reading from a file.
A horribly naive solution would be to make a string stream from this and assign ints or strings in a while loop from it.

C++ Strings single chars [duplicate]

This question already has answers here:
How to convert string to char array in C++?
(11 answers)
Closed 8 years ago.
I've just started learning C++ and im kinda confused about strings.
I first need a input word and save every single char in the certain position of a char-Array.
But strings are basically char-Arrays, aren't they?
But this does not work:
char word[];
cin >> word[];
Whereas this works but I dont know how to fill the chars into an Array.
string s;
cin >> s;
I've tried this so far, but i got an compile error:
string s;
cin >> s;
char word[] = s;
I'm sorry, I've just started programming and I wonder if anyone has some advice for me :)
char word[];
You need to give the size of the array. Then, you can take input to it directly. If you wish to copy read the std::string to the character array, then you need to use safe string copy functions like strncpy. For example -
char word[10];
std::string str("Hello");
strncpy(word, str.c_str(), sizeof(word));
However, std::string is recommended in C++ rather than working with character arrays.

How to convert intptr_t to string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easiest way to convert int to string in C++
Does anyone know how to do that conversion?
I need to concatenate a intptr_t type to a string, and therefore need to convert it.
It's not an int, as It's a 64 bit OS.
Correct me if I'm wrong
Thanks
intptr_t is just a number. Therefore:
Easiest way to convert int to string in C++
...and others
Converting numbers to strings and strings to numbers
Simple:
std::to_string(ip);
Well, simple if you have C++11.
std::stringstream ss;
ss << ip;
ss.str();
(or if you prefer:
ss << std::hex << ip;
)

How to convert Integer to string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How to convert a number to string and vice versa in C++
Append an int to a std::string
I want to convert integer to string, any one help me for these conversion?
itoa(*data->userid,buff1,10);
itoa(*data->userphone,buff2,10);
For C++, use std::stringstream instead.
#include <sstream>
//...
std::stringstream ss;
ss << *data->userid;
std::string userId = ss.str();
or std::to_string if you have access to a C++11 compiler.
If you have a C++11 compiler with the new std::to_string function you can use that. Otherwise use the std::stringstream solution by Luchian.