| | |
istringstream
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
I am using istringstream to put strings separated by commas to own std::strings.
I have done this wich works:
So I am extracting Value that consists of 6 strings separated by ",".
Now if I will take "f" away from Value. This will not work. The std::strings One to Five will not be assigned any strings as it is only 5 elements in the string Value now.
Why does this happening ?
Is this possible to make it work when you dont know how many strings you are trying to exctract from Value. The maximum will be 6 though.
I have done this wich works:
C++ Syntax (Toggle Plain Text)
std::string One, Two, Three, Four, Five, Six; std::string Value = "a,b,c,d,e,f"; istringstream is(Value); while ( getline(is, One, ',') && getline(is, Two, ',') && getline(is, Three, ',') && getline(is, Four, ',') && getline(is, Five, ',') && is >> Six) { //Action; }
So I am extracting Value that consists of 6 strings separated by ",".
Now if I will take "f" away from Value. This will not work. The std::strings One to Five will not be assigned any strings as it is only 5 elements in the string Value now.
Why does this happening ?
Is this possible to make it work when you dont know how many strings you are trying to exctract from Value. The maximum will be 6 though.
•
•
Join Date: Jul 2005
Posts: 1,692
Reputation:
Solved Threads: 267
Not if the substrings need to go into a rigid set of variables. You could declare an array/vector that could hold up to 6 strings and extract them out of the original string one at a time storing them in the array as you go. In the array they won't be assigned to string variables by the name of One, Two, etc, however.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
Okay, that sounds nice. I know exactly how vectors works and how to assign the elements but what I dont really know how to do, is to exctract each string separated by ',' in my std::string Value.
I know how to do if I know how many strings that are separated but not if I dont know how many strings that is separated by ','
Any idéas how to do that ?
Thanks...
I know how to do if I know how many strings that are separated but not if I dont know how many strings that is separated by ','
Any idéas how to do that ?
Thanks...
C++ Syntax (Toggle Plain Text)
std::vector<string> Store(5); std::string Value = "a,b,c,d,e,f"; //Strings can differ. This example is 6, //sometimes it can be 5,4,3,2 or 1
•
•
•
•
Not if the substrings need to go into a rigid set of variables. You could declare an array/vector that could hold up to 6 strings and extract them out of the original string one at a time storing them in the array as you go. In the array they won't be assigned to string variables by the name of One, Two, etc, however.
Last edited by Jennifer84; Mar 6th, 2008 at 6:17 pm.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
This is practially pretty much what Lerner said ...
C++ Syntax (Toggle Plain Text)
std::string Value = "a,b,c,d,e,f"; istringstream is(Value); std::string s; std::vector<string> v; while (getline(is, s, ',')) { // add item v.push_back(s); } // iterate through the vector and output each item ... for(std::vector<string>::iterator it = v.begin(); it != v.end(); it ++) { cout << *it << endl; }
Last edited by mitrmkar; Mar 6th, 2008 at 6:22 pm.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
Thank you very much mitrmkar and Lerner. That did solve it. I was not really sure how to continuosly get the next string after each ',' but now I know how to do that.
Thanks...
Thanks...

•
•
•
•
This is practially pretty much what Lerner said ...
C++ Syntax (Toggle Plain Text)
std::string Value = "a,b,c,d,e,f"; istringstream is(Value); std::string s; std::vector<string> v; while (getline(is, s, ',')) { // add item v.push_back(s); } // iterate through the vector and output each item ... for(std::vector<string>::iterator it = v.begin(); it != v.end(); it ++) { cout << *it << endl; }
![]() |
Similar Threads
- input from file into class (C++)
- parsing date strings mm/dd/yy?? (C++)
- Listing words in an Array of text (C++)
- Any Boolean Expression to Sum of Minterms (C++)
- Word Counting (C++)
- How many different numbers are there? (C++)
- Printing a Hash Table (C++)
- reading input ... quick C++ question ... (C++)
- Is ifs is a member function of ifstream class (C++)
Other Threads in the C++ Forum
- Previous Thread: Extract Words from a line in a .txt file
- Next Thread: about reference of a instance in class
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






