473,395 Members | 1,972 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.

container within a container issue: set in the map

Guys, what is more preferable, given that I don't have access to boost
or shared pointers:

std::map<T*, std::set<T2** *_myMap;

vs.

std::map<T*, std::set<T2* *myMap;

Notice that in the latter, containing set is not a pointer. Could
there be a drawbacks with the former, like copying over when trying to
insert elements into it?
Thanks
Sep 19 '08 #1
8 1248
puzzlecracker wrote:
Guys, what is more preferable, given that I don't have access to boost
or shared pointers:

std::map<T*, std::set<T2** *_myMap;

vs.

std::map<T*, std::set<T2* *myMap;

Notice that in the latter, containing set is not a pointer. Could
there be a drawbacks with the former, like copying over when trying to
insert elements into it?
Not enough data. For starters:

a) What is wrong with no pointers:

std::map< T, std::set<T2 myMap;

b) What are typical instances for T and T2?

c) Are you sure that you don't want to use custom comparison predicates? and
related: can your pointers be null? and how do they arise anyway?
Best

Kai-Uwe Bux
Sep 19 '08 #2
On Sep 19, 5:52 am, puzzlecracker <ironsel2...@gmail.comwrote:
Guys, what is more preferable, given that I don't have access
to boost or shared pointers:
std::map<T*, std::set<T2** *_myMap;
vs.
std::map<T*, std::set<T2* *myMap;
Notice that in the latter, containing set is not a pointer.
Could there be a drawbacks with the former, like copying over
when trying to insert elements into it?
I'll second Kai-Uwe's comments, but with regards to the question
about copying: the node based containers (list, set and map)
never copy an object once it is in the container. More
generally, check the container specifications: if insert and
erase don't invalidate references and pointers into the
container (except for references or pointers to the erased
element), then copying can't take place.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 19 '08 #3
On Sep 19, 5:50 pm, James Kanze <james.ka...@gmail.comwrote:
[snip]
generally, check the container specifications: if insert and
erase don't invalidate references and pointers into the
container (except for references or pointers to the erased
element), then copying can't take place.
I'm confident this is true for all STL implementations, but would just
make the point that iterators in general are free to do things like
keep a pointer to the container and an offset, and their on-going
validity may not guarantee copying hasn't taken place. Not sure of
the Standard's actual requirements, but I hope none of us are bored
enough to care....

Tony
Sep 19 '08 #4
puzzlecracker wrote:
std::map<T*, std::set<T2** *_myMap;
I would remove all the asterisks from there, or at the very least,
replace them with some suitable smart pointer. That kind of code is
*very* hard to make safe because you need to perform quite a lot of
manual memory management.

(And do you really want to sort the elements by a *pointer* to T
rather than the *value* of T? It is possible, but oftentimes perhaps not
the best of ideas. It can cause all kinds of logistical problems, unless
you really know what you are doing and it's something you explicitly
want to do.)
Sep 19 '08 #5
On Sep 19, 6:24 am, Juha Nieminen <nos...@thanks.invalidwrote:
puzzlecracker wrote:
std::map<T*, std::set<T2** *_myMap;

I would remove all the asterisks from there, or at the very least,
replace them with some suitable smart pointer. That kind of code is
*very* hard to make safe because you need to perform quite a lot of
manual memory management.

(And do you really want to sort the elements by a *pointer* to T
rather than the *value* of T? It is possible, but oftentimes perhaps not
the best of ideas. It can cause all kinds of logistical problems, unless
you really know what you are doing and it's something you explicitly
want to do.)
I would like to use smart pointers but, as I said, I cannot access
fancy library, and auto_ptr is obviously not suitable here.
Sep 19 '08 #6
On Sep 19, 11:45 am, tony_in_da...@yahoo.co.uk wrote:
On Sep 19, 5:50 pm, James Kanze <james.ka...@gmail.comwrote:
[snip]
generally, check the container specifications: if insert and
erase don't invalidate references and pointers into the
container (except for references or pointers to the erased
element), then copying can't take place.
I'm confident this is true for all STL implementations, but
would just make the point that iterators in general are free
to do things like keep a pointer to the container and an
offset, and their on-going validity may not guarantee copying
hasn't taken place.
That's probably the most common implementation, in fact. And it
does mean that iterators may remain valid despite copying. But
if you reread what I wrote, I said to check whether pointers and
references remain valid. No matter how the iterator is
implemented, if a pointer to the element remains valid, then the
element can't have been copied.
Sep 19 '08 #7
On Sep 19, 1:05 pm, puzzlecracker <ironsel2...@gmail.comwrote:

[...]
I would like to use smart pointers but, as I said, I cannot
access fancy library, and auto_ptr is obviously not suitable
here.
Smart pointers don't necessarily imply a fancy library; I think
my original implementation of RefCntPtr was only about 50 lines
(and in many cases, it's still preferable to
boost::shared_ptr---simplicity can be more important than
flexibility).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 19 '08 #8
On Sep 20, 5:33 am, James Kanze <james.ka...@gmail.comwrote:
On Sep 19, 11:45 am, tony_in_da...@yahoo.co.uk wrote:
On Sep 19, 5:50 pm, James Kanze <james.ka...@gmail.comwrote:
[snip]
generally, check the container specifications: if insert and
erase don't invalidate references and pointers into the
container (except for references or pointers to the erased
element), then copying can't take place.
I'm confident this is true for all STL implementations, but
would just make the point that iterators in general are free
to do things like keep a pointer to the container and an
offset, and their on-going validity may not guarantee copying
hasn't taken place.

That's probably the most common implementation, in fact. And it
does mean that iterators may remain valid despite copying. But
if you reread what I wrote, I said to check whether pointers and
references remain valid. No matter how the iterator is
implemented, if a pointer to the element remains valid, then the
element can't have been copied.
So you did. Sorry. - Tony
Sep 21 '08 #9

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

Similar topics

41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
3
by: jignesh shah | last post by:
Hi all, Is there a way to recover a single container if its been corrupted or mark bad without restoring whole tablespace? environment: db28.1/aix5.1/tsm/rs-6000. Regards Jignesh
4
by: Mitchel Haas | last post by:
Hello, Feeling a need for a generic tree container to supplement the available containers in the STL, I've created a generic tree container library (TCL). The library usage is very much like...
4
by: rn5a | last post by:
When I do this: <asp:DataList ID="dlProducts" runat="server"> <HeaderTemplate> <table border="0"> <tr> <td>Category</td> <td><%# Container.DataItem("Category") %></td> </tr> </table>
6
by: Greg | last post by:
Is there a simple way to test if a control is a container type control (e.g. groupbox, panel etc) I realise that all container controls have to by definition, have the...
4
by: Doc John | last post by:
I have a "global" MDI which will contain everything in the application. Since the application itslef will include three different "environments" with their own child Forms, I was wondering if I...
5
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
2
by: Jay Dee | last post by:
I have created a container that will position 4 panels that has 15 different layouts to choose from. I have based it similar to a System.Windows.Forms.SplitContainer but that has 4 panels...
12
nathj
by: nathj | last post by:
Hi, I have been working on a redesign of a site. this is a site that was developed by someone else and the CSS has been built to fit with the CMS that they supplied. This redesign is nearly...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
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.