如何将一个 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个回答

2

常见答案:itoa()

这是不好的。正如这里指出的那样,itoa是非标准的。


2
itoa是非标准函数:https://dev59.com/RHVC5IYBdhLWcg3wxEJ1 - David Dibben

2

这是一个使用IOStreams库中的解析和格式化组件将int附加到字符串的实现示例。

#include <iostream>
#include <locale>
#include <string>

template <class Facet>
struct erasable_facet : Facet
{
    erasable_facet() : Facet(1) { }
    ~erasable_facet() { }
};

void append_int(std::string& s, int n)
{
    erasable_facet<std::num_put<char,
                                std::back_insert_iterator<std::string>>> facet;
    std::ios str(nullptr);

    facet.put(std::back_inserter(s), str,
                                     str.fill(), static_cast<unsigned long>(n));
}

int main()
{
    std::string str = "ID: ";
    int id = 123;

    append_int(str, id);

    std::cout << str; // ID: 123
}

1
在C++20中,您可以使用可变参数lambda将任意可串流类型连接到字符串中,只需几行代码:
auto make_string=[os=std::ostringstream{}](auto&& ...p) mutable 
{ 
  (os << ... << std::forward<decltype(p)>(p) ); 
  return std::move(os).str();
};

int main() {
std::cout << make_string("Hello world: ",4,2, " is ", 42.0);
}

看到 https://godbolt.org/z/dEe9h75eb

使用move(os).str()可以保证在下一次调用lambda时,ostringstream对象的stringbuffer为空。


请注意,与此处提供的其他解决方案不同,此lambda表达式不是线程安全/可重入的。这可以通过将ostringstream生命周期移动到lambda主体中来解决,我认为在捕获列表中保持其初始化没有任何好处(由于移动,无论哪种方式都不会有任何内存重用)。 - Sean Middleditch
在 c++20 中,您也可以使用 https://en.cppreference.com/w/cpp/utility/format/format,可惜的是 libstdc++ https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html 尚未实现。 Clang 的 libc++ 有(目前在主干中),而您始终可以回退到 fmtlib https://github.com/fmtlib/fmt。 - sqrtroot

1

我写了一个函数,它以整数作为参数,并将其转换为字符串字面量。这个函数依赖于另一个函数,该函数将单个数字转换为其char等效项:

char intToChar(int num)
{
    if (num < 10 && num >= 0)
    {
        return num + 48;
        //48 is the number that we add to an integer number to have its character equivalent (see the unsigned ASCII table)
    }
    else
    {
        return '*';
    }
}

string intToString(int num)
{
    int digits = 0, process, single;
    string numString;
    process = num;

    // The following process the number of digits in num
    while (process != 0)
    {
        single  = process % 10; // 'single' now holds the rightmost portion of the int
        process = (process - single)/10;
        // Take out the rightmost number of the int (it's a zero in this portion of the int), then divide it by 10
        // The above combination eliminates the rightmost portion of the int
        digits ++;
    }

    process = num;

    // Fill the numString with '*' times digits
    for (int i = 0; i < digits; i++)
    {
        numString += '*';
    }


    for (int i = digits-1; i >= 0; i--)
    {
        single = process % 10;
        numString[i] = intToChar ( single);
        process = (process - single) / 10;
    }

    return numString;
}

0
你可以像这样使用 C 函数 itoa()
    char buf[3];
    itoa(age, buf, 10);
    name += buf;

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