How to concatenate a string and int in C++?

In this article, we will discuss how to concatenate a string and an int in C++.

Table Of Contents

Introduction

We have given a string & an int, and we have to concatenate/merge them into a single string. For example, we have a string strValue, and an int num, like this,

string strValue = "FIFA - ";
int num = 2022;

After concatenating this string strValue with int variable num, the final string should be like,

"FIFA - 2022"

In order to concatenate a std::string and int, we have to convert the given integer to a string first, because C++ did not support the concatenation of variables of different data types. There are different ways to do this. Let’s discuss them one by one.

Advertisements

Method 1: Using std::stringstream() function

In this approach, we will use the operator<< of std::stringstream. It will be used to convert the given integer variable to string. Once the integer is converted into a string, we will concatenate the two strings with the concatenation operator (+) and get the new merged string. The time complexity in this approach is in the order of O(1), while the space complexity of the algorithm in this approach is O(n), where n is the length of output string.

The std::stringstream Class in C++ can be used to treat string objects as a stream of characters, thus the operations insertion and extraction can be applied to/from the string just as similar to cin and cout. One can use these operations mostly to convert the given numerical data to string or vice versa. In our case we will use the insertions operator (<<) to insert the integer data to stream buffer, and then will use the str() method of std::stringstream as the extraction operator to get the stream in the form of string.

Let’s see an example,

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

using namespace std;

int main()
{
    string strValue = "FIFA - ";
    int num = 2022;

    // Create a string stream
    std::stringstream streamObj;

    // Insert integer to the string stream
    streamObj << num;

    // Merge string "strValue" with the string from stringstream 
    string mergedStr = strValue + streamObj.str();

    cout << "Concatenated String : " << mergedStr << endl;

    return 0;
}

Output:

Concatenated String : FIFA - 2022

Method 2: Use std::to_string() function

In this approach, we will use the std::to_string() function, which accepts an integer value, and returns it as a string. After converting int value to string, we wil concatnate the two strings with the concatenation operator (+) and return the output. The time complexity in this case is of the order O(1), while the space complexity is equal to the legth of output string.

Let’s see an example,

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string strValue = "FIFA - ";
    int num = 2022;

    // convert int to string, and add it to the string variable "strValue"
    string mergedStr = strValue + to_string(num);

    cout << "Concatenated String : " << mergedStr << endl;

    return 0;
}

Output

Concatenated String : FIFA - 2022

It concatenated a string with an int.

Method 3: Using boost::lexial_cast<> from the Boost Library

In this approach we will use boost::lexial_cast() function, which is defined in the Boost Library. This function will take the integer value as input, and returns that as a string. After which, we can concatenate the given string to this output string and print the resultant string. The time complexity in this approach is of the order O(1), while the space compexity is linear.

The boost::lexial_cast<> function can be accessed from Boost.Lexial_cast which is defined in "boost/lexial_cast.hpp" file. It is a cast operator which can convert the numeric value to string, representing that value or vice versa. In case, the conversion fails , it throws an exception of type bad_lexial_cast, which is usually derived from bad_cast. It accepts the int value as argument and returns the required string.

Let’s see an example,

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>

using namespace std;

int main()
{
    string strValue = "FIFA - ";
    int num = 2022;

    // convert int to string, and add it to the string variable "strValue"
    string mergedStr = strValue + boost::lexical_cast<std::string>(num);

    cout << "Concatenated String : " << mergedStr << endl;

    return 0;
}

Output

Concatenated String : FIFA - 2022

It concatenated a string with an int.

Summary

Today, we came accross various methods to concatenate a given integer with a string value. In all the approaches, we have first converted the numeric value of integer data type into a string, and then concatenated it to the original string value. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top