c++ - 将赋值运算符与 make_pair 方法一起使用会生成 CLion 警告

标签 c++ clion

在我的程序中有一个数组对声明如下:

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/

相关文章:

c# - 导出 C++ DLL 函数并导入 C#

c++ - 在没有物理连接的情况下写入时,boost async tcp 客户端不会返回错误

c++ - 在多个数据 block 中搜索多个单词

c++ - CUDA C++类被误认为是模板

clion - 在 Clion 中更改 CMake 语法颜色

C : Value escapes local scope?

opengl - CLion GLSL 支持插件。添加文件关联

c++ - 如何在 Clion 和 CMake 中添加 allegro 库?

CMake:C11 的 CMakeLists

c++ - std::stringstream 从字符串中读取 int 和字符串