如何将一个 std::string 和一个 int 进行拼接操作

775

我以为这很简单,但它却有些困难。如果我有

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

如何将它们合并为一个字符串"John21"


1
Herb Sutter在这个主题上有一篇很好的文章:"Manor Farm的字符串格式化程序"。他涵盖了Boost::lexical_caststd::stringstreamstd::strstream(已弃用)以及sprintfsnprintf的比较。 - Fred Larson
让我补充一下:我尝试了 'str = "hi"; str += 5; cout << str;',但没有看到任何效果。结果发现这调用了 operator+=(char) 并添加了一个不可打印的字符。 - daveagp
25个回答

11
#include <sstream>

template <class T>
inline std::string to_string (const T& t)
{
   std::stringstream ss;
   ss << t;
   return ss.str();
}

那么你的使用情况会看起来像这样:
   std::string szName = "John";
   int numAge = 23;
   szName += to_string<int>(numAge);
   cout << szName << endl;

搜索了一下 [并测试了一下 :p ]


10

这个问题可以用许多方法来解决。我将展示两种方式:

  1. 使用to_string(i)函数将数字转换为字符串。

  2. 使用字符串流。

    代码:

#include <string>
#include <sstream>
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main() {
    string name = "John";
    int age = 21;

    string answer1 = "";
    // Method 1). string s1 = to_string(age).

    string s1=to_string(age); // Know the integer get converted into string
    // where as we know that concatenation can easily be done using '+' in C++

    answer1 = name + s1;

    cout << answer1 << endl;

    // Method 2). Using string streams

    ostringstream s2;

    s2 << age;

    string s3 = s2.str(); // The str() function will convert a number into a string

    string answer2 = "";  // For concatenation of strings.

    answer2 = name + s3;

    cout << answer2 << endl;

    return 0;
}

哪一个更快? - ghchoi

7
作为一行代码:name += std::to_string(age); 即可。

6

如果你想使用+来拼接任何具有输出运算符的内容,你可以提供一个模板版本的operator+

template <typename L, typename R> std::string operator+(L left, R right) {
  std::ostringstream os;
  os << left << right;
  return os.str();
}

然后您可以直接编写串联方式:
std::string foo("the answer is ");
int i = 42;
std::string bar(foo + i);    
std::cout << bar << std::endl;

输出:

the answer is 42

这不是最有效的方法,但除非你在循环中做很多拼接操作,否则不需要最有效的方法。


2
如果我尝试将两个整数或一个整数和一个双精度浮点数相加,这个函数会被调用吗?我想知道这个解决方案是否会覆盖通常的加法... - Hilder Vitor Lima Pereira
1
该运算符返回一个std::string,因此在字符串不能转换为所需类型的表达式中,它不会是候选项。例如,这个operator+不能用于int x = 5 + 7;中的+。总之,我不会定义这样一个运算符,除非有非常强烈的理由,但我的目标是提供一个与其他答案不同的答案。 - uckelman
你是对的(我刚刚测试过了...)。当我尝试做类似于string s = 5 + 7这样的事情时,我得到了错误invalid conversion from ‘int’ to ‘const char’*。 - Hilder Vitor Lima Pereira
对于大多数用例,使用将左操作数或右操作数绑定到字符串(包括std::stringstd::string_viewconst char *)的模板应该足够。 - Kai Petzke

5
如果您正在使用MFC,您可以使用一个CString。
CString nameAge = "";
nameAge.Format("%s%d", "John", 21);

托管 C++ 也有一个 字符串格式化器


4
作为一个与Qt相关的问题已经被关闭,因此在这里介绍如何使用Qt来完成它:
QString string = QString("Some string %1 with an int somewhere").arg(someIntVariable);
string.append(someOtherIntVariable);

现在,字符串变量中%1的值已被someIntVariable的值替换,并且在末尾添加了someOtherIntVariable的值。


QString("Something ") + QString::number(someIntVariable) 也可以工作。 - gremwell

4

std::ostringstream是一个很好的方法,但有时候这个额外的技巧可能会变得方便,将格式转换为一行:

#include <sstream>
#define MAKE_STRING(tokens) /****************/ \
    static_cast<std::ostringstream&>(          \
        std::ostringstream().flush() << tokens \
    ).str()                                    \
    /**/

现在您可以像这样格式化字符串:
int main() {
    int i = 123;
    std::string message = MAKE_STRING("i = " << i);
    std::cout << message << std::endl; // prints: "i = 123"
}

为什么要使用预处理器#define而不是template?使用可变参数模板,甚至可以传递多个标记。但我不确定是否要使用它,因为在<<操作数的输出上进行static_cast回到std::ostringstream有点不安全。按照惯例,所有输出器都返回对原始流对象的引用,但这在标准中并没有得到保证。 - Kai Petzke
您正在评论一个12年前的答案。现在我们有可变参数模板了。我可能会选择 https://github.com/fmtlib/fmt。 - Pyry Jahkola

3
  • std::ostringstream
#include <sstream>

std::ostringstream s;
s << "John " << age;
std::string query(s.str());
  • std::to_string (C++11)
std::string query("John " + std::to_string(age));
  • boost::lexical_cast
#include <boost/lexical_cast.hpp>

std::string query("John " + boost::lexical_cast<std::string>(age));

哪一个是最快的? - ghchoi

3

有更多的选项可以用来将整数(或其他数字对象)与字符串连接起来。这是Boost.Format

#include <boost/format.hpp>
#include <string>
int main()
{
    using boost::format;

    int age = 22;
    std::string str_age = str(format("age is %1%") % age);
}

来自Boost.Spirit(v2)的Karma

#include <boost/spirit/include/karma.hpp>
#include <iterator>
#include <string>
int main()
{
    using namespace boost::spirit;

    int age = 22;
    std::string str_age("age is ");
    std::back_insert_iterator<std::string> sink(str_age);
    karma::generate(sink, int_, age);

    return 0;
}

Boost.Spirit Karma声称是整数转字符串最快的选项之一

2
您可以使用下面简单的技巧将整数连接到字符串中,但请注意,仅当整数为个位数时才适用。否则,请将整数逐位添加到该字符串中。
string name = "John";
int age = 5;
char temp = 5 + '0';
name = name + temp;
cout << name << endl;

Output:  John5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接