Jump to content
  • Advertisement
Sign in to follow this  
Daniel Miller

Is there already a function for this?

This topic is 5003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote an extremely simple function for extracting text word by word out of an std:string. However, for future reference, is there already a function for this in the standard library?

Share this post


Link to post
Share on other sites
Advertisement
I'm sure it could be done with a single function call, but I am not familiar enough with the algorithm section of STL to tell you off-hand. This works though:


#include <sstream>
using std::istringstream;

#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::endl;

int main(void)
{
string str = "Hello World";

istringstream iss(str);

string temp_string;

while(iss >> temp_string)
cout << temp_string << endl;

return 0;
}


Share this post


Link to post
Share on other sites
The C function strtok() will do this, but is evil ten ways from Sunday. boost::tokenizer isn't part of the standard library, but does this (and does it well, and elegantly).

Share this post


Link to post
Share on other sites
On the topic of this, because I loved this method so much, here is something from Oluseyi (fixed from his post):

std::vector<std::string> tokenize(const std::string & str, const std::string & delim)
{
using namespace std;
vector<string> tokens;

size_t p0 = 0, p1 = string::npos;
while(p0 != string::npos)
{
p1 = str.find_first_of(delim, p0);
if(p1 != p0)
{
string token = str.substr(p0, p1 - p0);
tokens.push_back(token);
}
p0 = str.find_first_not_of(delim, p1);
}

return tokens;
}





It returns a vector of the tokens parsed from the string. The best part is that you can specify multiple delim's to use in order to get tokens back. Quite handly and neat if you ask me [smile]

Example:

#include <vector>
#include <string>
#include <iostream>

std::vector<std::string> tokenize(const std::string & str, const std::string & delim)
{
using namespace std;
vector<string> tokens;

size_t p0 = 0, p1 = string::npos;
while(p0 != string::npos)
{
p1 = str.find_first_of(delim, p0);
if(p1 != p0)
{
string token = str.substr(p0, p1 - p0);
tokens.push_back(token);
}
p0 = str.find_first_not_of(delim, p1);
}

return tokens;
}

void main()
{
std::vector<std::string> tokens = tokenize("Width = 640","=; ");

for(int x=0;x<tokens.size();x++)
std::cout << tokens.at(x) << std::endl;
}


Will give you:

Width
640

Share this post


Link to post
Share on other sites
Thank you all for your responses. My way also uses find_first_of, which makes me feel special, but I use a map instead of a vector. I will check out boost (why didn't I think of that?), and that other link.

EDIT: I don't think using << would work because I have to differenciate between ' ' and '\n'.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!