所以我有这个片段
vector<int> *adj;
adj = new vector<int>[n];这是另一种常见的方式
vector<vector<int> adj(n);以前使用指针的方法可以用来制作二维数组吗?它会像后者一样工作吗?
另外,如果以前的方法可以用来制作二维矢量,那么也可以使用前者。
我也可以用以前的方式制作2d列表吗?
对于那些想知道以前的方法是错误的列表的人来说,它是为极客http://www.geeksforgeeks.org/topological-sorting/准备的极客
发布于 2017-12-16 14:13:00
有许多方法可以创建多维向量,但我使用这种方法,它利用了struct,
下面是2X2表的一个示例
#include<iostream>
#include<vector>
struct table
{
std::vector<int> column;
};
int main()
{
std::vector<table> myvec;
// values of the column for row1
table t1;
t1.column = {1,2};
// Push the first row
myvec.push_back(t1);
// values of the column for row2
table t2;
t2.column = { 3, 4};
// push the second row
myvec.push_back(t2);
// now let us see whether we've got a 2x2 table
for(auto row : myvec)
{
auto values = row.column;
for(auto value : values) std::cout<< value << " ";
std::cout<<"\n";
}
// Now we will try to get a particular value from the column index of a particular row
table row = myvec[1]; // 2nd row
std::cout<<"The value present at 2nd row and 1st column is: "<<row.column[0] <<"\n";
}这给了我
1 2
3 4
The value present at 2nd row and 1st column is: 3您可以轻松地将其更改为不同的尺寸。
注意:据我所知,我已经发布了这个答案,如果是错误的,有人纠正我。谢谢
https://stackoverflow.com/questions/47842687
复制相似问题