jlc*_*lin 5 c++ string operators c++11
为什么operator+=定义std::string但operator+未定义?请参阅下面的我的MWE(http://ideone.com/OWQsJk).
#include <iostream>
#include <string>
using namespace std;
int main() {
string first;
first = "Day";
first += "number";
cout << "\nfirst = " << first << endl;
string second;
//second = "abc" + "def"; // This won't compile
cout << "\nsecond = " << second << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您需要将其中一个原始字符串文字转换为std::string显式.你可以像其他已经提到过的那样做:
second = std::string("abc") + "def";
Run Code Online (Sandbox Code Playgroud)
或者使用C++ 14,您将能够使用
using namespace std::literals;
second = "abc"s + "def";
// note ^
Run Code Online (Sandbox Code Playgroud)
那些不是std::strings,它们是const char *.试试这个:
second = std::string("abc") + "def";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
516 次 |
| 最近记录: |