473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Vector of lists?

User-Agent: OSXnews 2.081
Xref: number1.nntp.dca.giganews.com comp.lang.c++:817366
Hi,
I have the following question:

I make a list whose elements are instances of class B;
list<class B>;

Now I wish to make a vector of such lists:

vector<list<class B v;

Is this operation valid?

thanks,
--a.
--
Aug 19 '06 #1
8 6835
Amit Bhatia wrote:
I make a list whose elements are instances of class B;
list<class B>;

Now I wish to make a vector of such lists:

vector<list<class B v;

Is this operation valid?
Yes, but your syntax is slightly off.

If you have a class B:

class B {};

then a list of that class would be:

std::list<BtheList;

and a vector of such lists would be:

std::vector<std::list<B v;

Best regards,

Tom

Aug 19 '06 #2

"Amit Bhatia" <am****************@nospam.gmail.coma écrit dans le message
de news: ec**********@daisy.noc.ucla.edu...
>
Hi,
I have the following question:

I make a list whose elements are instances of class B;
list<class B>;

Now I wish to make a vector of such lists:

vector<list<class B v;

Is this operation valid?

thanks,
--a.
--


Aug 19 '06 #3

"Amit Bhatia" <am****************@nospam.gmail.coma écrit dans le message
de news: ec**********@daisy.noc.ucla.edu...
>
Hi,
I have the following question:

I make a list whose elements are instances of class B;
list<class B>;

Now I wish to make a vector of such lists:

vector<list<class B v;

Is this operation valid?
yes

as long as you dont type
vector<list<class B>v; // should be not >>

moreover, elements in a vector must be default constructible
and list has a default constructor.

So you cannot have a vector of Widget if Widget is like :

class Widget
{
Widget() {} // default ctor is private
};

so
vector<Widgetvec(100);
will not compile

Eric

Aug 19 '06 #4
In article <nc***************@read2.cgocable.net>,
er**********@cgocable.ca says...

[ ... ]
So you cannot have a vector of Widget if Widget is like :

class Widget
{
Widget() {} // default ctor is private
};

so
vector<Widgetvec(100);
will not compile
That won't compile, but you CAN still create a vector of items that
aren't default constructible. In this case, you have to supply an object
of the correct type that's used to initialize the objects in the vector.
Since the initializing object isn't the first parameter, you also have
to supply an initial size for the vector:

class Widget {
int x_;
Widget() {} // default ctor is still private
public:
Widget(int x) x_(x) {} // requires a parameter
};

// A vector of 100 Widgets, each initialized to 200:
std::vector<Widgetvw(100, Widget(200));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 19 '06 #5
User-Agent: OSXnews 2.081
Xref: number1.nntp.dca.giganews.com comp.lang.c++:817423
Thanks.
but this brings me to one more question: I guess the elements of a list
"should" also be default constructible? I want to make a tree of nodes as follows, but am not sure now if it
will work:
In node.h

class Node
{
// some stuff ctors, dtors, etc;
//Default ctor;

Node &parent_node; // Could use pointer here but want to avoid it if
possible. vector< Node & child_nodes; //Could use pointer here but want to
avoid it if possible.};

In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes;
};
I don't want to create duplicate copies of nodes, and want everything to
point correctly to elements in the global list treeofnodes.
any suggestions?

thanks,
--a.

"Eric Pruneau" <er**********@cgocable.cawrote:
>
"Amit Bhatia" <am****************@nospam.gmail.coma écrit dans le message
de news: ec**********@daisy.noc.ucla.edu...
>>
Hi,
I have the following question:

I make a list whose elements are instances of class B;
list<class B>;

Now I wish to make a vector of such lists:

vector<list<class B v;

Is this operation valid?

yes

as long as you dont type
vector<list<class B>v; // should be not >>

moreover, elements in a vector must be default constructible
and list has a default constructor.

So you cannot have a vector of Widget if Widget is like :

class Widget
{
Widget() {} // default ctor is private
};

so
vector<Widgetvec(100);
will not compile

Eric


--
Aug 20 '06 #6

"Amit Bhatia" <am****************@nospam.gmail.coma écrit dans le message
de news: ec**********@zinnia.noc.ucla.edu...
>
Thanks.
but this brings me to one more question: I guess the elements of a list
"should" also be default constructible?
yes, or you have to allocate with initialization

I want to make a tree of nodes as follows, but am not sure now if it
will work:
In node.h

class Node
{
// some stuff ctors, dtors, etc;
//Default ctor;

Node &parent_node; // Could use pointer here but want to avoid it if
possible.
vector< Node & child_nodes; //Could use pointer here but want to
avoid it if possible.};
No no no... cannot do a vector of references. This is illegal
you must use pointers here.

Are you sure you want to use vector here...
If you have to do a lot of insertion/removal list is probably the
structure you want.
>
In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes;
Samething here with list.. must use pointers.
I dont see why you are using a list here and a vector in
your node class
};
I don't want to create duplicate copies of nodes, and want everything to
point correctly to elements in the global list treeofnodes.
any suggestions?
Well if you want a basic implementation of a tree, you seems to be on the
right track. just have a tree class with a list of Node. And Node having
also
a list of Node. Now do you needs your Nodes to be dynamically allocated?

something like

class Tree
{
public:
[...] ctor dtor
[...] // probably want to give access to some functions to search or sort
your tree
private:
Node m_Root;
list<Nodem_Childs;
};

class Node
{
public:
[...] ctor dtor
private:
[...] info about the node
list<Nodem_Childs;
// Do you really need a reference to the parent??? maybe not...
};
Aug 20 '06 #7
User-Agent: OSXnews 2.081
Xref: number1.nntp.dca.giganews.com comp.lang.c++:817485
"Eric Pruneau" <er**********@cgocable.cawrote:
>
"Amit Bhatia" <am****************@nospam.gmail.coma écrit dans le message
de news: ec**********@zinnia.noc.ucla.edu...
>>
Thanks.
but this brings me to one more question: I guess the elements of a list
"should" also be default constructible?

yes, or you have to allocate with initialization

I want to make a tree of nodes as follows, but am not sure now if it
>will work:
In node.h

class Node
{
// some stuff ctors, dtors, etc;
//Default ctor;

Node &parent_node; // Could use pointer here but want to avoid it if
possible.
vector< Node & child_nodes; //Could use pointer here but want to
avoid it if possible.};

No no no... cannot do a vector of references. This is illegal
you must use pointers here.

Are you sure you want to use vector here...
If you have to do a lot of insertion/removal list is probably the
structure you want.
>>
In tree.h

class Tree
{
//again ctors, dtors, and some other stuff;
list< Node &treeofnodes;

Samething here with list.. must use pointers.
I dont see why you are using a list here and a vector in
your node class
>};
I don't want to create duplicate copies of nodes, and want everything to
point correctly to elements in the global list treeofnodes.
any suggestions?

Well if you want a basic implementation of a tree, you seems to be on the
right track. just have a tree class with a list of Node. And Node having
also
a list of Node. Now do you needs your Nodes to be dynamically allocated?

something like

class Tree
{
public:
[...] ctor dtor
[...] // probably want to give access to some functions to search or sort
your tree
private:
Node m_Root;
list<Nodem_Childs;
};

class Node
{
public:
[...] ctor dtor
private:
[...] info about the node
list<Nodem_Childs;
// Do you really need a reference to the parent??? maybe not...
};

Thanks. Yes I do need quick access to the parent. In your suggestion,
there is something like

list<Nodem_childs;

But won't this result in duplicate copies: one in node and one in tree?
I want to avoid that. The feature that I wish to have is to be able to remove a particular
child node whenever I want, and also free up that space from the tree. If
I use pointers, and I have a list of pointer
class Tree{
//; ;
list<Node * m_Childs;
};

but when I free memory from a particular pointer, can I ensure somehow
that that pointer entry is also removed from the list? I guess I could do
it if I could use iterators as
class Node{

//; ;
vector<list<Node>::iterator m_Childs;
};

and then
class Tree{
//; ;
list<Nodetree;
};

but just using pointers ? btw, I wanted to use vector in nodes as I will
be needing random access, and when I do have to delete the children, I
will probably be requiring to delete all of them in one go.
thanks,
--a.
Aug 20 '06 #8
>
Thanks. Yes I do need quick access to the parent. In your suggestion,
there is something like

list<Nodem_childs;

But won't this result in duplicate copies: one in node and one in tree?
humm no.
the tree is just like a node (except it doesnt have a parent).
The tree contain a list of his (immediate) children, not the list of all
node in the tree.
Each of tree's child ( a node) contain a list of his children ( a list of
nodes) and so on.

So there is only one node created for each elements of your tree.

Also, you can use either reference or pointer for keeping track of
the parent of a node.

Eric
Aug 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Row | last post by:
Hi, Im relitivly new to java. I guess you could say im a newbie (even though i studied it 3 years ago) I have 2 simple questions. relating to java vectors Question 1: My application uses an...
3
by: Andreas Krueger | last post by:
Hi! I am fed up with vector<int> iv; iv.push_back(42); iv.push_back(9); iv.push_back(11); ... and would rather use a function "fillVector": vector<int> iv = fillVector(42,9,11); like...
6
by: Ash Lux | last post by:
Does the C++ vector template class use pointer-based link lists? Could I get a link explaining how it works, regardless of what it uses? Thank You, Ash Lux
6
by: Andreas Bauer | last post by:
I've got an array in the vein of MyType* array; arry = new MyType; This of course works fine, but I have the need to dynamically grow the boundaries of that array at runtime. I can't seem to...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
34
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...
2
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently...
22
by: craig | last post by:
I've declared the following as a "vector" of "lists" using STL vector< list<Edge*> > I've tried many ways to add objects to this but can't figure out how to do it. Does anyone have any tips,...
16
by: xyz | last post by:
I have to run the simulation of a trace file around (7gb contains 116million entries)... presently i am using vector iterators to check the conditions in my program..... it is taking 2 days to...
0
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...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.