×

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

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

Reverse a Number in C++

In this tutorial, you'll learn how to utilize a C++ program to reverse a number entered by a user at runtime. Because there are various ways to write a C++...

3 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

C++ Friend function

A friend function has the right to access all private and protected members of a class although it is defined outside that class' scope. Syntax class className{     ......     friend retyrn_type function_Name(argument);     .......   }   return_type function_Name(argument){     ......   } C++ friend function Example #include <iostream>   using namespace std;   class Length   {       private:           int meter;       public:           Length(): meter(5) { }           friend int addMethod(Length); //friend function declaration   };   int addMethod(Length l) // friend function definition   {       l.meter += 10; //accessing private data from non-member function       return l.meter;   }   int main()   {       Length L;       int totallength;       totallength=addMethod(L);       cout<<"Length: "<< totallength;       return 0;   } Output: Length: 15   C++ friend function...

1 minute read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.