简体   繁体   English

用于C ++的split函数

[英]split function for C++

Is there a split type function for C++ similar to Java? 是否有类似于Java的C ++拆分类型函数? 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). 给出的输入是<attribute> = <value> ,我必须将两者存储在不同的字符串或整数中(取决于值,例如,如果是数字,则使用整数)。

Use Boost::tokenizer as it does what you want to do. 使用Boost :: tokenizer就像你想做的那样。 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/ . 使用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 :) 这是一个非常酷的SO解决方案,可以通过空格对整个字符串进行标记 - 您必须在=之后将值重新处理,但它会教你STL好:)

Split a string in C++? 用C ++拆分一个字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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