Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

c++ - how do i print a vector of arrays/lists using int iterator in cpp?

I want to declare a 2d array of 500 * 500 size(for example).

Is a vector of arrays the best way to go?

I can't seem to print the array and every other code declares the iterator of vector type but i want to do it using int type.

#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);


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't iterate through a list with an integer, but you can declare j as an iterator (std::list<int>::iterator j = adjList[i].begin();) and use the asterisk like in pointers to get the element that an iterator is pointing at like this: cout << *j << ' ';. If you want to print a vector of arrays, just do it like how you would normally print a 2D array. The std::list container implements a linked list and is very different from containers that store elements in contiguous memory such as arrays and std::vector, which is likely not what you want here since it doesn't have random access.

By the way, if you want to pass large containers like vectors to a function like print() that does not need to modify the vector, you should use a constant reference instead of copying the vector. There is something called a range based for loop in C++11 that can iterate through things like vectors without you having to worry about things like iterators, and a std::set is probably more suited for implementing an adjacency list since it allows for checking if two vertices are adjacent with logarithmic complexity.

Lastly, in your input loop, you used adjList[u] when that element does not exist yet since adjList is empty. You should add a line adjList.resize(n) after you input n to create the empty sets or just declare adjList after you input n and use n as the constructor argument like this to tell the constructor to initialize adjList with a bunch of empty sets: vector<set<int>> adjList(n);.

Since it looks like you are doing a USACO problem, I would advise you to convert the input to zero indexing before doing any processing.

My code using range based for loops:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...