简体   繁体   中英

split function for C++

Is there a split type function for C++ similar to Java? I know of ignore, but I don't quite understand it, and how it'll work for my case.

My input is:

{
  item = ball
  book = lord of the rings
  movie = star wars
}

My input given is an <attribute> = <value> and I have to store the two in different strings, or integers (depending on the value, for example, if its a number, use an integer).

Use Boost::tokenizer as it does what you want to do. From the manual:

// simple_example_1.cpp
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>

int main(){
   using namespace std;
   using namespace boost;
   string s = "This is,  a test";
   tokenizer<> tok(s);
   for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
       cout << *beg << "\n";
   }
}

Use strtok(): http://www.cplusplus.com/reference/clibrary/cstring/strtok/ .

Just know that its not re-entrant because it uses an internal static variable, so don't call it twice in nested loops or anything like that.

and EDIT:

This is a very cool SO solution that would tokenize the whole string by spaces - you'd have to process the values back together after the = but it would teach you STL well :)

Split a string in C++?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM