如何在cpp中使用int迭代器打印数组/列表的向量?

时间:2018-01-27 18:04:03

标签: c++

我想声明一个500 * 500大小的二维数组(例如)。

数组矢量是最好的方法吗?

我似乎无法打印数组,并且每个其他代码都声明了vector类型的迭代器,但我想使用int类型来实现它。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<list>
#include<stack>
#define white 0
#define black 1
using namespace std;

void print(vector< list<int> > adjList)
{
    for(int i = 0; i<adjList.size(); i++)
    {
        for(int j = adjList[i].begin(); j != adjList[i].end(); j++)
        {
            cout<<j<<" "; //adjList[i][j] doesnt work

        }
        cout<<endl;
    }
}

int main()
{
        int n,m;
        cin>>n>>m;
        vector< list<int> > adjList;

        for(int i = 0; i<m ; i++)
        {
            int u,v;
            cin>>u>>v;
            adjList[u].push_back(v);
            adjList[v].push_back(u);
        }

        print(adjList);


}

5 个答案:

答案 0 :(得分:1)

你不能遍历带有整数的列表,但你可以将j声明为迭代器(std::list<int>::iterator j = adjList[i].begin();)并使用指针中的星号来获取迭代器指向的元素像这样:cout << *j << ' ';。如果要打印数组矢量,就像通常打印2D数组一样。 std::list容器实现了一个链表,与存储连续内存中元素的容器(例如数组和std::vector)非常不同,这可能不是您想要的,因为它没有随机访问。

顺便说一下,如果你想将像vector这样的大容器传递给像print()这样不需要修改向量的函数,你应该使用constant reference而不是复制向量。在C ++ 11中有一个叫range based for loop的东西可以迭代像向量之类的东西,而不必担心像迭代器这样的东西,而std::set可能更适合实现邻接列表,因为它允许检查两个顶点是否相邻,具有对数复杂度。

最后,在您的输入循环中,当adjList[u]为空时,该元素尚不存在时使用了adjList。在输入n以创建空集后,您应该添加一行adjList.resize(n),或者在输入adjList之后只需声明n并使用n作为构造函数参数来表示用一堆空集初始化adjList的构造函数:vector<set<int>> adjList(n);

由于看起来你正在解决USACO问题,我建议你在进行任何处理之前将输入转换为零索引。

我的代码使用基于范围的循环:

#include <iostream>
#include <vector>
#include <set>

void print(const std::vector<std::set<int>> &adjList)//pass by const reference
{
    for(std::size_t i = 0; i < adjList.size(); i++)
    {
        std::cout << i << ": ";
        for(auto &j : adjList[i])//range based for loop to iterate through adjList[i] and use the reference j to refer to each element
        {
            std::cout << j << ' ';
        }
        std::cout << '\n';
    }
}

int main()
{
    int n, m;
    std::cin >> n >> m;
    std::vector<std::set<int>> adjList(n); //adjList starts with n empty sets
    for(int i = 0; i < m; i++)
    {
        int u, v;
        std::cin >> u >> v;
        adjList[u].insert(v);
        adjList[v].insert(u);
    }
    print(adjList);
}

答案 1 :(得分:0)

问题是你有一个列表向量,但是列表没有定义operator [],然后你尝试使用它来访问它的元素。

因为你说你想要一个500 x 500的固定数组,所以你不需要一个列表向量,因为列表不是固定的长度。

例如,尝试使用矢量矢量。

答案 2 :(得分:0)

您不能使用int来迭代列表 C ++中的List是使用双向链表实现的,其中它们使用指针来访问下一个/前一个元素 我建议您使用的类型是list<int>::iterator,因为它是迭代列表的标准方法 还有其他一些方法可以做到这一点。例如:

for(auto x : adjList[i]){
    cout << x << " "; // Do something
      // Only valid in C++11 or newer.
}

另一个例子:

for(auto j = adjList[i].begin(); j != adjList[i].end(); j++){
    cout << *j << " "; // Do something with j (iterator) or *j (value).
      // Only valid in C++11 or newer.
}

对于经典的C ++示例将是:

for(list<int>::iterator it = adjlist[i].begin(); it != adjlist[i].end(): it++){
    cout << *it << " ";
}

答案 3 :(得分:0)

请注意,operator[]不是数组,不支持使用list建立索引。您可以使用vector替换整个程序中print()的使用,而void print(vector< vector<int> > adjList) { for (int i = 0; i < adjList.size(); i++) { for (int j = 0; j < adjList[i].size(); j++) { cout << adjList[i][j] << ' '; } cout << endl; } } 看起来像

var now = 2018;

var Josh = {
    dateOfBirth: 1990,
    age: function () {
        return (now - this.dateOfBirth);
    }
}

console.log(Josh.age())

答案 4 :(得分:0)

您可以在打印功能中使用基于范围的循环,如下所示:

void print(vector< list<int> > adjList)
{   
    for(auto i : adjList)
    {
        for(auto j: i)
        {
           cout << j << " ";
        }
    }
}

但是在运行代码时会出现seg错误。下面的主要功能应该是自我解释

int main()
{
    int n,m;
    cin>>n>>m;
    vector< list<int> > adjList;

    cout << "entered value : n" << n <<" m : "<<m <<endl;
    for(int i = 0; i<m ; i++)
    {
        int u,v;
        cin>>u>>v;
        adjList.push_back({v});
        adjList.push_back({u});
    }
    print(adjList);
}