istringstream

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

istringstream

 
0
  #1
Mar 6th, 2008
I am using istringstream to put strings separated by commas to own std::strings.
I have done this wich works:

  1. std::string One, Two, Three, Four, Five, Six;
  2. std::string Value = "a,b,c,d,e,f";
  3. istringstream is(Value);
  4.  
  5. while ( getline(is, One, ',') && getline(is, Two, ',') && getline(is, Three, ',') && getline(is, Four, ',') && getline(is, Five, ',') && is >> Six)
  6. {
  7. //Action;
  8. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: istringstream

 
0
  #2
Mar 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: istringstream

 
0
  #3
Mar 6th, 2008
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...
  1. std::vector<string> Store(5);
  2. std::string Value = "a,b,c,d,e,f"; //Strings can differ. This example is 6,
  3. //sometimes it can be 5,4,3,2 or 1



Originally Posted by Lerner View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: istringstream

 
0
  #4
Mar 6th, 2008
This is practially pretty much what Lerner said ...
  1. std::string Value = "a,b,c,d,e,f";
  2. istringstream is(Value);
  3. std::string s;
  4. std::vector<string> v;
  5.  
  6. while (getline(is, s, ','))
  7. {
  8. // add item
  9. v.push_back(s);
  10. }
  11. // iterate through the vector and output each item ...
  12. for(std::vector<string>::iterator it = v.begin(); it != v.end(); it ++)
  13. {
  14. cout << *it << endl;
  15. }
Last edited by mitrmkar; Mar 6th, 2008 at 6:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: istringstream

 
0
  #5
Mar 6th, 2008
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...

Originally Posted by mitrmkar View Post
This is practially pretty much what Lerner said ...
  1. std::string Value = "a,b,c,d,e,f";
  2. istringstream is(Value);
  3. std::string s;
  4. std::vector<string> v;
  5.  
  6. while (getline(is, s, ','))
  7. {
  8. // add item
  9. v.push_back(s);
  10. }
  11. // iterate through the vector and output each item ...
  12. for(std::vector<string>::iterator it = v.begin(); it != v.end(); it ++)
  13. {
  14. cout << *it << endl;
  15. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC