-3

I need to have dynamically allocated space of structs and those structs have to contain another dynamically allocated. If I do it by manual allocation, that would laborious.

So I want to do it through vectors:

using namespace std;

struct lol{
    int value;
    vector<int> vekt;
};

vector<lol> vektt;

It is logically, that I am going to do it like the code above but I dont know how to do 2 things that I am going to need for my program:

1.iterate through both of them to get values

2.push something into vekt( the vector of struct );

I tried something like this for pushing but doesnt work:

vektt[0] . vekt . push_back( 2 );

So I need to know how to iterate through both of these vectors and how to access members and methods of the vector vect.

alik33
  • 141
  • 3
  • 13
  • read some documentation and/or use the sidebar. – M.M Mar 26 '16 at 09:12
  • 1
    [How to iterate](http://stackoverflow.com/questions/409348/iteration-over-stdvector-unsigned-vs-signed-index-variable) , [How to push](http://stackoverflow.com/questions/26613246/pushing-back-data-into-2d-vector-in-c) – M.M Mar 26 '16 at 09:14
  • really? dont say! maybe I didnt quite understand and because of that came here? – alik33 Mar 26 '16 at 09:17

1 Answers1

0
// Iterating
for (auto &i: vektt)                 // or const auto &i according to requirements
{
    cout << i.value << ", ";
    for (auto j: i.vekt) cout << j << ' ';
    cout << '\n';
}
// Inserting int to vekt of i-th element of vektt
int a;
vektt[i].vekt.push_back(a);
anukul
  • 1,922
  • 1
  • 19
  • 37