[英]split function for C++
是否有類似於Java的C ++拆分類型函數? 我知道無視,但我不太了解它,以及它對我的情況如何有效。
我的意見是:
{
item = ball
book = lord of the rings
movie = star wars
}
給出的輸入是<attribute>
= <value>
,我必須將兩者存儲在不同的字符串或整數中(取決於值,例如,如果是數字,則使用整數)。
使用Boost :: tokenizer就像你想做的那樣。 從手冊:
// 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";
}
}
使用strtok(): http : //www.cplusplus.com/reference/clibrary/cstring/strtok/ 。
只知道它不可重入因為它使用內部靜態變量,所以不要在嵌套循環或類似的東西中調用它兩次。
和編輯:
這是一個非常酷的SO解決方案,可以通過空格對整個字符串進行標記 - 您必須在=之后將值重新處理,但它會教你STL好:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.