連接 std::string 和 int
組合 std::string 和 int 形成單一字串可以是簡單的任務。以下是實現此目的的多種方法:
1。使用Boost
#include <boost/lexical_cast.hpp> std::string name = "John"; int age = 21; std::string result = name + boost::lexical_cast<std::string>(age);
2.使用 C 11
std::string name = "John"; int age = 21; std::string result = name + std::to_string(age);
3。使用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
4.使用{fmt} 庫
#include <fmt/core.h> std::string name = "John"; int age = 21; std::string result = fmt::format("{}{}", name, age);
5.使用IOStreams
std::string name = "John"; int age = 21; std::stringstream sstm; sstm << name << age; std::string result = sstm.str();
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);
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;
#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);
#include <stlsoft/winstl/int_to_string.hpp> std::string name = "John"; int age = 21; std::string result = name + winstl::int_to_string(age);
#include <Poco/NumberFormatter.h> std::string name = "John"; int age = 21; std::string result = name + Poco::NumberFormatter().format(age);
以上是如何在 C 中連接 std::string 和 Integer ?的詳細內容。更多資訊請關注PHP中文網其他相關文章!