boost::algorithm::join() in C++ Library
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++ program for the
// above approach
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace std;
using namespace boost::algorithm;
// Function to join 2 or more strings
void concatenate(vector<string>& v1)
{
// Joining the strings with a
// whitespace
string s1 = boost::algorithm::join(v1, " ");
cout << s1 << endl;
// Joining the strings with a '$'
string s2 = boost::algorithm::join(v1, "$");
cout << s2 << endl;
}
// Driver Code
int main()
{
// Vector container to hold
// the input strings
vector<string> v1;
v1.push_back("Geeks");
v1.push_back("For");
v1.push_back("Geeks");
// Function Call
concatenate(v1);
return 0;
}
Output
Geeks For Geeks Geeks$For$Geeks
Example 2: Below is the C++ program to implement the above approach.
// C++ program for the above approach
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost::algorithm;
// Function to join 2 or more strings
void concatenate(vector<string>& v1)
{
// Joining the strings with
// the string "..."
string s1 = boost::algorithm::join(v1, "...");
cout << s1 << endl;
}
// Driver Code
int main()
{
// Vector container to hold the
// input strings
vector<string> v1;
v1.push_back("Geeks");
v1.push_back("For");
v1.push_back("Geeks");
// Function Call
concatenate(v1);
return 0;
}
Output
Geeks...For...Geeks