c++ - vector of vectors push_back -
i'm designing multilevel queue process simulator in c++ i've got problem when trying implement several queues (my queues vectors).so, "multilevel" 4 elements array (not vector). inside each of elements there vector (type t_pcb).
vector<vector<t_pcb>> multilevel[4]; my question is: how can insert element @ end of 1 these 4 t_pcb vectors? thank in advance.
i've tried code line below doesn't work (error: not matching member function call 'push_back')
multilevel[0].push_back(p); //where "p" t_pcb object the line above can not used when talking "multilevel" because array accepts arguments type: vector < t_pcb >
so, ask @ beginning: how can push object type "t_pcb" inside "multilevel"?
by doing this:
vector<vector<t_pcb> > multilevel[4]; you declare array of 4 zero-sized vectors, each of can contain objects of type vector<t_pcb>. wanted rather:
vector<vector<t_pcb> > multilevel(4); // ^^^ this instantiate vector of 4 default-initialized objects of type vector<t_pcb>. then, can do:
multilevel[size].push_back(p); notice, though, vector indices (like array indices) zero-based, size must less size of vector.
in above expression, sub-expression multilevel[size] returns reference size-th vector inside multilevel, , on vector invoking member function push_back(p), appends element p it.
Comments
Post a Comment