473,700 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Containers & Pointers, Discussion

Hi... Today I was trying to find the best way to handle elements in
Containers. I've found 3 appraoches but all do have pros and cons...

1. std::vector<MyC lass>
- inefficient
- you have to write assing an copy constructor
- to add new element into container you do need 2 lines of code
+ very comfortable use of algorhitms from STL

2. std::vector<MyC lass*>
+ efficient
+ only one line of code when adding new element
- problems with removing elements, you have to delete them first
from memory.

3. std::vector< CountedPtr<MyCl ass
* CountedPtr is a smart ptr, that tracks references of a pointer
+ efficient
+ only one line of code when adding new element
+ no problems with deleting pointers (will delete itself on loosing
reference count
- i'm not able to find a way to use STL algorhitms like for_each,
mem_fun ...

I feel like the third approach is the best I can use, the problem is
it has one big disadvantage I mentioned. I'd be really glad if someone
knew how to bypass it..

Well, anyway, if someone has any suggestions, advices, just tell me,
I'd really like to know what is the best way to store elements in
Containers.

Jun 5 '07 #1
7 1468
ms****@gmail.co m wrote:
Hi... Today I was trying to find the best way to handle elements in
Containers. I've found 3 appraoches but all do have pros and cons...

1. std::vector<MyC lass>
- inefficient
- you have to write assing an copy constructor
- to add new element into container you do need 2 lines of code
+ very comfortable use of algorhitms from STL

2. std::vector<MyC lass*>
+ efficient
+ only one line of code when adding new element
- problems with removing elements, you have to delete them first
from memory.

3. std::vector< CountedPtr<MyCl ass
* CountedPtr is a smart ptr, that tracks references of a pointer
+ efficient
+ only one line of code when adding new element
+ no problems with deleting pointers (will delete itself on loosing
reference count
- i'm not able to find a way to use STL algorhitms like for_each,
mem_fun ...

I feel like the third approach is the best I can use, the problem is
it has one big disadvantage I mentioned. I'd be really glad if someone
knew how to bypass it..

Well, anyway, if someone has any suggestions, advices, just tell me,
I'd really like to know what is the best way to store elements in
Containers.
You can use approach 2. Add a deletion policy to MyClass that can
intelligently delete the pointer depending on a condition. For example:

template <template <typename Tclass deletion_policy >
class MyClass{
};

template <typename T>
class deletion_policy {
};
MyClass<deletio n_policy<condit ion m;
Jun 5 '07 #2
ms****@gmail.co m wrote:
3. std::vector< CountedPtr<MyCl ass
* CountedPtr is a smart ptr, that tracks references of a pointer
+ efficient
yes, it depends on what you mean. you can pass the vectors by reference,
and the vector<Objectwo n't be copied. Additionally, using a
vector<Object*o r any other pointer type, you usually need to
separately allocate each object independently, which can become very
expensive for large numbers of small objects. For example, it's not
possible to store the pixels of an image in a vector<Pixel*>. ..
+ only one line of code when adding new element
+ no problems with deleting pointers (will delete itself on loosing
reference count
- i'm not able to find a way to use STL algorhitms like for_each,
mem_fun ...
I can't understand why... you can do something like this, for example:

#include <boost/shared_ptr.hpp>
#include <vector>
#include <iostream>
#include <algorithm>

struct print_element
{
template <typename T>
void operator()(cons t T& o){ std::cout << *o << " "; }
};

int main()
{
std::vector<boo st::shared_ptr< int v;
v.push_back(boo st::shared_ptr< int>(new int(3)));
v.push_back(boo st::shared_ptr< int>(new int(4)));

std::for_each(v .begin(), v.end(), print_element() );
}

what problem are you experiencing with function objects?
>
I feel like the third approach is the best I can use, the problem is
it has one big disadvantage I mentioned. I'd be really glad if someone
knew how to bypass it..
as usual,it depends on the context in which you are using the approach.
Surely enough, there are a lot of situation in which the third approach
is very advantageous..
Well, anyway, if someone has any suggestions, advices, just tell me,
I'd really like to know what is the best way to store elements in
Containers.
Forget about a general best way ;)

Regards,

Zeppe

Jun 5 '07 #3
Thanks Zeppe..
what problem are you experiencing with function objects?
std::for_each( players_.begin( ), players_.end(),
std::mem_fun( &MyClass::Fo o ) );

Am I able to do this? My compiler throws errors..
And to Fei Liu, I'm not very into the templates, so I don't understand
what you mean..

Jun 5 '07 #4
ms****@gmail.co m wrote:
Thanks Zeppe..
>what problem are you experiencing with function objects?

std::for_each( players_.begin( ), players_.end(),
std::mem_fun( &MyClass::Fo o ) );

Am I able to do this? My compiler throws errors..
And to Fei Liu, I'm not very into the templates, so I don't understand
what you mean..
It's going to be difficult then, because when you start using STL, you
are in the templates...AFA IK you shouldn't have any problem with
approach 3 as long as the smart pointer provides 'correct' copy behavior
per your application.

Fei
Jun 5 '07 #5
ms****@gmail.co m wrote:
Thanks Zeppe..
>what problem are you experiencing with function objects?

std::for_each( players_.begin( ), players_.end(),
std::mem_fun( &MyClass::Fo o ) );

Am I able to do this? My compiler throws errors..
I would suggest you to post a small code example the next time, because,
you know, it saves some time to us to see the problem you are
experiencing faster.

Anyway, you're actually right, mem_fun is not able to solve the template
matching for shared_ptr<Foo> ... he expects Foo*. If you are using the
boost library, you can use the boost::mem_fn function that overcomes
this problem. If you are using your own reference counted pointer, you
have got to write your own mem_fun (at a glance, it shouldn't be too
difficult). Consider this example:

#include <boost/shared_ptr.hpp>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <boost/mem_fn.hpp>

class Player
{
public:
Player(const std::string& name) : name_(name) { }
void printName() {
std::cout << "name: " << name_ << std::endl;
}
private:
std::string name_;
};

int main()
{
std::vector<boo st::shared_ptr< Player players;
players.push_ba ck(boost::share d_ptr<Player>(
new Player("Th4Ebil Kill4")));
players.push_ba ck(boost::share d_ptr<Player>(
new Player("Muffin Man")));

std::for_each( players.begin() , players.end(),
boost::mem_fn( &Player::printN ame ) );

}

>
And to Fei Liu, I'm not very into the templates, so I don't understand
what you mean..
well, i'm (a little bit) more in the template maybe, and that code seems
broken :) It declares a template template argument, that is a way to
pass a template as a template argument without being constrained to
instantiate it as a concrete type before. But in the example this is done.

Anyway, it's not possible to embed a deletion policy inside of the
object itself that is going to be allocated, because i doesn't know
about how it has been allocated. The solution 3 is the best one, if you
want a vector of pointer objects (whose greater advantage is to allow
polymorphic containers, more than performances, in my opinion) and you
want to get rid of the deallocation (which is a good idea, provided that
the container is actually the owner of the object, in a design sense).

Regards,

Zeppe
Jun 5 '07 #6
Zeppe wrote:
>
>>
And to Fei Liu, I'm not very into the templates, so I don't understand
what you mean..

well, i'm (a little bit) more in the template maybe, and that code seems
broken :) It declares a template template argument, that is a way to
pass a template as a template argument without being constrained to
instantiate it as a concrete type before. But in the example this is done.

Anyway, it's not possible to embed a deletion policy inside of the
object itself that is going to be allocated, because i doesn't know
about how it has been allocated. The solution 3 is the best one, if you
It's possible but I think there is misunderstandin g on my part about the
original problem. It's a question of whether not how about the deletion.
It's enough that MyClass have a property to indicate if a MyClass
object should be deleted before removed from container.
Jun 5 '07 #7
Fei Liu wrote:
Zeppe wrote:
>>
>>>
And to Fei Liu, I'm not very into the templates, so I don't understand
what you mean..

well, i'm (a little bit) more in the template maybe, and that code
seems broken :) It declares a template template argument, that is a
way to pass a template as a template argument without being
constrained to instantiate it as a concrete type before. But in the
example this is done.

Anyway, it's not possible to embed a deletion policy inside of the
object itself that is going to be allocated, because i doesn't know
about how it has been allocated. The solution 3 is the best one, if you

It's possible but I think there is misunderstandin g on my part about the
original problem. It's a question of whether not how about the deletion.
It's enough that MyClass have a property to indicate if a MyClass
object should be deleted before removed from container.
I really can't understand how a MyClass object can understand when a
pointer to it is being removed from a container. Can you give me a
little example code to explain the concept?

regards,

Zeppe
Jun 5 '07 #8

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

Similar topics

6
3371
by: Gandalf | last post by:
Hello. I have some questions about the standard containers. How does the standard containers behave if I do queue<Foo> myQ; queue<Foo> myQ2; .... insert into myQ... myQ = myQ2;
0
625
by: Wolfgang Lipp | last post by:
From: Lipp, Wolfgang Sent: Tuesday, 27?January?2004 13:26 <annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with the consent of their authors. </annotation>
12
1807
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The standard says that empty() has constant complexity. If it actually called the destructor for each object, it seems to me it would have linear complexity. Does empty() call the destructor for each object in the container? If yes, why is it described...
6
1513
by: Ares Lagae | last post by:
Hello, I am trying to create a container the stl way, and I have a couple of questions. The code of the container in question can be found at http://www.cs.kuleuven.ac.be/~ares/tmp/array_2_hpp.html It is a 2d array that dynamically allocates its storage. It cannot be resized. It offers element access methods for both sequential access and 2d element access.
14
8208
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At present the shared memory contains a few fixed-size structs, but I really need to be able to store more complex variable-sized data in there. So the next task is to work out how to store C++ objects, and if possible STL containers, in this shared...
18
3052
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary containers and sort those (only pointers have to be copied around then). However, that also means if I...
15
4082
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL containers destructors virtual >
21
2208
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not necessarily the same... class base { int x;
14
1640
by: Remo D. | last post by:
I've read with much interest the threads on how to create data containers in C. I started thinking if simpler ADT (other than the proposed List) could be used as a starting point. What about a dynamic vector? I mean a dynamic array with no predefined size. A possible API could be: typedef struct {
0
8726
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9214
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...
0
9075
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8925
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...
1
6561
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5903
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4404
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...
0
4657
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.