Thread: String splitting algorithm help

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    String splitting algorithm help

    Hi,

    Theres probably a really simple way of doing this but i cant work it out :\.

    I have a string which contains this sort of data: "group1\r\ngroup2\r\ngroup3\r\n..."

    I'm using \r\n as the splitting characters, and extracting each substring into a vector array of strings, although i don't know if thats the best structure to use? I cant use a standard array, as i don't know the number of elements.

    Anyway heres the code i have so far (probably complete rubbish)

    glist is the string with the data.

    Code:
            vector<string> group_list;
    	int pos_a = 0;
    	int pos_b = 0;
    	
    	for(int i = 0; i < glist.length(); i++)
    	{
    		pos_b = glist.find("\n", pos_b);
    		
    		string temp(glist.substr(pos_a, pos_b));
    		group_list.push_back(temp);
    		pos_a = pos_b + 1;
    	}
    I know thats fairly wrong, but im unsure how to disregard the \r\n characters completely?

    Maybe i would be better converting it to a char array and working with that?

    Thanks for any advice,
    Jack
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is there a reason you are using \r and \n? Why not just \n? If you could use \n only, then I would just use an istringstream initialized with glist and getline in a loop to find each line. That will discard all the newlines and you can add each line to the vector. It's possible that it will work with \r\n also, but I'm not sure.

    Your vector code looks good. If you continued with a similar approach as you have there, you'd want it to be a while loop that runs while pos_b is not string::npos. Make sure to check that before using substr.

  3. #3
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Sorry should have mentioned, the data is pulled in from a socket connection, i didnt choose to use the \r\n chars.
    TNT
    You Can Stop Me, But You Cant Stop Us All

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can still try a stringstream, it may correctly handle \r\n. If not, keep working on your current implementation, it's not that far off.

  5. #5
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Thanks alot, how do i use stringsteams getline?

    Im trying this

    Code:
    	stringstream MyStream;
    	
    	group_list.push_back(MyStream.getline(glist, glist.length()));
    But im not having much luck.
    TNT
    You Can Stop Me, But You Cant Stop Us All

  6. #6
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    << operator.

    std::stringstream c;
    c << somevariable;

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That example doesn't help here, IdioticCreation, since we want to use an input stream and getline.

    (TNT) Do you know how to use getline with cin or an ifstream? It is exactly the same, except you use the istringstream variable instead of cin or the ifstream variable. You'll want to do getline separately from push_back, since it fills the string with the data, it doesn't return it. Also, make sure you are using the proper version of getline. The MyStream.getline version is for C style strings, but you are using C++ strings.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM