How to use append() function In C++

Create a string and append characters one by one into that string by traversing into the container.

Syntax

string_name.append(1, character);

Example:

C++




// C++ program to convert vector of chars
// into string Using append() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    vector<char> v = { 'A', 'B', 'C', 'D', 'E' };
  
    string s;
  
    for (char& c : v) {
        s.append(1, c);
        s.append(1, ' ');
    }
  
    cout << s;
  
    return 0;
}


Output

A B C D E 

Convert Vector of chars to String in C++

Prerequisites: 

Here, we will discuss how to convert the vector of chars to std::string in C++. There are mainly 7 ways to convert Char Vector to String in C++ as follows:

  1. Using string constructor
  2. Using String Stream and iterator
  3. Using String Stream and index
  4. Using push_back() function
  5. Using append() function
  6. Using insert() function
  7. Using copy() method

Similar Reads

1. Using string constructor

Create a string and initialize it with a range-based constructor....

2. Using String Stream and iterator

...

3. Using String Stream and index

Create a stringstream, traverse the string through the iterator and add the element into the stringstream by dereferencing the iterator....

4. Using push_back() function

...

5. Using append() function

Create a stringstream, traverse the string through the index and add the element into the stringstream....

6. Using insert() function

...

7. Using copy() method

Create a string and push back characters one by one into that string through traversing into the container....