c++ - 对值读取错误 C++

标签 c++ dictionary compiler-errors std-pair

我收到编译器错误:

error: request for member 'first' in 'list.std::map::operator[], std::less, std::allocator > > >((* &0.0)).std::pair::second', which is of non-class type 'int' int closestX = list[0].second.first;

当尝试从一个名为列表定义的映射中读取数据并使用迭代器时:

map<double, pair<int, int>> list;
map<double, pair<int, int>>::iterator it = list.begin();

列表中的成员插入:

list.insert(it, pair<double, pair<int, int>>(findDistance(posr,posc,j, i), pair<int, int>(j, i)));

我正在使用以下方法从 map 中读取值:

int closestX = list[0].second.first;
int closestY = list[0].second.second;

错误似乎表明 list[0].second.first 的返回类型是非类类型 int,但返回类型与 closestX 的值类型完全匹配,我有现在碰壁了。假设 list[0] 已初始化并包含一个有效值。

最佳答案

list[0]已经是值类型 pair<int, int> ,不是迭代器类型。所以你可以写

int closestX = list[0].first;
int closestY = list[0].second;

关于c++ - 对值读取错误 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330098/

相关文章:

c - 编译C代码的问题

android - 由于 'build' 文件夹问题,适用于 Android 的 React Native 编译在 Windows 上失败

c++ - 读取一行输入,包括以空格分隔的字符串,并使用 getline 将它们显示为一个完整的行

c++ - ^ 在 bool GetDeviceInformation(String ^ port, LibCECConfiguration ^configuration, uint32_t timeoutMs) 中是什么意思

javascript:使用函数方法通过数组的多个值过滤对象

ios - 使用谓词 iOS SDK 从字典数组中过滤数据

c++ - 在C++中与libmongoclient链接时出错

c++ - 关闭一个对话框并打开另一个对话框

c++ - 编写默认构造函数强制零初始化?

c# - 字典的 IEnumerator 是否保证一致?