相关疑难解决方法(0)

C++ - 使用std :: string的统一初始化程序

我正在尝试使用C++字符串类的统一初始化程序.以下是代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1 {"aaaaa"};
    string str2 {5, 'a'};
    string str3 (5, 'a');

    cout << "str1: " << str1 << endl;
    cout << "str2: " << str2 << endl;
    cout << "str3: " << str3 << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出将是:

str1: aaaaa
str2: a
str3: aaaaa
Run Code Online (Sandbox Code Playgroud)

这让我摸不着头脑.为什么str2不能达到预期的效果str3呢?

c++ string uniform-initialization c++11 list-initialization

9
推荐指数
2
解决办法
945
查看次数