How to Add Int to String in C++

  1. Use += Operator and std::to_string Function to Append Int to String
  2. Use std::stringstream to Add Int to String
  3. Use the append() Method to Add Int to String

This article will explain several methods of adding integer to string in C++.

Use += Operator and std::to_string Function to Append Int to String

The std::string class supports the most common form of concatenation using the core operators like + and +=. In the following example, we demonstrate the latter one since it’s the most eloquent solution. Before the int value is appended at the end of the string, the int value should be converted to the same type, which is implemented with the std::to_string function call.

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;

int main(){
    string app_str = "This string will be appended to ";
    int number = 12345;

    cout << app_str << endl;
    app_str += to_string(number);
    cout << app_str << endl;

    return EXIT_SUCCESS;
}

Output:

This string will be appended to
This string will be appended to 12345

The above method is also compatible with floating-point numbers as shown in the next code sample:

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;

int main(){
    string app_str = "This string will be appended to ";
    float fnumber = 12.345;

    cout << app_str << endl;
    app_str += to_string(fnumber);
    cout << app_str << endl;

    return EXIT_SUCCESS;
}

Output:

This string will be appended to
This string will be appended to 12.345000

Use std::stringstream to Add Int to String

stringstream can ingest multiple input types and store them as the string format. It provides easy-to-use operators and builtin methods to redirect the constructed string to console output.

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;
using std::stringstream;

int main(){
    string app_str = "This string will be appended to ";
    int number = 12345;
    stringstream tmp_stream;

    cout << app_str << endl;
    tmp_stream << app_str << number;
    cout << tmp_stream.str() << endl;

    return EXIT_SUCCESS;
}

Output:

This string will be appended to
This string will be appended to 12345

Use the append() Method to Add Int to String

append() is a member function of the std::basic_string class, and it can do multiple types of appending operations as specified by the parameters. In the simplest form, when a single string argument is passed, it appends to the object from which the method is called. As an alternative, it can take a single char and integer number, representing the appended count of a given char. We can see the full list of parameters in the manual.

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::to_string;

int main(){
    string app_str = "This string will be appended to ";
    int number = 12345;

    cout << app_str << endl;
    app_str.append(to_string(number));
    cout << app_str;

    return EXIT_SUCCESS;
}

Output:

This string will be appended to
This string will be appended to 12345

Related Article - C++ String

  • How to Convert String to Int in C++
  • How to Replace a Part of the String in C++