M
mscava
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<MyClass>
- 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<MyClass*>
+ 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<MyClass> >
* 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.
Containers. I've found 3 appraoches but all do have pros and cons...
1. std::vector<MyClass>
- 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<MyClass*>
+ 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<MyClass> >
* 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.