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 9 1585
Manuel wrote: Hi. I'm a question about vectors.
I've a question :-)
sorry, I'm a bit tired...
"Manuel" <ma**********************@tin.it> wrote in message
news:43**********************@reader1.news.tin.it. .. 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..)
Winbatch wrote: 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());
}
-------------------------------------------------------
On Fri, 30 Dec 2005 00:54:08 +0100 in comp.lang.c++, Manuel
<ma**********************@tin.it> wrote, 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.
David Harmon wrote: On Fri, 30 Dec 2005 00:54:08 +0100 in comp.lang.c++, Manuel <ma**********************@tin.it> wrote,
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
Manuel wrote: David Harmon wrote: 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
Luke Meyers wrote: 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
Manuel wrote: 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
Luke Meyers wrote: Presumably your base window has more going on than just containing canvases.
Oh..yes, sure. Your design sounds fine.
Thanks.
I'm a C++ newbie and your opinion is an encouragement.
Cheers,
Manuel This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: {AGUT2}=IWIK= |
last post by:
Hello all,
It's my fisrt post here and I am feeling a little stupid here, so go easy..
:) (Oh, and I've spent _hours_ searching...)
I am desperately trying to read in an ASCII...
|
by: BCC |
last post by:
If I create a vector of vectors of double:
std::vector< std::vector<double> > table1;
Are my vectors of doubles uninitialized? Do I have to loop through table1
and initialize each vector of...
|
by: Nancy Keuss |
last post by:
Hi,
I've created a vector of vectors of ints, and I want to pass it as a
parameter to a function. Is this possible, and if so, then what is the
syntax like for the function header and function...
|
by: Dennis |
last post by:
Hi
I'm trying to implement a vector of vectors where find can be used to
find a vector<double> in the vectors of vectors, that is hard to
understand i guess.
What I mean is that I got a vector...
|
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and...
|
by: pmatos |
last post by:
Hi all,
I have a vector of vector of ints, I could use C approach by using
int but I think C++ vector<vector<int> > would be easier to manage.
So I have a function which creates and initializes...
|
by: mahurshi |
last post by:
I've got a gate structure that looks like this
/* Defining sGATE structure */
struct sGATE
{
string name;
vector<int> input;
int output;
};
|
by: cayblood |
last post by:
Hello, I have been interested in something kind of like the
next_permutation from the STL algorithm library, except that I want it
to find possible combinations of vector elements. Here is a more...
|
by: acosgaya |
last post by:
hi,
I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors.
I am trying to partition...
|
by: madhu |
last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx
vector <intv1;
v1.push_back( 10 ); //adds 10 to the tail
v1.push_back( 20 ); //adds 20 to the tail
cout << "The size of v1 is " <<...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |