我从一个文件中读取了一些数字并使用以下代码将它们放在一个图形(称为grafo
)中:
struct node{
vector<int> vic;
bool visitato = false;
};
int main (){
vector<node> grafo;
ifstream in("input.txt");
int n, m, s, from, to;
in >> n >> m >> s;
grafo.resize(n);
for (int i = 0; i < m; i++){
in >> from >> to;
grafo[from].vic.push_back(to);
}
}
如何打印所有这些元素? 这是输入的txt文件:
0 2
0 4
1 4
3 2
2 4
4 3
我想我必须为每个向量打印所有(子)向量,所以我试着用这样的东西:
for (int i = 0; grafo.size() < n; i++)
for(int j = 0; j < grafo[i].vic.size(); j++)
cout << "From node " << i << " to node " << grafo[i].vic[j] << endl;
有什么建议吗?
编辑:ot,它有效,但我不确定我是否真的得到了我所做的以及它为什么有效。答案 0 :(得分:3)
grafo.size() < n
- 循环中的条件(即for
)将始终为false,这不是您想要的。
更改
for (int i = 0; grafo.size() < n; i++)
^^^^^^^^^^^^^^^^
到
for (int i = 0; i < grafo.size(); i++)
答案 1 :(得分:1)
你最好使用矢量迭代器。
using namespace std;
struct node{
vector<int> vec;
bool visitato = false;
};
int main()
{
vector<node> grafo;
int n,m,s,from,to;
ifstream in("input.txt");
in>>n>>m>>s;
grafo.resize(n);
for (int i = 0; i < m; i++){
in >> from >> to;
grafo[from].vec.push_back(to);
}
int i=0,j=0;
for ( auto it = grafo.begin(); it != grafo.end(); ++it, ++i){
for ( auto it2 = it->vec.begin(); it2 != it->vec.end(); ++it2, ++j){
cout << "From node " << i << " to node " << *it2 << endl;
}
}
return 0;
}
答案 2 :(得分:1)
您可能想要使用https://github.com/gileli121/VectorEx 在这个库中,你有显示矢量矢量的显示功能。
它在Windows下运行(在Windows 7上测试)并在visual studio 2015中开发。
此示例向您展示如何使用它:
// includes.functions
#include <windows.h>
// Include CU3 Library
// Get the files from https://github.com/gileli121/CU3-Library/
#include "CU3_Library\EasyCoding\easy_macros.h"
// Include VectorEx https://github.com/gileli121/VectorEx
#include "VectorEx\VectorEx.h"
#include "VectorEx\VectorDisplay.h"
using namespace vectorex;
using namespace vectordisplay;
/*
Example 1 - Manually make vector of vectors and then show it with vectordisplay::DisplayVector_OfVector
This example shows how to use display std::vector of std::vector(s) array datatype
with vectordisplay::DisplayVector_OfVector
*/
int main()
{
std::vector<std::vector<std::string>> stringVecofvec; // Create Vector of std::string vectors
DisplayVector_OfVectors(&stringVecofvec); // Display it
/*
As you can see, you get list view that is completely gray with nothing in it.
This is because that the vector (that intended to contains other vectors)
have 0 size.
So we resize this main vector to 10 in the next step.
*/
stringVecofvec.reserve(20); // Allocate space for 20 vectors
stringVecofvec.resize(10); // Resize the main vector to 10. so will be space for 10 vector in it.
DisplayVector_OfVectors(&stringVecofvec); // Display it
/*
As you can see, now the list view is not completely empty. you will see vector 0 - vector 9 (10 vectors)
It shows the room for the 10 vectors.
Also, you will see that after vector 9, there is unamed columns and the last one named with the number 19.
This is visualize the stringVecofvec.capacity() - the reserved space.
Remember that we was call "stringVecofvec.reserve(20);" ? this is why you see 19. (0-19 is 20)
However, the list view is still completely gray. because all the 10 vector inside the main vector
is 0 size.
So we resize some few vector (few vectors of 0-9 ) to random sizes.
*/
stringVecofvec[0].reserve(30); // Reserve space for 30 rows in vector 0
stringVecofvec[0].resize(5); // Resize vector 0 to 5.
stringVecofvec[1].reserve(20); // Reserve space for 20 rows in vector 1
stringVecofvec[2].reserve(13); // Reserve space for 13 rows in vector 2
stringVecofvec[3].resize(10); // And resize vector 3 to 10
stringVecofvec[5].reserve(15); // Reserve space for 15 rows in vector 5
stringVecofvec[6].resize(15); // And resize vector 6 to 15
stringVecofvec[7].reserve(27); // Reserve space for 27 rows in vector 7
stringVecofvec[9].reserve(24);// Reserve space for 10 rows in vector 9
stringVecofvec[9].resize(6); // And resize vector 9 to 6
DisplayVector_OfVectors(&stringVecofvec); // Display it
/*
As you can see, now not everything is gray. you will see some vectors that colored in white color,
some with Grey-Blue color and some that completely gray.
The vectors that are completely gray are those that with size 0. others have size that is bigger
then 0.
The size of each vector is visualized by the number of the white columns.
For example, in this case - vector 0 will have 5 white columns, and vector 6 will have 15 white columns.
The reserved size / .capacity() is visualized by the Grey-Blue color.
Vector with 0 size have no white columns. only gray.
Vector with 0 size but it's .capacity() bigger then 0 have only Grey-Blue color.
The .size() of the vector can never be bigger then the .capacity(). so if the .capacity() is 0,
the vector will have only gray color.
.size() visualized by white color
.capacity() visualized by Grey-Blue color
out of this range is unallocated memory zone
unallocated memory zone start where the .capacity() ends.
unallocated memory zone is visualized by Gray color.
reserved space for vector is visualized by on row and col of green color
In the next step will fill some vectors with data. You can fill data only on areas with white color.
*/
/*
Liked this example? Did that helped you? If so, you can also thanks by giving a small contribution
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=S2KFGDTQZG4XQ
*/
// Fill data in vector 0
stringVecofvec[0].at(0) = "HELLO"; // Fill data in vector 0 -> row 0
stringVecofvec[0].at(4) = "VECTORS!"; // Fill data in vector 0 -> row 4
/*
Try to add stringVecofvec[0].at(5) = "TEST";
It will not work because you try to fill data in no-space
you can fill data only in space.
*/
// Fill data in vector 3 ()
stringVecofvec[3].at(4) = "OF"; // Fill data in vector 3 -> row 4
stringVecofvec[3].at(0) = "WORLD"; // Fill data in vector 3 -> row 0
stringVecofvec[3].at(8) = "VERY"; // Fill data in vector 3 -> row 8
/*
you can't fill data in vector 2. it have 0 size.
But you can try different thing that will work.
Try to add here:
stringVecofvec[2].push_back("TEST");
What will happen here is that it will use the reserved space that
is visualized by Grey-Blue color and write in the text in the next available
*unused* reserved space
*/
// Fill data in vector 6
stringVecofvec[6].at(4) = "VECTORS"; // Fill data in vector 6 -> row 4
stringVecofvec[6].at(0) = "OF MANY"; // Fill data in vector 6 -> row 0
stringVecofvec[6].at(14) = "EASY :)"; // Fill data in vector 6 -> row 14
// Fill data in vector 9
stringVecofvec[9].at(4) = "EXAMPLE !"; // Fill data in vector 9 -> row 4
stringVecofvec[9].at(0) = "VECTORS !"; // Fill data in vector 9 -> row 0
/*
Try to add here
stringVecofvec[9].push_back("TEST");
If you do not know how vectors works, you may expect that it will add
"TEST" in row 5.
This is not true. It will add it in row 6.
.push_back looks for the next *unused* reserved space.
the next *unused* reserved space is available in row 6 that colored in Grey-Blue color.
After that, unused become used so it colored in white with the text in it.
*/
DisplayVector_OfVectors(&stringVecofvec); // Display it
/*
Is it looks much more beautiful than this ugly black console?
Enjoy.
*/
return 0;
}
这是最终输出的图像: