Splitting a vector of strings into tokens?

This is a discussion on Splitting a vector of strings into tokens? within the C++ Programming forums, part of the General Programming Boards category; If I have: vector<string> line(k) Which each line can essentially be anything and k is some user specified thing, how ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Splitting a vector of strings into tokens?

    If I have:
    vector<string> line(k)

    Which each line can essentially be anything and k is some user specified thing, how would I go about creating a new vector of strings from this that only contains words?

    For example, say the input was:
    Code:
    This            is    a  reaaally      annoying
           test        I    have   to do      
      and I     really    don't   get
    it at    all
    The way I have coded my program now, each line is a string in vector<string> line.
    I am trying to create a new vector that instead contains:
    Code:
    This
    is
    a
    reaaally
    annoying
    etc. etc.
    The reason I ask is because I need to find a way to remove the trailing spaces from the input. In addition, I also have to make each line of output 30 characters, or start a new line if adding in one more word would put the line over the limit.

    I can provide any clarification if necessary.

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    I haven't tested this, but I think this is one way of doing it:

    Code:
    std::string Text = "This            is    a  reaaally      annoying...";
    std::vector< std::string > Words;
    std::string Temp;
    
    for ( int i = 0; i < Text.size(); ++i )
    {
    	if ( Text[i] == ' ' && Temp.size() > 0 )
    	{
    		Words.push_back( Temp );
    		Temp = "";
    	}
    	else
    		Temp += Text[i];
    }
    If you just want to get rid of the extra spaces, you can avoid the vector and have a loop that searches for a space, if there is a space before it, then erase it.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Try using a std::stringstream, since it usually splits up words for you...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    yea, stringstream. that's a nice solution.
    For example, you collect the words into the vector "result", then for each line[i], you could

    Code:
    string temp;
    istrstream in(line[i].c_str());
    while(!in.eof()){
       in >> temp;
       result.push_back(temp);
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> For example, you collect the words into the vector "result", then for each line[i], you could

    FYI...

    Don't use eof() to control the loop, you'll often end up with one duplicate value at the end of the vector.

    Also, you should use a stringstream, not strstream since strstreams have been deprecated.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    26
    Since you mentioned that you are getting the string from input, I think istream_iterator is more appropriate.

    You can just get all the strings using a one liner:

    Code:
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	vector<string> vec;
    
            // get it
    	copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(vec));
    
            // print it
    	copy(vec.begin(), vec.end(), ostream_iterator<string>(cout, " "));
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. splitting strings...
    By newbie_socketsp in forum C Programming
    Replies: 4
    Last Post: 08-15-2008, 10:17 AM
  2. splitting up names (strings)
    By Butters007 in forum C Programming
    Replies: 8
    Last Post: 12-07-2006, 12:13 AM
  3. Splitting A String Into Tokens!
    By bobthebullet990 in forum C Programming
    Replies: 15
    Last Post: 03-22-2006, 08:24 AM
  4. Splitting strings into words.
    By 0x7f in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2003, 02:49 PM
  5. splitting strings
    By Unreg1 in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2002, 10:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21