连接 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();
6.使用 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);
7.使用 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;
8.使用STLSoft的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);
9.使用STLSoft的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);
10。使用 Poco NumberFormatter
#include <Poco/NumberFormatter.h> std::string name = "John"; int age = 21; std::string result = name + Poco::NumberFormatter().format(age);
以上是如何在 C 中连接 std::string 和 Integer ?的详细内容。更多信息请关注PHP中文网其他相关文章!