473,546 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector of lists: anyone know how to do this?

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, or references to material that might
explain it?

Any help greatly appreciated.

Jun 17 '06 #1
22 10741
craig wrote:
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, or references to material that
might explain it?


And what is reason you decided to hide your code from us? How can we
figure out what you're doing wrong?


Brian
Jun 17 '06 #2

craig wrote:
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, or references to material that might
explain it?

Any help greatly appreciated.


Why not something like:

Edge *pEdge = NULL;
vector< list< Edge * > > edges( 10 );
edges[ 0 ].push_back( pEdge );
Putting pointers into STL containers is not always a good idea. Make
sure you know what you're doing.

Prefer something like this:

boost::shared_p tr< Edge > pEdge;
vector< list< boost::shared_p tr< Edge > > edges( 10 );
edges[ 0 ].push_back( pEdge );

Your choice of smart pointer depends on context though, although
boost::shared_p tr is for many uses a good one. std::auto_ptr is not
normally a good choice.
Of course including the code that doesn't work and telling us what
you're thinking about might help us to answer better.
K

Jun 17 '06 #3
In article <1150530822.539 811.41420
@i40g2000cwc.go oglegroups.com> ,
ki************* ***@gmail.com says...

[ ... ]
Your choice of smart pointer depends on context though, although
boost::shared_p tr is for many uses a good one. std::auto_ptr is not
normally a good choice.


Let me put that more strongly. Anything you store in a
standard container needs to be copyable, and
std::auto_ptr isn't copyable, so it's simply not allowed.
In most cases, compilation will fail if you try, though
the error message(s) rarely make the source of the
problem very clear.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 17 '06 #4
Brian,..Sorry, I didn't give more info (it was late and I was tired).
But here is a bit more:

vector<Vertex*> vertices;
// array of adjecacency lists
vector< list<Edge*> > adjLists; <<<<< the focus

This is an array(vector) of "adjacency lists" for a graph. The graph
is empty at first, and then added to dynamically. I don't know if more
initialization has to be done before I add to it, but I've tried to add
elements in the following ways. (Adding an element is essentially
adding an "Edge" to the i-th adjacency list). pEdge is a ptr to an
Edge:

I tried:
adjLists.push_b ack( pEdge );
then:

adjLists[0].push_back( pEdge);

then:
list <Edge*> L;
adjLists[0] = L

I've tried many other things also, but most don't compile, and the ones
that do crash.

-Craig
Default User wrote:
craig wrote:
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, or references to material that
might explain it?


And what is reason you decided to hide your code from us? How can we
figure out what you're doing wrong?


Brian


Jun 17 '06 #5
Thanks, Kirit for your comments. I'm going to see if I can incorporate
your idea into what I'm doing. I've also provided a little more info
in my response to Brian. If my initial info was too vague, perhaps you
could look at this response.

Thanks,
-Craig

Kirit Sælensminde wrote:
craig wrote:
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, or references to material that might
explain it?

Any help greatly appreciated.


Why not something like:

Edge *pEdge = NULL;
vector< list< Edge * > > edges( 10 );
edges[ 0 ].push_back( pEdge );
Putting pointers into STL containers is not always a good idea. Make
sure you know what you're doing.

Prefer something like this:

boost::shared_p tr< Edge > pEdge;
vector< list< boost::shared_p tr< Edge > > edges( 10 );
edges[ 0 ].push_back( pEdge );

Your choice of smart pointer depends on context though, although
boost::shared_p tr is for many uses a good one. std::auto_ptr is not
normally a good choice.
Of course including the code that doesn't work and telling us what
you're thinking about might help us to answer better.


K


Jun 17 '06 #6
On 17 Jun 2006 10:44:38 -0700, "craig" <cr**********@c omcast.net>
wrote:
vector<Vertex* > vertices;
// array of adjecacency lists
vector< list<Edge*> > adjLists; <<<<< the focus

This is an array(vector) of "adjacency lists" for a graph. The graph
is empty at first, and then added to dynamically. I don't know if more
initializati on has to be done before I add to it, but I've tried to add
elements in the following ways. (Adding an element is essentially
adding an "Edge" to the i-th adjacency list). pEdge is a ptr to an
Edge:

I tried:
adjLists.push_ back( pEdge );
This does not compile because you try to push a pointer to and Edge
object into a vector of lists(!) of pointers to Edges.
then:

adjLists[0].push_back( pEdge);
adjLists is empty, you access the fist element in an empty array ->
crash.
then:
list <Edge*> L;
adjLists[0] = L
again, you access to fist element in an empty array.
I've tried many other things also, but most don't compile, and the ones
that do crash.


You have an array of list <Edge*>. So, first you need an element in
your array. Since STL supports only value semantics you have to copy a
list <Edge*> into adjLists.

list <Edge*> lst;
adjLists.push_b ack (lst);

// alternatively: adjLists.resize (1);

Then you can access the inserted list<Edge*> (the first and only so
far) with operator[], which returns a refernce(!) to that list
<Edge*>.

adjLists[0].push_back( pEdge);

Good luck!
Roland Pibinger
Jun 17 '06 #7
Thanks so much Roland!! I'm deeply greatful!

I'm in a pinch and am trying to get this to work properly after
considering other alternatives. I definitely need to study templates
much more when I get a chance.

If you (or any one else) has a moment, one more question:

Currently I am trying to use this "vector of lists" in the following
graph member:

void Graph::addEdge( Vertex &v1, Vertex &v2, int weight )
{
ind = "compute index of Vertex in vertex list"
Edge *e = new Edge (v1, v2, weight );
list <Edge*> lst; <<<<<<<<< important part to
make dynamic
adjLists.push_b ack (lst); <<<<<<<<<import ant part to make
dynamic
adjLists[ind].push_back( e );
}
I need to dynamically create a new "list <Edge*> " in place of "lst"
upon each call to "addEdge". How do I do this with templates?

Thanks,....
-Craig


Roland Pibinger wrote:
On 17 Jun 2006 10:44:38 -0700, "craig" <cr**********@c omcast.net>
wrote:
vector<Vertex* > vertices;
// array of adjecacency lists
vector< list<Edge*> > adjLists; <<<<< the focus

This is an array(vector) of "adjacency lists" for a graph. The graph
is empty at first, and then added to dynamically. I don't know if more
initializati on has to be done before I add to it, but I've tried to add
elements in the following ways. (Adding an element is essentially
adding an "Edge" to the i-th adjacency list). pEdge is a ptr to an
Edge:

I tried:
adjLists.push_ back( pEdge );


This does not compile because you try to push a pointer to and Edge
object into a vector of lists(!) of pointers to Edges.
then:

adjLists[0].push_back( pEdge);


adjLists is empty, you access the fist element in an empty array ->
crash.
then:
list <Edge*> L;
adjLists[0] = L


again, you access to fist element in an empty array.
I've tried many other things also, but most don't compile, and the ones
that do crash.


You have an array of list <Edge*>. So, first you need an element in
your array. Since STL supports only value semantics you have to copy a
list <Edge*> into adjLists.

list <Edge*> lst;
adjLists.push_b ack (lst);

// alternatively: adjLists.resize (1);

Then you can access the inserted list<Edge*> (the first and only so
far) with operator[], which returns a refernce(!) to that list
<Edge*>.

adjLists[0].push_back( pEdge);

Good luck!
Roland Pibinger


Jun 17 '06 #8
Again , thanks so much Roland and others!!!

Roland you corrections worked perfectly. One more question for anyone:
I need to dynamically create new "list <Edge*>"'s in place of "lst"
upon subsequent calls to "addEdge" (code below). How do I do this with
templates?

Do I create a list <Edge*> lists[10]; somewhere and index into it?

void Graph::addEdge( Vertex &v1, Vertex &v2, int weight )
{
ind = "compute index of Vertex in vertex list"
Edge *e = new Edge (v1, v2, weight );
list <Edge*> lst; <- how to make dynamic ??
adjLists.push_b ack (lst); <-how to make dynamic??
adjLists[ind].push_back( e );
}


Roland Pibinger wrote:
On 17 Jun 2006 10:44:38 -0700, "craig" <cr**********@c omcast.net>
wrote:
vector<Vertex* > vertices;
// array of adjecacency lists
vector< list<Edge*> > adjLists; <<<<< the focus

This is an array(vector) of "adjacency lists" for a graph. The graph
is empty at first, and then added to dynamically. I don't know if more
initializati on has to be done before I add to it, but I've tried to add
elements in the following ways. (Adding an element is essentially
adding an "Edge" to the i-th adjacency list). pEdge is a ptr to an
Edge:

I tried:
adjLists.push_ back( pEdge );


This does not compile because you try to push a pointer to and Edge
object into a vector of lists(!) of pointers to Edges.
then:

adjLists[0].push_back( pEdge);


adjLists is empty, you access the fist element in an empty array ->
crash.
then:
list <Edge*> L;
adjLists[0] = L


again, you access to fist element in an empty array.
I've tried many other things also, but most don't compile, and the ones
that do crash.


You have an array of list <Edge*>. So, first you need an element in
your array. Since STL supports only value semantics you have to copy a
list <Edge*> into adjLists.

list <Edge*> lst;
adjLists.push_b ack (lst);

// alternatively: adjLists.resize (1);

Then you can access the inserted list<Edge*> (the first and only so
far) with operator[], which returns a refernce(!) to that list
<Edge*>.

adjLists[0].push_back( pEdge);

Good luck!
Roland Pibinger


Jun 17 '06 #9

craig wrote:
void Graph::addEdge( Vertex &v1, Vertex &v2, int weight )
{
ind = "compute index of Vertex in vertex list"
Edge *e = new Edge (v1, v2, weight );
list <Edge*> lst; <- how to make dynamic ??
adjLists.push_b ack (lst); <-how to make dynamic??
adjLists[ind].push_back( e );
}


The list is already dynamic, the push_back will add to the end. The
vector is slightly different though. I think you missed the suggestion
of the resize() somewhere.

What you need is (I'm not g'teeing it will compile directly) is
something like:

vector< list< boost::shared_p tr< Edge > > >::size_type ind = /* Your
calc */;
if ( adjLists.size() < ind + 1 ) // Remember it is zero indexed
adjLists.resize ( ind + 1 );
adjLists[ ind ].push_back( boost::shared_p tr< Edge >( new Edge( v1, v2,
weight ) ) );

Things to note:
* The if statement checks to see that the vector is big enough _before_
you access the index.
* The use of the smart pointer. Don't get in the habit of using raw
pointers.
* The use of the type vector<>::size_ type for the index. Your compiler
vendor has gone to a lot of trouble making sure you won't have any
unexpected overflows etc. when they chose this type. Use it rather than
make your own guess about what is right.
* The vector is zero indexed so index 1 means it must be at least size
2
K

Jun 18 '06 #10

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

Similar topics

7
3995
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 Vector called stringArray. I am using a GUI to access this Vector. I have 2 buttons. ("NEXT" and "PREVIOUS"). I would like to use these buttons to...
34
4132
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want...
8
6845
by: Amit Bhatia | last post by:
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>;
1
3951
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot convert `__gnu_cxx::__normal_iterator<DailyTemp*, std::vector<DailyTemp, std::allocator<DailyTemp >' to `DailyTemp*' in assignment I believe the...
82
3948
by: Peter Olcott | last post by:
I need std::vector like capability for several custom classes. I already discussed this extensively in the thread named ArrayList without Boxing and Unboxing. The solution was to simply create non-generic (non C++ template) std::vector like capability for each of these custom classes. (Solution must work in Visual Studio 2002). Since I have...
3
3724
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an assignment is done of the referenced OtherObj object (according to the compiler messages). This is strange, since the referenced OtherObj is initialized...
13
7591
by: t.hall | last post by:
Is std::vector<T>::size_type guaranteed to be the same type as std::vector<U>::size_type? To be more explicit, given void f(T, U); and std::vector<Tvt; std::vector<Uvu; which have the same size, can I write
19
10733
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is there an existing adapter? Thanks, Daniel.
16
5799
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 finish whole simulation..... my question are the map iterators are faster than vector iterators..... does it improve the performance.... thanks to...
0
7507
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.