read string by word by word

Hi im not talking about read word from outside source.

let say i declared string

string temp = "hello my name is temp";

I want to put word by word to string vector.

[ hello, my , name, is , temp ];

any idea?
This is known as "tokenizing" a string, and now that you know the term for it, you'll have no trouble finding many solutions. Here's a start:

http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c
plain C++:

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
    string temp = "hello my name is temp";
    vector<string> v;
    istringstream buf(temp);
    for(string word; buf >> word; )
        v.push_back(word);
}


boost, as mentioned on this forum earlier today

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
int main()
{
     std::string temp = "hello my name is temp";
     std::vector<std::string> v;
     boost::algorithm::split(v, temp, boost::algorithm::is_any_of(" "));
}
Topic archived. No new replies allowed.