小编fql*_*qlx的帖子

为什么std::string 在添加整数时返回字符串的尾部

我知道您不能使用operator+将整数连接到 astd::string而不将其转换为 achar*std::string

但是为什么添加一个整数会返回字符串的尾部呢?

#include <iostream>
#include <string>
int main()
{
    std::string x;
    x = "hello world" + 3;
    std::cout << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

印刷: lo world

如果你改变: x = "hello world" + 8;

我们打印: rld

这背后的原因是什么?未定义的行为?

c++ string

2
推荐指数
1
解决办法
141
查看次数

标签 统计

c++ ×1

string ×1