繁体   English   中英

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

[英]how do i print a vector of arrays/lists using int iterator in cpp?

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

arrays 的向量是 go 的最佳方法吗?

我似乎无法打印数组,并且所有其他代码都声明了向量类型的迭代器,但我想使用 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);


}

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

既然你说你想要一个 500 x 500 的固定数组,那么无论如何你都不想要一个列表向量,因为列表不是固定长度的。

例如,尝试一个向量的向量。

您不能使用int来遍历列表。
C++ 中的列表是使用双向链表实现的,其中它们使用指针来访问下一个/上一个元素。
我建议您使用的类型是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 << " ";
}

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

顺便说一句,如果你想将像向量这样的大型容器传递给像print()这样不需要修改向量的 function,你应该使用常量引用而不是复制向量。 在 C++11 中有一种称为基于范围的 for 循环的东西,它可以遍历诸如向量之类的东西,而不必担心诸如迭代器之类的东西,而std::set可能更适合实现邻接列表,因为它允许检查两个顶点以对数复杂度相邻。

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

由于看起来您正在做一个竞争性编程问题,我建议您在进行任何处理之前将输入转换为零索引。

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

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

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

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

但是,当您运行代码时,您会遇到段错误。 下面的主要 function 应该是不言自明的

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

请注意, std::list不是数组,不支持使用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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM