Tag Archives: join
C++ || How To Join & Concatenate An Array/Vector/Container Into A Delimited String Using C++
The following is a module with functions which demonstrates how to join and concatenate an array/vector/container into a delimited string using C++.
The function demonstrated on this page joins a container and returns the concatenated string according to the separator(s). The delimiter separator is included in the returned string only if there is more than one element.
The separator can be either a single character or multiple characters. If no separating characters are specified, the string is joined using a comma (‘,’).
1. Join – Integer Array
The example below demonstrates the use of ‘Utils::join‘ to join an array into a delimiter separated string.
In this example, the default separator is used to join the array, which is a comma (‘,’).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Join - Integer Array // Declare data int arry[] = { 1987, 19, 22, 2009, 2019, 1991, 28, 31 }; // Get array size int size = sizeof(arry) / sizeof(arry[0]); // Join using default separator std::string results = Utils::join(arry, arry + size); // Display results std::cout << results; // expected output: /* 1987,19,22,2009,2019,1991,28,31 */ |
2. Join – String Vector
The example below demonstrates the use of ‘Utils::join‘ to join a vector into a delimiter separated string.
In this example, a custom separator is used to join the vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Join - String Vector // Declare data std::vector<std::string> names = { "Kenneth", "Jennifer", "Lynn", "Sole" }; // Join using custom separator std::string results = Utils::join(names.begin(), names.end(), "; "); // Display results std::cout << results; // expected output: /* Kenneth; Jennifer; Lynn; Sole */ |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jun 5, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.h // Description: Handles general utility functions // ============================================================================ #pragma once #include <string> #include <sstream> namespace Utils { /** * FUNCTION: join * USE: Concatenates elements in the given range [first, last), using the * specified separator between each item * @param first: The first position of the sequence * @param last: The last position of the sequence * @param separator: The character(s) to use as a separator. Separator is * included in the returned string only if there is more than one element * @return: A string containing the elements delimited by the separator * character, or empty if values has zero elements */ template<typename InputIt> std::string join(InputIt first, InputIt last, const std::string& separator = ",") { std::ostringstream result; if (first != last) { result << *first; while (++first != last) { result << separator << *first; } } return result.str(); } }// http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jun 5, 2021 // Taken From: http://programmingnotes.org/ // File: program.cpp // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ #include <iostream> #include <string> #include <exception> #include <vector> #include "Utils.h" void display(const std::string& message); int main() { try { // Declare data int arry[] = { 1987, 19, 22, 2009, 2019, 1991, 28, 31 }; // Get array size int size = sizeof(arry) / sizeof(arry[0]); // Join using default separator std::string results = Utils::join(arry, arry + size); // Display results display(results); display(""); // Declare data std::vector<std::string> names = { "Kenneth", "Jennifer", "Lynn", "Sole" }; // Join using custom separator std::string results2 = Utils::join(names.begin(), names.end(), "; "); // Display results display(results2); } catch (std::exception& e) { display("\nAn error occurred: " + std::string(e.what())); } std::cin.get(); return 0; } void display(const std::string& message) { std::cout << message << std::endl; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.