【问题标题】:How to iterate using vector of vector of pair in c++?如何在c ++中使用对向量的向量进行迭代?
【发布时间】:2020-05-25 09:31:53
【问题描述】:

我想问一下你如何迭代向量对的向量?假设我有以下内容。

typedef pair<int,int> Pair;
vector<vector<Pair> > adjList;

然后我尝试使用以下代码进行迭代:

vector<vector<Pair> > :: iterator i;
vector<Pair> :: iterator it;
for(i = adjList[N].begin(); i != adjList[N].end(); ++i)
{
   for(it = adjList[N].begin(); it != adjList[N].end(); ++it)
   //and the rest of the code

但是它返回一个错误

'no match for ‘operator=’ (operand types are ‘std::vector > >::iterator {aka __gnu_cxx::__normal_iterator >*, std::vector > > >}’ and ‘std::vector >::iterator {aka __gnu_cxx::__normal_iterator*, std::vector > >}’)'. 

有人知道吗?谢谢。

【问题讨论】:

  • N 在这段代码中代表什么??
  • 为什么你认为adjList[N].begin() 有两种不同的类型?

标签: c++ vector iterator


【解决方案1】:

您可以使用基于范围的 for 循环和结构化绑定:

for(auto& inner : adjList) {
    for(auto& [first, second] : inner) {
        first = ...;
        second = ...;
    }
}

【讨论】:

  • @FantasticMrFox 在工作中卡住了旧版本,是吗?我知道这种感觉。 :-)
  • 是的,但希望不会太久。 if constexpr 将改变我的生活...
  • @FantasticMrFox 确实! ...和结构化的绑定也很不错。
【解决方案2】:

您的问题是您在此处使用第一个元素,即外部列表的迭代器:

for(i = adjList[N].begin(); i != adjList[N].end(); ++i)

您明确表示i 是外部列表的迭代器。您可能打算这样做:

for(i = adjList.begin(); i != adjList.end(); ++i)

但为了方便和可读性,请使用基于范围的 for 循环:

for (const auto& row : adjList) {
    for (const auto& pair : row) {
       // print pair.second or whatever

如果要编辑这对,请删除 2 consts。

【讨论】:

  • for 循环内的数组是否也必须是二维数组?例如,intvisited[i]it].
【解决方案3】:

vector<vector<Pair> > :: iterator i;
vector<Pair> :: iterator it;
for(i = adjList.begin(); i != adjList.end(); ++i)
{
   for(it =(*i).begin(); it != (*i).end(); ++it){}
}

如果您的项目使用 C++11 或更高版本,请使用 auto 简化 typedef

for(auto i = adjList.begin(); i != adjList.end(); i++){
   for(auto itr = (*i).begin(); itr != (*i).end(); itr++){
  }
}

此外,对于范围:

for(auto & i:adjList){
  for(auto &j: i){
    // xxxx
  }
}

【讨论】:

    【解决方案4】:
    for(i = adjList[N].begin(); i != adjList[N].end(); ++i)
    

    这迭代了第n个内部向量而不是i旁边的外部向量类型与adjList[N].begin()的类型不一致,N是什么。

    #include <utility>
    #include <vector>
    
    
    typedef std::pair<int,int> Pair;
    int main() {
        std::vector<std::vector<Pair> > adjList;
    
        std::vector<std::vector<Pair> >::iterator i;
        std::vector<Pair>::iterator it;
        for (i = adjList.begin(); i != adjList.end(); ++i)//iterating over the outer vector
        {
            for (it = i -> begin(); it != i -> end(); ++it)//over the inner ones
            {
                it -> first = ...;//assign to the first of the pair
                it -> second = ...;//assign to the second of the pair
            }
        }
    
    }
    

    【讨论】:

    • 我试图只遍历输出(在我的例子中,我正在实现一个图表)。我已经添加了边和顶点,现在我正在尝试标记访问的边。是否需要再次分配该对?
    • @qwertyuiop 不,没有必要。我这样做只是为了向您展示如何到达对的元素。您可以使用std::cout &lt;&lt; it -&gt; first 来显示输出而不是重新分配。
    • @qwertyuiop 我用这种方式告诉你错误在哪里,但如果我是你,我会使用 TedLyngmo 的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多