迭代对的向量 [英] Iterate over vector of pair
本文介绍了迭代对的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了以下代码片段,但它似乎不起作用.
I have written following code snippet but it does not seem to be working.
int main(){
int VCount, v1, v2;
pair<float, pair<int,int> > edge;
vector< pair<float, pair<int,int> > > edges;
float w;
cin >> VCount;
while( cin >> v1 ){
cin >> v2 >> w;
edge.first = w;
edge.second.first = v1;
edge.second.second = v2;
edges.push_back(edge);
}
sort(edges.begin(), edges.end());
for ( vector < pair<float,pair<int,int>> >::const_iterator it = edges.begin() ; itt != edges.end; it++){
cout >> it.first;
}
return 0;
}
它在包含 for 循环的行上抛出错误.错误是:
It is throwing an error at the line containing for loop. The error is:
error: no match for ‘operator<’ in ‘it < edges.std::vector<_Tp, _Alloc>::end [with _Tp = std::pair<float, std::pair<int, int> >, _Alloc = std::allocator<std::pair<float, std::pair<int, int> > >, std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const std::pair<float, std::pair<int, int> >*, std::vector<std::pair<float, std::pair<int, int> > > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::const_pointer = const std::pair<float, std::pair<int, int> >*]
有人可以帮我吗?
推荐答案
循环中至少有三个错误.
There are at least three errors in the loop.
for ( vector < pair<float,pair<int,int>> >::const_iterator it = edges.begin() ; itt != edges.end; it++){
cout >> it.first;
}
首先你必须使用 edges.end()
而不是 edges.end
.而身体内部必须有
First of all you have to use edges.end()
instead of edges.end
. And inside the body there has to be
cout << it->first;
代替
cout >> it.first;
为了避免此类错误,您可以简单地编写
To escape such errors you could write simply
for ( const pair<float, pair<int,int> > &edge : edges )
{
std::cout << edge.first;
}
这篇关于迭代对的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文