从对象的向量获取价值时出错 [英] Error in Getting Value from Vector of Pairs

查看:86
本文介绍了从对象的向量获取价值时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在对向量的迭代器中访问对的值时会出现以下错误?

Why do I get the error below when accessing the pair's values in a iterator of a vector of pairs?

vector< pair<int,string> > mapper;
if(Hash(input, chordSize) != id){
    mapper.push_back(make_pair(tmp, input));
}

for (vector< pair<int,string> >::iterator it = mapper.begin(); it != mapper.end(); ++it)
{
    cout << "1st: " << *it.first << " "           // <-- error!
         << "2nd: " << *it.second << endl;        // <-- error!
}

错误讯息:

main_v10.cpp:165:25:error:'std :: vector>> :: iterator'没有成员命名为'first'
main_v10.cpp:165:56:error :'std :: vector>> :: iterator'没有成员命名为'second'

main_v10.cpp:165:25: error: ‘std::vector > >::iterator’ has no member named ‘first’ main_v10.cpp:165:56: error: ‘std::vector > >::iterator’ has no member named ‘second’

p>

推荐答案

这也是一个适用于指针的问题(迭代器的行为非常像一个指针)。有两种方法来访问指针(或迭代器)指向的值的成员:

This is an issue which applies for pointers, too (an iterator behaves pretty much like a pointer). There are two ways to access a member of the value the pointer (or iterator) points to:

it->first     // preferred syntax: access member of the pointed-to object

(*it).first   // verbose syntax: dereference the pointer, access member on it

运算符优先级将您的表达式转换为

The operator precedence turns your expression into

*(it.first)   // wrong! tries to access a member of the pointer (iterator) itself

尝试访问成员第一在迭代器本身,它失败,因为它没有成员第一

which tries to access the member first on the iterator itself, which fail, because the it doesn't have a member called first. If it did, you'd then dereference the value of that member.

但是,在大多数情况下,您应该使用 std :: map 从键映射到值。而不是向量< pair< int,string> > ,你应该使用 map< int,string> ,它的行为类似(插入,迭代和东西也发生在对)数据结构中的键用于更快的随机访问:

However, in most such cases you should use std::map to map from key to values. Instead of vector<pair<int,string> >, you should use map<int,string> which behaves similar (insertion, iteration and stuff also happens with pairs), but it sorts the keys in the data structure for faster random access:

map<int,string> mapper;
if(Hash(input, chordSize) != id){
    mapper.push_back(make_pair(tmp, input));
}

for (map<int,string>::iterator it = mapper.begin(); it != mapper.end(); ++it)
{
    cout << "1st: " << it->first << " "
         << "2nd: " << it->second << endl;
}

请注意,地图和对的向量之间的本质区别是: map通过按键对它们进行排序来重新排列元素。以后无法查询插入顺序。在某些情况下,您不想这样做(当插入顺序很重要时),因此在这种情况下,您的解决方案或包含至少键和值的自定义类型的向量是正确的解决方案。

Note that an essential difference between a map and a vector of pairs is that a map rearranges the elements by sorting them by their key. The order of insertion can't be queried afterwards. There are cases in which you don't want to do that (when insertion order matters), so in such cases either your solution or a vector with custom types containing at least the key and value are the correct solution.

这篇关于从对象的向量获取价值时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆