ChatGPT解决这个技术问题 Extra ChatGPT

如何连接 std::string 和 int

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

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

如何组合它们以获得单个字符串 "John21"

Herb Sutter 有一篇关于这个主题的好文章:"The String Formatters of Manor Farm"。他涵盖了 Boost::lexical_caststd::stringstreamstd::strstream(已弃用)和 sprintfsnprintf
让我补充一点:我试过 'str = "hi"; str += 5; cout << str;'并没有看到任何效果。结果这调用了 operator+=(char) 并添加了一个不可打印的字符。

O
Okkenator

按字母顺序:

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 the {fmt} library
result = fmt::format("{}{}", name, age);

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

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

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

// 9. 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);

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

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

是安全的,但很慢;需要 Boost(仅标题);大多数/所有平台都是安全的,需要 C++11(to_string() 已包含在 #include 中)是安全且快速的;需要 FastFormat,必须编译;大多数/所有平台(同上)既安全又快速;需要 {fmt} 库,可以编译或以仅头文件模式使用;大多数/所有平台安全、缓慢且冗长;需要#include (来自标准C++)是脆弱的(你必须提供足够大的缓冲区)、快速和冗长; itoa() 是一个非标准的扩展,不能保证对所有平台都可用。它很脆弱(你必须提供足够大的缓冲区)、快速和冗长;什么都不需要(是标准的 C++);所有平台都很脆弱(您必须提供足够大的缓冲区),可能是最快的转换,冗长;需要 STLSoft(仅标题);大多数/所有平台都是安全的(您不会在单个语句中使用多个 int_to_string() 调用),快速;需要 STLSoft(仅标题);仅限 Windows 是安全的,但速度很慢;需要 Poco C++ ;大多数/所有平台


除了你已经建立的一个链接之外,你的表现评论是基于什么的?
这几乎是您从一个答案中获得的全部声誉!你幸运的 bean ;) 我认为 8 是标准 C(当然也是 C++),但可能值得区分。
2. 很慢,因为 std::to_string(age) 创建了一个附加到结果的临时字符串。
如果您使用的是 Arduino,也可以使用 String(number)
我也会添加 snprintf,也许是 sprintf 的 instread。
A
Azeem

在 C++11 中,您可以使用 std::to_string,例如:

auto result = name + std::to_string( age );

和之前一样。
J
Jay Conrod

如果您有 Boost,则可以使用 boost::lexical_cast<std::string>(age) 将整数转换为字符串。

另一种方法是使用字符串流:

std::stringstream ss;
ss << age;
std::cout << name << ss.str() << std::endl;

第三种方法是使用 C 库中的 sprintfsnprintf

char buffer[128];
snprintf(buffer, sizeof(buffer), "%s%d", name.c_str(), age);
std::cout << buffer << std::endl;

其他海报建议使用 itoa。这不是标准功能,因此如果您使用它,您的代码将无法移植。有些编译器不支持它。


请注意,不保证 snprintf 以空值终止字符串。这是确保它有效的一种方法:
 char buffer[128];缓冲区[sizeof(buffer)-1] = '\0'; snprintf(buffer, sizeof(buffer)-1, "%s%d", name.c_str(), age); std::cout << 缓冲区 << std::endl; 
我的倾向是永远不要使用 sprintf,因为这会导致缓冲区溢出。上面的示例是一个很好的示例,如果名称很长,则使用 sprintf 将是不安全的。
请注意, snprintf 同样是非标准的 c++(如您提到的 itoa)。取自 c99
@terson:我在答案中没有看到 sprintf,只有 snprintf
J
Jean-François Fabre
#include <iostream>
#include <sstream>

std::ostringstream o;
o << name << age;
std::cout << o.str();

太好了,BYT 头文件是 sstream
b
bluish
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string itos(int i) // convert int to string
{
    stringstream s;
    s << i;
    return s.str();
}

无耻地从 http://www.research.att.com/~bs/bs_faq2.html 窃取。


s 是堆栈变量,调用 itoss 的内存将是空闲的。 s 应该从堆中分配,而 free 应该在使用后分配,对吗?
即使字符串对象超出范围,按值返回也可以,stackoverflow.com/a/3977119/5393174
链接已损坏:“找不到页面”
K
Kevin

这是最简单的方法:

string s = name + std::to_string(age);

这是 C++11 后的解决方案!
A
Azeem

如果您有 C++11,则可以使用 std::to_string

例子:

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

name += std::to_string(age);

std::cout << name;

输出:

John21

如您所见,在 VC++ 2010 中为 name += std::to_string(static_cast<long long>(age)); here
@neonmate name += std::to_string(age + 0LL); 怎么样?
很好的解决方案。谢谢
b
bluish

在我看来,最简单的答案是使用 sprintf 函数:

sprintf(outString,"%s%d",name,age);

snprintf 可能很棘手(主要是因为它在某些情况下可能不包含空字符),但我更喜欢这样做以避免 sprintf 缓冲区溢出潜在问题。
当您将 std::string 传递给 %s 时,sprintf(char*, const char*, ...) 在某些版本的编译器上会失败。但并非全部(这是未定义的行为),它可能取决于字符串长度(SSO)。请使用 .c_str()
加上 sprintf 会受到缓冲区溢出的影响,因此可能会注入代码
S
Seb Rose
#include <string>
#include <sstream>
using namespace std;
string concatenate(std::string const& name, int i)
{
    stringstream s;
    s << name << i;
    return s.str();
}

Z
Zing-
#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;

Googled [并经过测试:p]


F
Francesco Boi

这个问题可以通过多种方式解决。我将以两种方式展示它:

使用 to_string(i) 将数字转换为字符串。使用字符串流。代码:#include #include #include #include using namespace std; int main() { string name = "John";整数年龄 = 21;字符串答案 1 = ""; // 方法 1)。字符串 s1 = to_string(年龄)。字符串 s1=to_string(年龄); // 知道整数被转换成字符串 // 我们知道在 C++ 中使用 '+' 可以很容易地完成连接 answer1 = name + s1; cout << answer1 << endl; // 方法 2)。使用字符串流 ostringstream s2; s2 << 年龄;字符串 s3 = s2.str(); // str() 函数会将数字转换为字符串 string answer2 = ""; // 用于字符串的连接。答案 2 = 名称 + s3; cout << answer2 << endl;返回0; }


哪个更快?
v
vitaut

在 C++20 中,您将能够:

auto result = std::format("{}{}", name, age);

同时您可以使用 the {fmt} librarystd::format 是基于:

auto result = fmt::format("{}{}", name, age);

免责声明:我是 {fmt} 库和 C++20 std::format 的作者。


嗨@vitaut,fmt::format 支持动态输入字符串吗?例如:fmt::format(variable,name,age)
确实如此。您需要将动态格式字符串包装在 fmt 8.x 中的 fmt::runtime
嗨@vitaut,如何使代码与数字一起工作:store.push_back(fmt::arg("1", "pi"));,这也会引发异常fmt::vformat("{1} = {2}",store)
嗨@vitaut,我可以将 fmt::format 与 SYSTEMTIME t;? fmt::format("{}",t); 一起使用吗?请给我一个例子。感谢您!
u
uckelman

如果您想使用 + 连接任何具有输出运算符的内容,您可以提供 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

这不是最有效的方法,但除非您在循环中进行大量连接,否则您不需要最有效的方法。


如果我尝试添加整数或整数和双精度,是否会调用此函数?我想知道这个解决方案是否会覆盖通常的添加......
该运算符返回一个 std::string,因此不会成为字符串不可转换为所需类型的表达式中的候选者。例如,此 operator+ 不适合用于 int x = 5 + 7; 中的 +。考虑到所有因素,如果没有非常令人信服的理由,我不会定义这样的运算符,但我的目标是提供与其他人不同的答案。
你是对的(我刚刚测试过......)。当我尝试执行类似字符串 s = 5 + 7 之类的操作时,我收到了从 'int' 到 'const char'* 的错误无效转换
对于大多数用例,使用将左侧或右侧操作数绑定到字符串(包括 std::stringstd::string_viewconst char *)的模板就足够了。
V
Vadim Kotov

如果您使用的是 MFC,则可以使用 CString

CString nameAge = "";
nameAge.Format("%s%d", "John", 21);

托管 C++ 也有一个 string formatter


G
Greg

作为一个班轮:name += std::to_string(age);


P
Pyry Jahkola

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 有点不安全。这是一个约定,所有输出器都返回一个指向原始流对象的引用,但这在标准中没有得到保证。
您正在评论一个 12 岁的答案。我们现在有可变参数模板。我现在可能会选择 github.com/fmtlib/fmt
l
leinir

由于一个与 Qt 相关的问题已被关闭,有利于这个问题,这里是如何使用 Qt 来做到这一点:

QString string = QString("Some string %1 with an int somewhere").arg(someIntVariable);
string.append(someOtherIntVariable);

字符串变量现在有 someIntVariable 的值代替 %1,最后有 someOtherIntVariable 的值。


QString("Something") + QString::number(someIntVariable) 也可以
m
mloskot

有更多选项可用于将整数(或其他数字对象)与字符串连接起来。它是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 声称是fastest option for integer to string转换之一。


I
Isma Rekathakusuma

std::ostringstream

#include std::ostringstream s; s << "约翰" << 年龄; std::string 查询(s.str());

std::to_string (C++11)

std::string 查询(“约翰” + std::to_string(age));

boost::lexical_cast

#include std::string query("John " + boost::lexical_cast(age));


哪个最快?
D
David G

下面是如何使用 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
}

A
Azeem

常见答案:itoa()

这是不好的。正如 here 所指出的,itoa 是非标准的。


P
Peter Mortensen

您可以使用下面给出的简单技巧将 int 连接到字符串,但请注意,这只适用于整数为单个数字的情况。否则,将整数逐位添加到该字符串。

string name = "John";
int age = 5;
char temp = 5 + '0';
name = name + temp;
cout << name << endl;

Output:  John5

P
Peter Mortensen

我写了一个函数,它以 int 数字为参数,并将其转换为字符串文字。此函数依赖于将单个数字转换为其等效字符的另一个函数:

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;
}

P
PeterSom

在 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() 保证 ostringstream 对象的字符串缓冲区在下次调用 lambda 时为空。


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