Heim >Backend-Entwicklung >C++ >Wie kann ich einen std::string und eine Ganzzahl in C verketten?
Das Verketten eines std::string und eines int
Das Kombinieren eines std::string und eines int zu einem einzelnen String kann ein sein einfache Aufgabe. Hier sind mehrere Ansätze, um dies zu erreichen:
1. Boost verwenden
#include <boost> std::string name = "John"; int age = 21; std::string result = name + boost::lexical_cast<:string>(age);</:string></boost>
2. Verwendung von C 11
std::string name = "John"; int age = 21; std::string result = name + std::to_string(age);
3. Verwendung von FastFormat
#include <fastformat> 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</fastformat>
4. Verwendung der {fmt}-Bibliothek
#include <fmt> std::string name = "John"; int age = 21; std::string result = fmt::format("{}{}", name, age);</fmt>
5. Verwendung von IOStreams
std::string name = "John"; int age = 21; std::stringstream sstm; sstm <p><strong>6. Verwendung von itoa</strong></p><pre class="brush:php;toolbar:false">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);
7. Sprintf verwenden
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;
8. Verwendung von integer_to_string von STLSoft
#include <stlsoft> 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);</stlsoft>
9. Verwenden von winstl::int_to_string()
#include <stlsoft> std::string name = "John"; int age = 21; std::string result = name + winstl::int_to_string(age);</stlsoft>
10 von STLSoft. Verwenden von Poco NumberFormatter
#include <poco> std::string name = "John"; int age = 21; std::string result = name + Poco::NumberFormatter().format(age);</poco>
Das obige ist der detaillierte Inhalt vonWie kann ich einen std::string und eine Ganzzahl in C verketten?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!