473,770 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL: Map of List of Pointers

Ok, so I'm trying to code a map of lists of pointers to a class (that
contains a struct). Basically, the class holds the struct that I'm
populating, and there can be multiple of these. The declaration looks
like this:
map<int, list<my_class*> > map_test;

Now, how do I go about adding and accessing a new element to any given
list? How would I access the data? I did this with a list of
pointers, but can't figure out how to get the map into the equation.
My 'list' code looked like this:

#include <iostream>
#include <string>
#include <list>
#include <map>

using namespace std;

class ex_class
{
public:
struct
{
int num;
int num2;
} info;

int inc_num () {return ++info.num;}
int dec_num () {return --info.num;}
int add_nums () {return info.num + info.num2;}
void print_vals ()
{
cout << "num: " << info.num << " num2: " << info.num2 << "\n\n";
}
};

int main ()
{
list<ex_class*> class_list, anon_list;
list<ex_class*> ::iterator class_it, anon_it;
map<int, list<ex_class*> > map_test;
ex_class classone;
classone.info.n um = 40;
classone.info.n um2 = 30;
classone.print_ vals();
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << "Sum: " << classone.add_nu ms() << "\n\n";

class_list.push _back (&classone);
class_it=class_ list.begin();
(*class_it)->info.num = 20;
(*class_it)->print_vals() ;

anon_list.push_ back (new ex_class);
anon_it=anon_li st.begin();
(*anon_it)->info.num = 33;
(*anon_it)->info.num2 = 77;
(*anon_it)->print_vals() ;

return 0;
}
Any help would be great!

Oct 20 '05 #1
6 24447

<gt*****@gmail. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Ok, so I'm trying to code a map of lists of pointers to a class (that
contains a struct). Basically, the class holds the struct that I'm
populating, and there can be multiple of these. The declaration looks
like this:
map<int, list<my_class*> > map_test;

Now, how do I go about adding and accessing a new element to any given
list? How would I access the data? I did this with a list of
pointers, but can't figure out how to get the map into the equation.
My 'list' code looked like this:

#include <iostream>
#include <string>
#include <list>
#include <map>
#include <utility>

using namespace std;

class ex_class
{
public:
struct
{
int num;
int num2;
} info;

int inc_num () {return ++info.num;}
int dec_num () {return --info.num;}
int add_nums () {return info.num + info.num2;}
void print_vals ()
{
cout << "num: " << info.num << " num2: " << info.num2 << "\n\n";
}
};

int main ()
{
list<ex_class*> class_list, anon_list;
list<ex_class*> ::iterator class_it, anon_it;
map<int, list<ex_class*> > map_test;
This line won't compile. Make it:

map<int, list<ex_class*> > map_test;
ex_class classone;
classone.info.n um = 40;
classone.info.n um2 = 30;
classone.print_ vals();
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << classone.inc_nu m() << '\n';
cout << "Sum: " << classone.add_nu ms() << "\n\n";

class_list.push _back (&classone);
class_it=class_ list.begin();
(*class_it)->info.num = 20;
(*class_it)->print_vals() ;

anon_list.push_ back (new ex_class);
anon_it=anon_li st.begin();
(*anon_it)->info.num = 33;
(*anon_it)->info.num2 = 77;
(*anon_it)->print_vals() ;
map_test.insert (pair<int, list<ex_class*> >(42, class_list));
map_test.insert (pair<int, list<ex_class*> >(99, anon_list));

list<ex_class*> ::const_iterato r it(map_test[42].begin());
cout << (**it).info.num << '\n';

/* 42 and 99 are arbitrary values */

-Mike

return 0;
}
Any help would be great!

Oct 20 '05 #2
Mike-
Thanks for the speedy reply, and help. I have two more questions. Is
there a way to do something like this so I wouldn't have to use an
intermediate object? (I know this probably doesn't compile,it's just an
example to give you an idea of what I want to do):

map_test.insert (pair<int, list<ex_class*> >(43, new ex_class);

Also, how would I reference the struct in the class?

Oct 21 '05 #3

"gthorne" <gt*****@gmail. com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
Mike-
Thanks for the speedy reply, and help. I have two more questions. Is
there a way to do something like this so I wouldn't have to use an
intermediate object? (I know this probably doesn't compile,it's just an
example to give you an idea of what I want to do):

map_test.insert (pair<int, list<ex_class*> >(43, new ex_class);
You can't do that because the second template parameter is
a pointer to a list, not a pointer to an 'ex_class'. Also
note that this still uses an 'intermediate' (more formally,
a 'temporary') object, that is, the pointer object returned
by the 'new' operator
However you could first insert empty lists into your map,
and then insert 'ex_class' objects into those lists after
the fact.

Also, how would I reference the struct in the class?


See the last 'cout' statement in my example.

(BTW, in my last post, I forget to
advise you that you should 'delete' any objects which you've
allocate with 'new', when done with them. Your code you
posted did not do that).

-Mike
Oct 21 '05 #4
"gthorne" <gt*****@gmail. com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
Is
there a way to do something like this so I wouldn't have to use an
intermediate object? (I know this probably doesn't compile,it's just an
example to give you an idea of what I want to do):

map_test.insert (pair<int, list<ex_class*> >(43, new ex_class);


First, I suggest that you get into the habit of using typedefs to make your
code more clear. This would also avoid your earlier error that was corrected
by Mike Wahler.

class Foo{ /* ... */};

typedef list<Foo*> Foos;
typedef map<int, Foos> FoosDictionary;

Adding make_pair to this, we get a much more readable code:

Foos anon_foos;
FoosDictionary foosDictionary;
foosDictionary. insert(make_pai r(42, anon_foos));

As you've observed, this takes a copy of anon_foos and stores that copy in
the dictionary. In order to get what you asked, write a function that
returns an ex_class*, and call it when calling make_pair:

ex_class * prepare_ex_clas s(/* ... */)
{
ex_class * object = new ex_class(/* ... */);
// do other things to object ...
return object;
}

Then, when you insert:

map_test.insert (make_pair(43, prepare_ex_clas s(/* ... */));

But then you should change the definition of the dictionary:

typedef map<int, Foos *> FoosDictionary;

Be warned though, these are not exception safe. A common safe way of storing
dynamic objects in standard containers is to store smart pointers instead of
plain pointers. Even your list<Foo*> could benefit from a smart pointer:

#include <boost/shared_ptr.hpp>

using namespace boost;

/* ... */

typedef shared_ptr<Foo> FooPtr;
typedef list<FooPtr> Foos;
typedef shared_ptr<Foos > FoosPtr;
typedef map<int, FoosPtr> FoosDictionary;

// etc. :)

Ali

Oct 21 '05 #5
Ali/Mike-
Thanks for your great replies. As for 'delete' and the typedefs, those
are coming. What I posted above was just sample code that I was
tinkering with to get this to work.

Do you think it may be better to put the list inside of the class?
Basically, without getting into the specific (i.e. 'boring') business
need for this code, think of it as a grocery store checkout (that's not
what it is, but it's a good example). The class holds the information
for one item. The list is the multiple items that the cashier is
ringing up, and the map is the multiple lanes. Should it be 'map of
lists of pointers to classes containing a struct', or 'map of pointers
to classes containing lists of structs'?

You guys have been a great help, and I appreciate it very much.

Oct 21 '05 #6
"gthorne" <gt*****@gmail. com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
As for 'delete' and the typedefs, those
are coming.
Note that, if you go the smart pointer route, you highly likely won't need
to delete explicitly. boost::shared_p tr, which is also a standard library
feature after TR1, deletes the objects it owns automatically.

As for the class design, given what you describe, I picture it as the
following:
The class holds the information
for one item.
class Item {/* ... */};
The list is the multiple items that the cashier is
ringing up,
Simply:

typedef list<Item> GroceryBag;

or, if it needs more logic:

class GroceryBag
{
list<Item> items_;
Color color_; // "colour" works too :)
// etc.

/* ... */
};
and the map is the multiple lanes.
class Cashier
{
/* ... */

public:

void process(Grocery Bag const & groceryBag);

/* ... */
};

typedef map<int, Cashier> Cashiers;

class Shopper
{
GroceryBag bag_;

/* ... */

public:

void check_out(Cashi er const & cashier)
{
cashier.process (bag_);
}
};

Cashier & least_busy_cash ier(Cashiers & cashiers)
{
return /* one of the cashiers */
}

customer.check_ out(least_busy_ cashier(cashier s));
Should it be 'map of
lists of pointers to classes containing a struct', or 'map of pointers
to classes containing lists of structs'?


I don't see any reason for pointers in what you described.

Ali

Oct 21 '05 #7

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

Similar topics

4
1697
by: Asapi | last post by:
class base { }; class d1 : public base { }; class d2 : public base {
2
2408
by: Barry Hynes | last post by:
G'Day folks, Have been working on this problem for quite some time and still no farther ahead. :( Here is my problem...bare with me i am very green :) I have to implement a Safe List, that is derived from the STL class List and uses exception handling.
2
2356
by: Jim Strathmeyer | last post by:
I have a weird question about const correctness when using an stl list. I have a wrapper Inventory class that holds a list of pointers to Items. (Yes, they have to be pointers.) Now, obviously the Inventory class isn't going to mutate the Items, so its Add function should be Add(const Item *), and the list should be std::list<const Item *>. One way to access the Inventory's item's is to iterate through them with: std::list<const Item...
4
2881
by: Christian Christmann | last post by:
Hi, I need an STL list and was thinking of putting the list in wrapper class. The reason for my decision is that you can much better perform consistency checks. For instance, I need a function to append a list to another. Using a wrapper class I could do something like:
8
4896
by: cpptutor2000 | last post by:
I am using an STL list to store data packets in a network simulator. The data packets are really C structs, which have other C structs inside them. These structs contain unsigned char arrays of various sizes, and some unsigned int values. There are no pointers values stored in these structs. I have a function, from inside of which I have a loop as follows: Please note that dataPacketStore is the name of the struct being stored. ...
5
4603
by: Allerdyce.John | last post by:
In STL list, is it safe to do this: list<A> aList; //private attribute of MyClass void MyClass:: aMethod() { list<A>::Iterator iter; for ( iter = aList.begin() ; iter != aList.end() ; iter++) { A a = (*iter);
6
2965
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter = room->living.begin(); while (iter != room->living.end()) {
3
2725
by: Christian Christmann | last post by:
Hi, reading the output of gprof for one of my projects, I found that the STL list assignment operator consumes a larger fraction of the program's execution time. The exact entry in gprof's output looks as follows: std::list<MyClass*, std::allocator<MyClass*::operator= (std::list<MyClass*, std::allocator<MyClass* const&) >
27
2771
by: Jason Doucette | last post by:
I'm getting an assertion fire from a list iterator being checked against NULL. This did not occur in VC++ 2003 (v7.1). Are there changes that have been made to the STL between these versions that I should know about? Any resources I should check into? thanks, Jason
2
4099
by: harsh.murari | last post by:
I wanted to know how the C++ STL list is implemented. Specifically, I wanted to understand the memory usage by the list objects. I have the following code snippet: std::list <intL; L.push_back(10); L.push_back(20);
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10037
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.