×

Boost split in C++ library

Boost::split in C++ library

Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm library, is examined in this article. The latter has numerous techniques for manipulating strings, including clipping and replacing.

By dividing the given string sequence into tokens and a delimiter, the boost: split function can be used. The third parameter should be a predicate function that specifies the delimiter. If the specified element is a delimiter, the function should return true.

To identify spaces in the provided text and separate them into tokens, we supply an isspace function object in the example that follows. To save tokenized substrings, boost::split also requires a destination sequence container. It should be noted that the function call overwrites the destination container's prior contents after the first parameter, which is the destination container itself.

Example 1:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
//usage of std 
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
int main() {
    string text = "my name is khan";
    vector<string> words;
    boost::split(words, text, isspace);
    for (const auto &item : words) {
        cout << item << "; ";
    }
    cout << endl;
    return EXIT_SUCCESS;
}

Output:

Boost::split in C++ library

When two or more delimiters are close to one another, the boost::split function in the preceding code sample stores empty strings. Although, as demonstrated in the following example, we can specify the fourth optional argument boost::token compress on, which causes neighbouring delimiters to be combined. Keep in mind that in order for these two code samples to operate properly, Boost libraries must be installed on the system.

Example 2:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
using std::stringstream;
//usage of libraries
int main() {
    string text = " my name is khan ";
    vector<string> words;


    boost::split(words, text, isspace, boost::token_compress_on);
    for (const auto &item : words) {
        cout << item << "; ";
    }
    cout << endl;


    return EXIT_SUCCESS;
}

Output:

Boost::split in C++ library

Example 3:

/ /C++ program to divide a string into many substrings separated by
//using a separator boost::split
//this header document includes boost::split function
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
	string input("my name is khan ");
	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:

Boost::split in C++ library

Related Topics

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

Top 5 IDEs for C++ That You Should Try Once

In the past decades, creating an application or interface from the very basic idea, the developer has to struggle a lot for it. Because an application is a combination of...

3 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.