
Concaténer une chaîne et un entier en C
Concaténer une chaîne et un entier en C est une tâche courante, mais il peut être difficile de trouver la solution la plus efficace et la plus efficace. manière concise de le faire. Différentes méthodes peuvent être utilisées pour y parvenir, et le choix dépend de facteurs tels que les performances, la sécurité et la compatibilité de la plate-forme.
1. Utilisation du lexical_cast de Boost :
1 2 3 4 5 | # 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 std::to_string:
1 2 3 4 5 | # include <iostream>
std::string name = "John" ;
int age = 21;
std::string result = name + std::to_string(age);
|
Copier après la connexion
3. Utilisation de FastFormat.Format :
1 2 3 4 5 6 | # include <FastFormat.h>
std::string name = "John" ;
int age = 21;
std::string result;
fastformat::fmt(result, "{0}{1}" , name, age);
|
Copier après la connexion
4. Utilisation de FastFormat.Write :
1 2 3 4 5 6 | # include <FastFormat.h>
std::string name = "John" ;
int age = 21;
std::string result;
fastformat::write(result, name, age);
|
Copier après la connexion
5. Utilisation de la bibliothèque {fmt} :
1 2 3 4 5 | # include <fmt/format.h>
std::string name = "John" ;
int age = 21;
std::string result = fmt::format( "{}{}" , name, age);
|
Copier après la connexion
6. Utilisation d'IOStreams :
1 2 3 4 5 6 7 8 | # include <iostream>
# include <sstream>
std::string name = "John" ;
int age = 21;
std::stringstream sstm;
sstm << name << age;
std::string result = sstm.str();
|
Copier après la connexion
7. Utiliser itoa :
1 2 3 4 5 6 | # include <cstdio>
std::string name = "John" ;
int age = 21;
char numstr[21];
std::string result = name + itoa(age, numstr, 10);
|
Copier après la connexion
8. Utilisation de sprintf :
1 2 3 4 5 6 7 | # include <cstdio>
std::string name = "John" ;
int age = 21;
char numstr[21];
sprintf(numstr, "%d" , age);
std::string result = name + numstr;
|
Copier après la connexion
9. Utilisation de integer_to_string:
1 2 3 4 5 6 | # include <stlsoft/integer_to_string.hpp>
std::string name = "John" ;
int age = 21;
char numstr[21];
std::string result = name + stlsoft::integer_to_string(numstr, 21, age);
|
Copier après la connexion
10. Utilisation de winstl::int_to_string():
1 2 3 4 5 | # include <wintlsoft/int_to_string.h>
std::string name = "John" ;
int age = 21;
std::string result = name + winstl::int_to_string(age);
|
Copier après la connexion
11. Utilisation du NumberFormatter de Poco :
1 2 3 4 5 | # include <Poco/NumberFormatter.h>
std::string name = "John" ;
int age = 21;
std::string result = name + Poco::NumberFormatter().format(age);
|
Copier après la connexion
Les caractéristiques de performance, de sécurité et de compatibilité de chaque méthode sont détaillées pour vous aider à choisir la meilleure approche pour vos besoins spécifiques.
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!