Home > Backend Development > C++ > How Can I Concatenate a std::string and an Integer in C ?

How Can I Concatenate a std::string and an Integer in C ?

Barbara Streisand
Release: 2024-12-24 01:43:14
Original
557 people have browsed it

How Can I Concatenate a std::string and an Integer in C  ?

Concatenating a std::string and an int

Combining a std::string and an int to form a single string can be a straightforward task. Here are multiple approaches to achieve this:

1. Using Boost

#include <boost/lexical_cast.hpp>

std::string name = "John";
int age = 21;

std::string result = name + boost::lexical_cast<std::string>(age);
Copy after login

2. Using C 11

std::string name = "John";
int age = 21;

std::string result = name + std::to_string(age);
Copy after login

3. Using FastFormat

#include <fastformat/format.hpp>

std::string name = "John";
int age = 21;

std::string result;
fastformat::fmt(result, "{0}{1}", name, age); // FastFormat.Format
fastformat::write(result, name, age); // FastFormat.Write
Copy after login

4. Using the {fmt} library

#include <fmt/core.h>

std::string name = "John";
int age = 21;

std::string result = fmt::format("{}{}", name, age);
Copy after login

5. Using IOStreams

std::string name = "John";
int age = 21;

std::stringstream sstm;
sstm << name << age;
std::string result = sstm.str();
Copy after login

6. Using itoa

std::string name = "John";
int age = 21;

char numstr[21]; // enough to hold all numbers up to 64-bits
std::string result = name + itoa(age, numstr, 10);
Copy after login

7. Using sprintf

std::string name = "John";
int age = 21;

char numstr[21]; // enough to hold all numbers up to 64-bits
sprintf(numstr, "%d", age);
std::string result = name + numstr;
Copy after login

8. Using STLSoft's integer_to_string

#include <stlsoft/integer_to_string.hpp>

std::string name = "John";
int age = 21;

char numstr[21]; // enough to hold all numbers up to 64-bits
std::string result = name + stlsoft::integer_to_string(numstr, 21, age);
Copy after login

9. Using STLSoft's winstl::int_to_string()

#include <stlsoft/winstl/int_to_string.hpp>

std::string name = "John";
int age = 21;

std::string result = name + winstl::int_to_string(age);
Copy after login

10. Using Poco NumberFormatter

#include <Poco/NumberFormatter.h>

std::string name = "John";
int age = 21;

std::string result = name + Poco::NumberFormatter().format(age);
Copy after login

The above is the detailed content of How Can I Concatenate a std::string and an Integer in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template