Using strtok, need to insert null into vector

K

krini_pop

I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Thanks
 
R

red floyd

I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Thanks

Yeah, don't use strtok. :( The definition of strtok says that multiple
delimiters will be rolled into one. You might get your test into a
std::string, and use find_first_of()
 
K

Kristo

I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

First, you need to read the FAQ, paying special attention to section
5.8. Then, feel free to post again and we'll be glad to help.

Kristo
 
D

Default User

I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?


Here is one I've worked up for this sort of task. If you need
strtok()'s ability to have multiple delimiters, then you'll have to
work it out yourself.

#include <vector>
#include <string>
#include <sstream>

using namespace std;

void Explode(const string &inString, vector<string> &outVector, char
separator)
{
stringstream inStream(inString);
string tempString;

while (getline(inStream, tempString, separator))
outVector.push_back(tempString);
}

int main()
{
string s = "one,two,three,,five";
vector<string> vec;

Explode (s, vec, ',');


for (int i = 0; i < vec.size(); i++)
cout << i << ": " << vec << endl;

return 0;
}


Result:

0: one
1: two
2: three
3:
4: five




Brian
 
A

Alex Vinokur

I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Thanks

Look at
http://groups-beta.google.com/­group/perfo/msg/9d49a1be3a5c63­35?hl=en


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
K

krini_pop

Thank you for the help. It works. I appreciate it!

Default said:
I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?


Here is one I've worked up for this sort of task. If you need
strtok()'s ability to have multiple delimiters, then you'll have to
work it out yourself.

#include <vector>
#include <string>
#include <sstream>

using namespace std;

void Explode(const string &inString, vector<string> &outVector, char
separator)
{
stringstream inStream(inString);
string tempString;

while (getline(inStream, tempString, separator))
outVector.push_back(tempString);
}

int main()
{
string s = "one,two,three,,five";
vector<string> vec;

Explode (s, vec, ',');


for (int i = 0; i < vec.size(); i++)
cout << i << ": " << vec << endl;

return 0;
}


Result:

0: one
1: two
2: three
3:
4: five




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top