C ++连接字符串和int [英] C++ concatenate string and int

查看:414
本文介绍了C ++连接字符串和int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这将很简单,但它提出了一些困难。如果我有

I thought this would be really simple but it's presenting some difficulties. If I have

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

如何组合它们以获取单个字符串John21?

How do I combine them to get a single string "John21"?

推荐答案

按字母顺序:

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

// 1. with Boost
result = name + boost::lexical_cast<std::string>(age);

// 2. with C++11
result = name + std::to_string(age);

// 3. with FastFormat.Format
fastformat::fmt(result, "{0}{1}", name, age);

// 4. with FastFormat.Write
fastformat::write(result, name, age);

// 5. with IOStreams
std::stringstream sstm;
sstm << name << age;
result = sstm.str();

// 6. with itoa
char numstr[21]; // enough to hold all numbers up to 64-bits
result = name + itoa(age, numstr, 10);

// 7. with sprintf
char numstr[21]; // enough to hold all numbers up to 64-bits
sprintf(numstr, "%d", age);
result = name + numstr;

// 8. with STLSoft's integer_to_string
char numstr[21]; // enough to hold all numbers up to 64-bits
result = name + stlsoft::integer_to_string(numstr, 21, age);

// 9. with STLSoft's winstl::int_to_string()
result = name + winstl::int_to_string(age);

// 10. With Poco NumberFormatter
result = name + Poco::NumberFormatter().format(age);




  1. 需要 Boost (仅限标题);大多数/所有平台

  2. 是安全的,需要C ++ 11( to_string() 已包含在 #include< string>

  3. 需要 FastFormat ,必须进行编译;大多数/所有平台

  4. 安全,快速;需要 FastFormat ,必须进行编译;大多数/所有平台

  5. 安全,缓慢,冗长;需要 #include< sstream> (来自标准C ++)

  6. 很脆弱(必须提供足够大的缓冲区)和verbose; itoa()是一个非标准扩展,不能保证适用于所有平台。

  7. 很脆弱(必须提供足够大的缓冲区),快速,不需要什么(是标准C ++);所有平台

  8. 很脆弱(您必须提供足够大的缓冲区),可能是最快的conversion ,verbose;需要 STLSoft (仅限标题);大多数/所有平台

  9. safe-ish(您不要使用多个 int_to_string()在单个语句中调用),快;需要 STLSoft (仅限标题);仅限Windows

  10. 安全,但速度缓慢;需要 Poco C ++ ;大多数/所有平台

  1. is safe, but slow; requires Boost (header-only); most/all platforms
  2. is safe, requires C++11 (to_string() is already included in #include <string>)
  3. is safe, and fast; requires FastFormat, which must be compiled; most/all platforms
  4. is safe, and fast; requires FastFormat, which must be compiled; most/all platforms
  5. safe, slow, and verbose; requires #include <sstream> (from standard C++)
  6. is brittle (you must supply a large enough buffer), fast, and verbose; itoa() is a non-standard extension, and not guaranteed to be available for all platforms
  7. is brittle (you must supply a large enough buffer), fast, and verbose; requires nothing (is standard C++); all platforms
  8. is brittle (you must supply a large enough buffer), probably the fastest-possible conversion, verbose; requires STLSoft (header-only); most/all platforms
  9. safe-ish (you don't use more than one int_to_string() call in a single statement), fast; requires STLSoft (header-only); Windows-only
  10. is safe, but slow; requires Poco C++ ; most/all platforms

这篇关于C ++连接字符串和int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆