这个函数类似于C中的strtok。输入序列被分割成token,用分隔符分隔。分隔符通过谓词给出。
用法:
Template: split(Result, Input, PredicateT Pred); 参数: Input: A container which will be searched. Pred: A predicate to identify separators. This predicate is supposed to return true if a given element is a separator. Result: A container that can hold copies of references to the substrings. 返回: A reference the result
应用:它用于将字符串拆分为由分隔符分隔的子字符串。
例:
Input:boost::split(result, input, boost::is_any_of("\t")) input = "geeks\tfor\tgeeks" Output:geeks for geeks Explanation:Here in input string we have "geeks\tfor\tgeeks" and result is a container in which we want to store our result here separator is "\t".
CPP
// C++ program to split
// string into substrings
// which are separated by
// separator using boost::split
// this header file contains boost::split function
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
string input("geeks\tfor\tgeeks");
vector<string> result;
boost::split(result, input, boost::is_any_of("\t"));
for (int i = 0; i < result.size(); i++)
cout << result[i] << endl;
return 0;
}
输出:
geeks for geeks
相关用法
注:本文由纯净天空筛选整理自pawan_asipu大神的英文原创作品 boost::split in C++ library。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。