简体   繁体   中英

C++: how to print all elements in a vector of vectors

I read from a file some number and put them in a graph (called grafo ) with this code:

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

How can I print all those elements? Here's the input txt file:

0 2
0 4
1 4
3 2
2 4
4 3

I think I have to print all (sub) vectors for each vector, so I tried with something like this:

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;

Any advice?

EDIT: ot, it works but I'm not sure I really got what I did and why is it working.

The condition, ie grafo.size() < n , in the for -loop will always be false, which is not what you want.

Change

for (int i = 0; grafo.size() < n; i++)
                ^^^^^^^^^^^^^^^^

to

for (int i = 0; i < grafo.size(); i++)

You'd better use vector iterator.

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

You may want to use https://github.com/gileli121/VectorEx In this library you have display function that display vector of vectors.

It works under windows (tested on windows 7) and developed under in visual studio 2015.

This example shows you how to use it:

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

Here is an image of the final output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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