
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to concatenate a std::string and an int in C++?
In this program we will see how to concatenate a string and integer type data in C++. To concatenate string and integer data, we have to convert integer to string at first. To convert it we are using stringstream. This provides some features. It takes number or string then make it string.
Input: String “str” and Number 10 Output: Concatenated string “str10”
Algorithm
Step 1: Take string and number Step 2: Convert number to string Step 3: Concatenate them Step 4: End
Example Code
#include <iostream> #include <sstream> using namespace std; string int_to_str(int x) { stringstream ss; ss << x; return ss.str(); } int main() { string my_str = "This is a string: "; int x = 50; string concat_str; concat_str = my_str + int_to_str(x); cout << concat_str; }
Output
This is a string: 50
- Related Articles
- Concatenate string to an int value in Java
- How to parse a string to an int in C++?
- How to convert a String to an int in Java
- How to convert a Java String to an Int?
- How can I concatenate str and int objects in Python?
- How to convert an int to string in C++?
- How to concatenate numerical vectors and a string to return a string in R?
- How to convert an std::string to const char* or char* in C++?
- How to convert std::string to LPCSTR in C++?
- How to convert std::string to LPCWSTR in C++?
- How to check if a C/C++ string is an int?
- How to concatenate variable in a string in jQuery?
- How to concatenate strings from different columns and an additional string using a MySQL query?
- How to concatenate a string with numbers in Python?
- How to convert std::string to lower case in C++?

Advertisements