Maison > développement back-end > C++ > Comment puis-je concaténer un std::string et un Integer en C ?

Comment puis-je concaténer un std::string et un Integer en C ?

Barbara Streisand
Libérer: 2024-12-24 01:43:14
original
620 Les gens l'ont consulté

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

Concaténer un std::string et un int

Combiner un std::string et un int pour former une seule chaîne peut être un tâche simple. Voici plusieurs approches pour y parvenir :

1. Utiliser Boost

#include <boost/lexical_cast.hpp>

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

std::string result = name + boost::lexical_cast<std::string>(age);
Copier après la connexion

2. Utilisation de C 11

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

std::string result = name + std::to_string(age);
Copier après la connexion

3. Utilisation de 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
Copier après la connexion

4. Utilisation de la bibliothèque {fmt}

#include <fmt/core.h>

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

std::string result = fmt::format("{}{}", name, age);
Copier après la connexion

5. Utiliser IOStreams

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

std::stringstream sstm;
sstm << name << age;
std::string result = sstm.str();
Copier après la connexion

6. Utiliser 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);
Copier après la connexion

7. Utilisation de 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;
Copier après la connexion

8. Utilisation de 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);
Copier après la connexion

9. Utilisation de 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);
Copier après la connexion

10. Utilisation de Poco NumberFormatter

#include <Poco/NumberFormatter.h>

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

std::string result = name + Poco::NumberFormatter().format(age);
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal