小编Shi*_*hir的帖子

使用双花​​括号初始化vector <string>

有人能解释下面例子中用双花括号和单花括号初始化之间的行为差​​异吗?

代码#1:

vector<string> v = {"a", "b"};
string c(v[0] + v[1]);
cout << "c = " << c;
cout << "c.c_str() = " << c.c_str();
Run Code Online (Sandbox Code Playgroud)

输出#1:

c = ab
c.c_str() = ab
Run Code Online (Sandbox Code Playgroud)

代码#2:

vector<string> v = {{"a", "b"}};
string c(v[0] + v[1]);
cout << "c = " << c;
cout << "c.c_str() = " << c.c_str();
Run Code Online (Sandbox Code Playgroud)

输出#2:

c = a\acke?Z\ 
c.c_str() = a
Run Code Online (Sandbox Code Playgroud)

c++ constructor initialization-list

4
推荐指数
1
解决办法
3084
查看次数

标签 统计

c++ ×1

constructor ×1

initialization-list ×1