output from strtok

I have string say "Hello How are You".
After I apply strtok() function to it in want to store it in and array
such that
Array[0]="Hello";
Array[1]="How";
and so on.

If I can not do this can you anyone please tell me another data structure such that i will be able to access the words of the string seperately.

Note: I originall have the data in type string
I don't know if you can convert a single string to an array of strings, but why can't you bypass this problem with making the array in the first place?
http://www.cplusplus.com/reference/clibrary/cstring/strtok/


In the example code - you could assign the string to a positon in an array or push them into a vector or some other STL container.

This is a C method though - someone might know a better C++ methodology.

HTH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

    using namespace std;

int main()
{
    string line = "Hello How are You";
    vector <string> word;
    istringstream ss;
    ss.str(line);

    string temp;
    while(ss >> temp)
        word.push_back(temp);

    for (int i=0; i<word.size(); i++)
        cout << word[i] << endl;

    return 0;
}
Hello
How
are
You
Last edited on
I am new to vectors, so i an going to need a little more help
1. how do i compare if a word is present in a vector or not;
for e.g
if hello is present in the vector

how can i do this for many strings
do i use an array of vector and put in for loop


Thank You Very much
I really appreciate it
A vector can be regarded very much like an ordinary array. The main advantage is the vector can automatically resize itself as more items are added. Very simple to use.

Individual items can be accessed with item[index] just like an array. The first entry is item[0], just like an array.

To see whether "hello" is in the vector, the same as for an array, you'd have to test each item.

If you're uncomfortable with vectors, rather than using my code:
 
    word.push_back(temp);
you would do something like
1
2
    array[i] = temp;
    i++;


See http://www.cplusplus.com/reference/stl/vector/

I also used stringstream in my code.
http://www.cplusplus.com/reference/iostream/istringstream/
Your Code will do just fine.
my question is that I have multiple strings let say 54(this number can vary);

i want to perform the same operation like the one done in your above code to every string and store them in some variable which i can access later on for comparison.
You can indeed have a vector containing another vector. I think that's what you are asking.
vector<vector<string> > list;
yes, that is exactly what i am asking can you please help explain to me the code of how to do this, let say for 3 strings e.g string1="Hello how are"; string2="I am Fine"; string3="Good";

Really appreciate you helping me.
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    string line = "Hello How are You";

    string temp;
    istringstream ss;
    
    vector <string> word;
    vector <vector <string> > list;

    {
        ss.clear();
        word.clear();
        ss.str(line);
        while(ss >> temp)
            word.push_back(temp);
        list.push_back(word);
    }


Repeat lines 10 to 15 as many times as you like, the only thing at will need to change for each iteration is the contents of "line".
In this case how do i access the individual words.
lets say the first word of the string2.
Same as in the first example. Just need two subscripts, list[j][i]

There's a related example which may be useful here:
http://www.cplusplus.com/forum/general/84647/
Thank You very very much
How can I display the above words in vector containing another vector.
What will be the limits of both the for loops.
To find how many items in a vector, use the size() function.
http://www.cplusplus.com/reference/stl/vector/

See the example in this post: http://www.cplusplus.com/forum/general/84647/#msg453718
u < data.size() and v < data[u].size()

That's similar to my own post above, http://www.cplusplus.com/forum/beginner/84633/#msg453672
1
2
    for (int i=0; i<word.size(); i++)
        cout << word[i] << endl;
This is a prime example of where C++ code runs much slower than C code. My guess? Maybe 100 times slower, maybe 1000.

Consider what the C code would do.
1. You'd allocate a fixed length buffer on the stack. Cost zero.
2. Read the data into the string.
3. Call strtok to tokenize the string using a static context. Cost char comparison against each token character, plust writing NULL into the string and updating the context pointer.
4. print the value out.


Consider the C++ code:
1. Create a string object. Cost initially near zero, just a couple of values set (length and pointer).

2. Read the data into the string. Cost regrowing the target string to be large enough to hold the value, calls malloc possibly several times.

3. Create an istringstream from the string. These stringstream things have been shown to be expensive previously.
http://www.cplusplus.com/forum/general/74026/#msg396186

4. Create a vector of strings. Cost initially near zero, just a couple of values set (length and pointer).

5. Create another string to read from the istringstream. Cost initially near zero, just a couple of values set (length and pointer).

6. Read from istring stream into string until stream goes bad and append to vector.
This is a linear search thru the istream, checking against a list of delimiters.
Grows the target string, call to malloc eventually.
Appends the string to the vector, grows the vector logarithmicly, calling malloc and string copy constructors/destructors each time.
Thank You Very Much
Topic archived. No new replies allowed.