how to declare number of vectors dynamically

I need to declare the number of vectors according to an input parameter call NUM HOST.
for example, if NUMHOST=4, I need to declare 4 vectors and the name of the vectors seriam: vector1, vector2, vector3, in struct for would be vectori...

for (int i = 0; i <= NUMHOST; i++)
int vector????[3];
I suggest nesting vectors.

1
2
3
std::vector<std::vector<YourClass>> v;
for (int i = 0; i < NUMHOST; ++i)
    v.emplace_back();
Yeah, store the vectors in a vector. But you don't have to use a loop.Instead you can pass the size of the vector to the constructor.
std::vector<std::vector<YourClass>> v(NUMHOST);
Last edited on
@Peter87

You are very right! My loop would be terribly inefficient; especially when NUMHOST → ∞ compared to your version (or resize()).

1
2
3
4
5
6
7
8
9
10
11
#include <vector>

int main()
{
    enum {NUMHOST = 4};
    std::vector<std::vector<int>> v(NUMHOST);
    v.at(3).push_back(12);
//    for (int i = 0; i < NUMHOST; ++i)
//        v.emplace_back();
}
I believe that I can not use vector of vectors because I'm programming a distributed system and each node should only be aware of your information and not the information global network, so I have to have a vector only.
I don't understand your logic, how is that relevant to whether you use a vector of vectors or not? Alternatives are far and few.
It should not matter, as long as you don't share information that is not necessary.

1
2
3
4
5
6
7
8
9
10
11
12
int do_something_to_a_vector( std::vector <int> & v )
  {
  // this function only knows about v
  }

int main()
  {
  // The list of vectors
  std::vector <std::vector <int> > vs( n );
  ...
  do_something_to_a_vector( vs[ 2 ] );
  ...
I try declare the function do_something_to_a_vector, but when I call the function "do_something_to_a_vector (vs [2]), ocurrs the following error:

no matching function for call to do_something_to_a_vector(int&)'

Also I not understand what would be the parameter 2, would be the index the vector?
thanks.
Is vs declared like on line 9 of Duoas' code?
thanks, now is all right, however, I not understand:

1- what is "vs[ 2 ]?"

2- how assign values to vs???
vs is a vector that contains other vectors. vs[2] is the third vector it contains.
I understand,thanks, but I try assign values para v[a], but don't get, I try:

v[1]=1;

v.at(0).at(0) =1;

v.at(1).push_back(1);


std::vector <std::vector <int> > vs( n );
creates a vector of n empty vectors. These vectors must be populated before you can access elements of those vectors.


std::vector<std::vector<int>> vs(n, std::vector<int>(m))
creates a vector of n vectors of m elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <iomanip>
#include <iostream>

void print(const std::vector<int> & v)
{
    for ( unsigned i=0; i<v.size(); ++i )
        std::cout << std::setw(3) << v[i]  ;
    std::cout << '\n' ;
}

int main()
{
    unsigned n = 5, m=10 ;
    std::vector <std::vector <int> > vs( n, std::vector<int>(m) );

    for ( unsigned i=0; i<vs.size(); ++i )
        for ( unsigned j=0; j<vs[i].size(); ++j )
            vs[i][j] = i+j ;

    for ( unsigned i=0; i<vs.size(); ++i )
        print(vs[i]) ;
}
Last edited on
thanks,
works!!
Topic archived. No new replies allowed.