Vector of vectors

M

Manuel

Hi.
I'm a question about vectors.

if I write a code that (in pseudo code):

1)Declare vector1
2)Declare vector2
3)vector1.push_back(vector2)
4)...
5)vector2 filled with various elements

vector1 is updated?
Even if I've "push_back" vector2 before fill it?
or I should respect a sequence, like:

1)Declare vector1
2)Declare vector2
3)vector2 filled with various elements
4)vector1.push_back(vector2)
5)...


thx,

Manuel
 
W

Winbatch

Manuel said:
Hi.
I'm a question about vectors.

if I write a code that (in pseudo code):

1)Declare vector1
2)Declare vector2
3)vector1.push_back(vector2)
4)...
5)vector2 filled with various elements

vector1 is updated?
Even if I've "push_back" vector2 before fill it?
or I should respect a sequence, like:

I believe that push_back makes a copy, so that vector 1 would contain an
empty vector 2. (Unless you are storing a vector of pointers..)
 
M

Manuel

Winbatch said:
I believe that push_back makes a copy, so that vector 1 would contain an
empty vector 2. (Unless you are storing a vector of pointers..)

it's a vector of pointers:

------------------------------------------------
#include <windows.h>
#include "mhcontainer.h"


#include <iostream>//For debug

struct DeleteObjs
{
template <typename T>
void operator()(const T* ptr) const
{
delete ptr;
}
};

//Put widget into container
void mhcontainer::addWidget(mhwidget* w) { widgetList.push_back(w); }

//Draw all widgets
void mhcontainer::drawAll()
{
std::cout << "size: " << widgetList.size();
std::vector<mhwidget*>::iterator it = widgetList.begin();
while(it != widgetList.end())
{
std::cout << "widget!";
(*it++)->draw();
}
}

//Delete all widgets
void mhcontainer::deleteAll()
{
std::for_each(widgetList.begin(), widgetList.end(), DeleteObjs());
}

-------------------------------------------------------
 
D

David Harmon

On Fri, 30 Dec 2005 00:54:08 +0100 in comp.lang.c++, Manuel
1)Declare vector1
2)Declare vector2
3)vector1.push_back(vector2)
4)...
5)vector2 filled with various elements

vector1.push_back creates an element of vector1 and initializes it
as a copy of vector2. Thereafter, vector1[0] and vector2 have
different identities. Vector1[0].push_back() has no effect on
vector2, and vice versa.
 
M

Manuel

David said:
On Fri, 30 Dec 2005 00:54:08 +0100 in comp.lang.c++, Manuel
1)Declare vector1
2)Declare vector2
3)vector1.push_back(vector2)
4)...
5)vector2 filled with various elements


vector1.push_back creates an element of vector1 and initializes it
as a copy of vector2. Thereafter, vector1[0] and vector2 have
different identities. Vector1[0].push_back() has no effect on
vector2, and vice versa.

Thanks.
And what if v1 push_back a pointer to v2?

ciao,

Manuel
 
L

Luke Meyers

Manuel said:
David said:
vector1.push_back creates an element of vector1 and initializes it
as a copy of vector2. Thereafter, vector1[0] and vector2 have
different identities. Vector1[0].push_back() has no effect on
vector2, and vice versa.

Thanks.
And what if v1 push_back a pointer to v2?

So v1 is a vector of pointers to vectors like v2. It holds a pointer
to v2 -- the location of v2 in memory. Changing the contents of that
memory (by populating v2) doesn't affect the pointer, just the values
accessible by dereferencing it.

There's only one copy of v2. If you update it, those updates will be
seen however you access them -- directly (via v2) or indirectly (by
retrieving the pointer from v1 and dereferencing it).

HTH,
Luke
 
M

Manuel

Luke said:
So v1 is a vector of pointers to vectors like v2. It holds a pointer
to v2 -- the location of v2 in memory. Changing the contents of that
memory (by populating v2) doesn't affect the pointer, just the values
accessible by dereferencing it.

There's only one copy of v2. If you update it, those updates will be
seen however you access them -- directly (via v2) or indirectly (by
retrieving the pointer from v1 and dereferencing it).

Thanks.
So it should work...

This is the case I'm trying to code:

A very simple GUI in openGL.
I've a main base window, some canvas, and some buttons.
The base window is a container for canvas.
The canvas are container for buttons.

Some buttons are created and deleted "on the fly",
so the content of some canvas is variable.

So my idea is to have a base_window vector filled with pointers to canvas.

Maybe OK?

Thanks,

Manuel
 
L

Luke Meyers

Manuel said:
I've a main base window, some canvas, and some buttons.
The base window is a container for canvas.
The canvas are container for buttons.

Some buttons are created and deleted "on the fly",
so the content of some canvas is variable.

So my idea is to have a base_window vector filled with pointers to canvas.

Maybe OK?

Sounds like an acceptable scheme. I assume you don't really mean that
the base window *is* a container of canvases, but rather that it has or
is associated with one. In C++ land, the word "container" tends to
mean specifically STL containers or similar constructs. Presumably
your base window has more going on than just containing canvases.

But I'm just being pedantic. Your design sounds fine.

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top