在我的程序中有一个数组对声明如下:
pair <string, int> mostOftenPairs[10];
例如,当我想初始化它时:
for (short i = 0; i < 10; ++i)
mostOftenPairs[i] = make_pair(std::string(), 0);
CLion 强调了发生赋值的行,当我将鼠标悬停在它上面时,出现此错误:
'pair::operator=(type)' is deleted
然而,程序运行没有任何问题,但我的问题是是否有任何方法用于为对而不是'='赋值?是否可以在同一行中初始化对数组?我的意思是类似于这个:
pair <string, int> mostOftenPairs[10] = make_pair(std::string(), 0);
最佳答案
Is it possible to initialise the array of pairs in the same line? I mean something similar to this one:
如std::pair
constructor 的文档中所述,要默认初始化它们,您不需要做任何事情。
1) Default constructor. Value-initializes both elements of the pair, first and second.
(重点是我的)所以 std::pair
的默认构造函数将已经初始化(空字符串和 0 为 int),如果您不需要默认值,请使用 std::vector
:
std::vector<std::pair<std::string,int>> mostOftenPairs( 10, std::make_pair( std::string(), 1 ) );
关于c++ - 将赋值运算符与 make_pair 方法一起使用会生成 CLion 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50235249/