开发者

how to tokenize a string [duplicate]

This question already has answers here: 开发者_C百科 Closed 11 years ago.

Possible Duplicate:

How do I tokenize a string in C++?

hello every one i want to divide my string into two parts based on '\t' is there any built in function i tried strtok but it take char * as first in put but my variable is of type string thanks


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

int main() {
   std::string str("abc\tdef");
   char split_char = '\t';
   std::istringstream split(str);
   std::vector<std::string> token;

   for(std::string each; std::getline(split, each, split_char); token.push_back(each));
}


Why can't you use C standard library?

Variant 1. Use std::string::c_str() function to convert a std::string to a C-string (char *)

Variant 2. Use std::string::find(char, size_t) to find a necessary symbol ('\t' in your case) than make a new string with std::string::substr. Loop saving a 'current position' till the end of line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜