C++ 迭代对的向量
我已经编写了以下代码片段,但它似乎不起作用C++ 迭代对的向量,c++,vector,stl,iterator,const-iterator,C++,Vector,Stl,Iterator,Const Iterator,我已经编写了以下代码片段,但它似乎不起作用 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 ){
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;
}
intmain(){
int VCount,v1,v2;
对边;
向量边;
浮动w;
cin>>VCount;
而(cin>>v1){
cin>>v2>>w;
第一个边缘=w;
edge.second.first=v1;
edge.second.second=v2;
边缘。推回(边缘);
}
排序(edges.begin()、edges.end());
对于(vector::const_迭代器it=edges.begin();itt!=edges.end;it++){
cout>>首先;
}
返回0;
}
它在包含for循环的行上抛出错误。错误是:
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> >*]
错误:与(vector::const_iterator it=edges.begin()的运算符不匹配;
it!=edges.end();//Use(),并假设itt是一个打字错误
it++)
{
cout first;//使用->
}
此外,您可能需要为std::sort
添加自定义比较器。循环中至少有三个错误
for ( vector < pair<float,pair<int,int>> >::const_iterator it = edges.begin() ; itt != edges.end; it++){
cout >> it.first;
}
而不是
cout >> it.first;
为了避免这些错误,你可以简单地写
for ( const pair<float, pair<int,int> > &edge : edges )
{
std::cout << edge.first;
}
for(常量对和边:边)
{
std::coutvectorv;//x>>y;
v、 向后推(形成一对(x,y));
}
排序(v.begin(),v.end(),比较);
对于(向量::迭代器p=v.begin();p!=v.end();p++){
那不是it!=edges.end()吗如果你在for循环中添加了一个角括号之间的空格:<代码>向量>配对>::CistaSythRealthAuth< /Cord>?EdCuM:直角括号问题是C++解决的,我看不出<代码> ITT < /CUT>。11@PiotrS.这取决于编译器这就是为什么它是一个狂野的punt@EdChum:否,每个C++11编译器st正确处理>
我应该如何添加自定义排序?使用结构或类是否比创建向量对(包含浮点和对)更好?Peeyus可以使用lambda或函子作为std::sort
的第三个参数,您可以选择对所需的成对元素执行排序。在搜索过程中,有几个示例。如果真的丢失了,请问一个新问题。我很困惑何时使用->
以及何时使用
。您能帮帮忙吗?@Peeyus迭代器如指针指针也是一种特殊类型的迭代器)。例如,当您有一个指向std::pair对象的指针时,您可以使用p->first,p->second。迭代器的使用方法与此相同。或者只对(const auto&edge:edges)使用
@VladfromMoscow以下代码对我有效,但如果我理解正确的话,你不应该这么认为?(这个问题可能在c++17中被修复了吗?Graph::Graph(vector&edges,int-nVert){adjList.resize(nVert);for(auto I:edges){adjList[I.first]。推回(I.second);adjList[i.second]。推回(i.first);}
@AntonSavin将自动为它提供一些其他类型的迭代器,而不是手动指定的向量::const_迭代器
?@gh05t这里您使用的是我答案末尾显示的基于范围的for循环。
cout >> it.first;
for ( const pair<float, pair<int,int> > &edge : edges )
{
std::cout << edge.first;
}
vector<pair<int,int>>v; // <-- for this one
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
v.push_back(make_pair(x,y));
}
sort(v.begin(),v.end(),compare);
for( vector<pair<int,int>>::iterator p=v.begin(); p!=v.end(); p++){
cout<<"Car "<<p->first<<" , "<<p->second<<endl;
}
cout<<endl;
// you can also write like this
for(auto it:v){
cout<<"Car "<<it.first<<" , "<<it.second<<endl;
}
cout<<endl;
// you can also write like this
for(pair<int,int> it2:v){
cout<<"Car "<<it2.first<<" , "<<it2.second<<endl;
}
// now you can code for your one by taking reference from this ok