📜  C++库中的boost :: split

📅  最后修改于: 2021-05-30 16:27:06             🧑  作者: Mango

此函数类似于C中的strtok。输入序列被拆分为标记,并由分隔符分隔。分隔符是通过谓词给出的。

句法:

Template:
split(Result, Input, PredicateT Pred);

Parameters:
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.

Returns: 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".
// C++ program to split
// string into substrings
// which are separated by
// separater using boost::split
  
// this header file contains boost::split function
#include 
#include 
using namespace std;
  
int main()
{
    string input("geeks\tfor\tgeeks");
    vector 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        
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”