當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ boost::split用法及代碼示例


這個函數類似於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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。