Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

boost::split in C++ library

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

This function is similar to strtok in C. Input sequence is split into tokens, separated by separators. Separators are given by means of the predicate.
Syntax: 

Template:
split(Result, Input, Predicate 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
 Time Complexity: O(n)
 Auxiliary Space: O(1)

Application : It is used to split a string into substrings which are separated by separators. 
Example: 

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;
}


Output: 

geeks
for
geeks        

 

My Personal Notes arrow_drop_up
Last Updated : 07 May, 2023
Like Article
Save Article
Similar Reads