Is there already a function for this?

Started by
4 comments, last by Daniel Miller 19 years ago
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?
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;}
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).
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:
Width640
Al-go-rithms
"after many years of singularity, i'm still searching on the event horizon"
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'.

This topic is closed to new replies.

Advertisement