迭代遍历pair向量

12

我写了以下代码片段,但似乎没有起作用。

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循环的行处出现了错误。错误是:

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> >*]

有人能帮我吗?


1
这应该是 it != edges.end(),我没有看到任何地方声明了 itt - EdChum
如果在for循环中的尖括号之间添加一个空格,会不会起作用呢?例如:vector < pair<float,pair<int,int> > >::const_iterator - EdChum
@PiotrS。这取决于编译器,因此这是一个不确定的猜测。 - EdChum
@EdChum:不,每个C++11编译器都必须正确处理>> - Piotr Skotnicki
您应该发布真实的代码。上面的代码会“抛出”更多的错误。 - Jonathan Potter
显示剩余2条评论
4个回答

16

C++14 迭代器更加简单。

for (const auto& pair : edges)
{
    std::cout << pair.first;
}

C++17 迭代器允许更深层次的访问

for (const auto& [first, sec] : edges)
{
    std::cout << first;
}

16

循环中至少有三个错误。

for ( vector < pair<float,pair<int,int>> >::const_iterator it = edges.begin() ; itt != edges.end; it++){
        cout >> it.first;
    }
首先,您必须使用 edges.end() 而不是 edges.end。并且在函数体内部必须有:
    cout << it->first;

代替

    cout >> it.first;
为避免此类错误,您可以简单地编写:
for ( const pair<float, pair<int,int> > &edge : edges )
{
   std::cout << edge.first;
}

我很困惑何时使用 -> 和何时使用 .,能否帮忙解答一下? - Peeyush
1
@Peeyush 迭代器就像指针一样,指针也是特殊类型的迭代器。例如,当你有一个指向std::pair对象的指针时,你可以使用p->first、p->second。迭代器的使用方式也是相同的。 - Vlad from Moscow
2
或者只需使用 for ( const auto& edge : edges ) - Anton Savin
@VladfromMoscow 以下代码对我有效,但根据您的理解,它不应该有效,是吗?(这个问题在C++17中有没有被修复呢?) adjList.resize(nVert); for(auto i: edges) { adjList[i.first].push_back(i.second); adjList[i.second].push_back(i.first); }@AntonSavin 是否会自动提供一些其他类型的迭代器,而不是手动分配的vector < pair<float,pair<int,int>> >::const_iterator - Xgh05t
@gh05t,你在使用我回答末尾展示的基于范围的for循环。 - Vlad from Moscow
哦,对了,它会在内部解引用迭代器,所以你就不必手动操作了,明白了,谢谢!(大脑短路瞬间哈哈) - Xgh05t

4
for ( vector < pair<float,pair<int,int>> >::const_iterator it = edges.begin() ; 

     it != edges.end () ;  // Use (), and assuming itt was a typo
     it++)
{
    cout << it->first; // Use -> 
}

此外,您可能想为std::sort添加自定义比较器。

我应该如何添加自定义排序?使用结构体或类而不是制作一个包含浮点数和一对的向量(其中包含一对)更好吗? - Peeyush
1
@Peeyush 在 std::sort 的第三个参数中使用 lambda 表达式或函数对象(functor),可以选择对所需的 pair 元素进行排序。在 SO 上进行搜索,有多个示例。如果实在不知道可以提一个新问题。 - P0W

1
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接