boost::algorithm::join() in C++ Library
Last Updated :
09 Mar, 2022
The library Boot.StringAlgorithms provides many functions for string manipulations. The string can be of type std::string, std::wstring, or any instance of the class template std::basic_string.
boost::algorithm:join():
The join() function in the C++ boost library is included in the library “boost/algorithm/string”. This function is used to join two or more strings into one long string by adding a separator between the strings. The strings to be joined are provided in a container like a vector of string. Popular used containers are std::vector<std::string>, std::list<boost::iterator_range<std::string::iterator>.
Syntax:
join(container, separator)
Parameters:
- container: It contains all the strings which have to be joined.
- separator: It is a string that separates the joined strings.
Example 1: Below is the C++ program to implement the above approach.
C++
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace std;
using namespace boost::algorithm;
void concatenate(vector<string>& v1)
{
string s1 = boost::algorithm::join(v1, " " );
cout << s1 << endl;
string s2 = boost::algorithm::join(v1, "$" );
cout << s2 << endl;
}
int main()
{
vector<string> v1;
v1.push_back( "Geeks" );
v1.push_back( "For" );
v1.push_back( "Geeks" );
concatenate(v1);
return 0;
}
|
Output
Geeks For Geeks
Geeks$For$Geeks
Example 2: Below is the C++ program to implement the above approach.
C++
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost::algorithm;
void concatenate(vector<string>& v1)
{
string s1 = boost::algorithm::join(v1, "..." );
cout << s1 << endl;
}
int main()
{
vector<string> v1;
v1.push_back( "Geeks" );
v1.push_back( "For" );
v1.push_back( "Geeks" );
concatenate(v1);
return 0;
}
|
Output
Geeks...For...Geeks
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...