CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2008
    Posts
    10

    Vector of Strings: help!

    I need to write a C++ program taking a vector of strings and separating out the words in each, storing them into a vector, but I don't know how. Could anyone help me please?


    Template Code:
    Code:
    vector <string> seperateWords (vector<string> lines) {
        vector<string > words;
        
        //Separating stuff goes here
    
        return words;
    }
    Thanks in advance!
    Last edited by assaf2b; April 12th, 2008 at 11:53 AM. Reason: Addition of Code tags.

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Vector of Strings: help!

    Use a stringstream to do the "dirty" work for you.
    Here's a minimal (and untested) example:
    Code:
    std::vector<std::string> Foo(const std::string& data)
    {
        std::vector<std::string> result;
    
        std::istringstream converter(data);
        std::string buffer;
    
        while(converter >> buffer)
        {
            result.push_back(buffer);
        }
    
        return result;
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    10

    Re: Vector of Strings: help!

    Thanks. Could you please explain what exactly have you done there?

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Vector of Strings: help!

    Quote Originally Posted by assaf2b
    Thanks. Could you please explain what exactly have you done there?
    What is it that you don't understand?

  5. #5
    Join Date
    Apr 2008
    Posts
    10

    Re: Vector of Strings: help!

    Everything, really. I was given this assignment in order to be accepted into a class, but I know little to nothing (as of now) regarding C++.

  6. #6
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Vector of Strings: help!

    You can't run before you learn how to walk.
    I suggest you pick up a (good) book and start reading ASAP if you want to get into that class.

  7. #7
    Join Date
    Apr 2008
    Posts
    10

    Re: Vector of Strings: help!

    Could anyone please just write, as a comment, next to each line what it's supposed to mean... as in, why is it there?

    I'd highly appreciate it.

  8. #8
    Join Date
    Oct 2004
    Posts
    296

    Re: Vector of Strings: help!

    Why do you think you should be accepted into the class if you can't do the assignment?

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vector of Strings: help!

    Quote Originally Posted by assaf2b
    Could anyone please just write, as a comment, next to each line what it's supposed to mean... as in, why is it there?
    With all due respect, that is ridiculous. No one learns programming that way, no matter what programming language is being taught.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Vector of Strings: help!

    Quote Originally Posted by assaf2b
    Thanks. Could you please explain what exactly have you done there?
    Look at the documentation for istringstream and it should be fairly obvious what he has done there.
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured