我知道您不能使用operator+
將整數連接到std::string
而不將其轉換爲char*
或std::string
。爲什麼std :: string在添加整數時返回字符串尾部
但是爲什麼添加一個整數會返回一個字符串的尾部?
#include <iostream>
#include <string>
int main()
{
std::string x;
x = "hello world" + 3;
std::cout << x << std::endl;
}
打印:lo world
如果更改:x = "hello world" + 8;
我們打印:rld
什麼是這背後的原因何在呢?未定義的行爲?
ptr + int = other ptr – Borgleader
'「hello world」'不是'std :: string',它是一個'const char [12]'。 – molbdnilo
你可以少調查一下 - '「hello world」+ 0','「hello world」+ 1','「hello world」+ 2'等等。一種模式很快就會出現。 – molbdnilo