【问题标题】:Iterate over vector of pair迭代向量对
【发布时间】:2014-10-25 16:45:59
【问题描述】:

我已经编写了以下代码 sn-p 但它似乎不起作用。

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

谁能帮帮我?

【问题讨论】:

  • 不应该是it != edges.end()吗?我在任何地方都没有看到 itt 声明
  • 如果您在 for 循环中的尖括号之间添加一个空格,它是否有效:vector &lt; pair&lt;float,pair&lt;int,int&gt; &gt; &gt;::const_iterator?
  • @EdChum:直角括号问题自 C++11 起已解决
  • @PiotrS。取决于编译器,这就是为什么它是一个狂野的平底船
  • @EdChum:不,每个 C++11 编译器都必须正确处理 &gt;&gt;

标签: c++ vector stl iterator const-iterator


【解决方案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

【讨论】:

    【解决方案2】:

    循环中至少有三个错误。

    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;
    }
    

    【讨论】:

    • 我很困惑何时使用-&gt;,何时使用.。你能帮忙吗?
    • @Peeyush 像指针一样的迭代器也是特定类型的迭代器)。例如,当您有一个指向 std::pair 对象的指针时,您可以使用 p->first,p->second。使用迭代器的方式相同。
    • 或者只是for ( const auto&amp; edge : edges )
    • @VladfromMoscow 下面的代码对我有用,但如果我理解正确的话,它不应该按照你的意思? (这个问题是否已经在 c++17 中修复了?)Graph::Graph(vector&lt;pair&lt;int, int&gt;&gt;&amp; edges, int nVert) { adjList.resize(nVert); for(auto i: edges) { adjList[i.first].push_back(i.second); adjList[i.second].push_back(i.first); } @AntonSavin 会自动给它一些其他类型的迭代器,而不是手动分配的 vector &lt; pair&lt;float,pair&lt;int,int&gt;&gt; &gt;::const_iterator
    • @gh05t 在这里,您正在使用我的答案末尾显示的基于范围的 for 循环。
    【解决方案3】:
    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 使用 lambda 或仿函数作为 std::sort 的第三个参数,您可以选择对所需的对元素执行排序。搜索SO,有几个例子。如果真的迷路了,请提出一个新问题
    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 2020-01-03
    • 2016-11-09
    • 2011-11-15
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多