|
-
January 16th, 2012, 02:49 PM
#1
best std container class for my application?
Hello,
I have a class called CTestObject and it has one variable int id. I want to add instances of this to an std container and be able to:
1. quickly determine if the value of "int id" exists in the container and update the object
or
2. add a new object to the container
Which std container is the best for this application? What method should I use to search the container for the object id?
Thanks!
-
January 16th, 2012, 03:06 PM
#2
Re: best std container class for my application?
Do you mean that every instance of CTestObject class is unique (has a unique int id)?
Have you implemented a copy ctor ans assignment operator to support object uniqueness?
Victor Nijegorodov
-
January 16th, 2012, 03:12 PM
#3
Re: best std container class for my application?
Every instance may not be unique...
-
January 16th, 2012, 03:15 PM
#4
Re: best std container class for my application?
Do you want to add a new object in the middle of the container?
If yes, you could consider deque std: eque or std::list. Otherwise I would suggest std::vector. For 1. you would could do a simple loop over the entire container to look for the id. The efficiency could be improved by keeping the container sorted, but then...
There are containers that will do the sorting for you as soon as elements are added:
std::map and std::set are 'automatically' sorted, but they only keep unique keys/elements resp.. If you dont want to keep duplicate CTestObjects, then std::set will work well for you. If you do need to consider duplicates, then look at std::multimap, or suggestions in previous paragraph.
If you use one of the former suggestions, you could use std::find, or manually loop over the container yourself. Or sort and then use binary_search.
If you use a map or a set, you can use their member method find(...)
Last edited by Amleto; January 16th, 2012 at 03:19 PM.
-
January 16th, 2012, 03:40 PM
#5
Re: best std container class for my application?
Ok so here is a quick example I made:
Code:
#include <vector>
using namespace std;
class Test
{
public:
Test(int id) { id_=id; };
~Test() {};
void UpdateTest(Test* update) { value_=update->value_; };
int id_;
int value_;
};
int main()
{
vector<Test*> testvec;
Test* test1 = new Test(1);
Test* test2 = new Test(2);
Test* test3 = new Test(3);
testvec.push_back(test1);
testvec.push_back(test2);
testvec.push_back(test3);
vector<Test*>::iterator it;
Test* findtest = new Test(2);
it=find(testvec.begin(), testvec.end(), findtest->id_);
if(it != testvec.end())
{
(*it)->UpdateTest(findtest);
delete findtest;
}
else
testvec.push_back(findtest);
// cleanup
delete test1;
delete test2;
delete test3;
}
But I get a compilation error:
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(41): error C2446: '==' : no conversion from 'const int' to 'Test *'
I think I need to use find_if(). Can anyone show me an example of it with respect to what I am trying to accomplish?
Thanks!
Last edited by aseminov; January 16th, 2012 at 03:58 PM.
-
January 16th, 2012, 03:42 PM
#6
Re: best std container class for my application?
 Originally Posted by aseminov
O...
But I get a compilation error on the if(it) line. How am I suppossed to check if find() returned a match or not?
The first thing you must do is to fix the "compilation error"!
Victor Nijegorodov
-
January 16th, 2012, 03:47 PM
#7
Re: best std container class for my application?
so basically you are sarcastic in general and not helpful?
-
January 16th, 2012, 04:00 PM
#8
Re: best std container class for my application?
 Originally Posted by aseminov
so basically you are sarcastic in general and not helpful?
It would be much more helpful if you showed the exact error message and the line where it happened.
Victor Nijegorodov
-
January 16th, 2012, 04:04 PM
#9
Re: best std container class for my application?
1) There is no reason to use vector<Test*> . Store objects instead.
2) How many elements do you expect to have ? If there are many
elements, a linear search (i.e. std::find) is slow
3) You mention the non-uniqueness, when you are searching for an
element and want to update it, which element would you choose ?
-
January 16th, 2012, 04:04 PM
#10
Re: best std container class for my application?
find -- Look at the docs for find. What are you comparing here (the error message says):
Code:
it=find(testvec.begin(), testvec.end(), findtest->id_);
Viggy
-
January 16th, 2012, 04:25 PM
#11
Re: best std container class for my application?
From the way you've described the requirements, multimap sounds like the best container.
-
January 17th, 2012, 01:45 AM
#12
Re: best std container class for my application?
 Originally Posted by GCDEF
From the way you've described the requirements, multimap sounds like the best container.
If you listen again unordered_multimap sounds even better.
-
January 17th, 2012, 03:13 AM
#13
Re: best std container class for my application?
 Originally Posted by GCDEF
From the way you've described the requirements, multimap sounds like the best container.
 Originally Posted by nuzzle
If you listen again unordered_multimap sounds even better.
He wants update if already present, so... an (unordered_)map would be best, no?
EDIT:
Or why not just an (unordered_)set? If the key is in value...?
Last edited by monarch_dodra; January 17th, 2012 at 03:25 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 17th, 2012, 03:24 AM
#14
Re: best std container class for my application?
 Originally Posted by aseminov
it=find(testvec.begin(), testvec.end(), findtest->id_);[/CODE]
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(41): error C2446: '==' : no conversion from 'const int' to 'Test *'
I think I need to use find_if().
You are correct: Your container contains pointers to object, yet you are looking for an integer.
There are two solutions:
-The bad one: Make your test implicitly constructable from an integer. This may lead to some unexpected behavior if you are not careful. I really shouldn't even be mentioning this method.
The second solution would be to learn about functors! A funtor is like a function, but it is actually an object that can have a state. To "call the functor", you actually call that object's operator(). Here is an example of a functor that can be instanciated with a specific int, and then can be called to print that int:
Code:
#include <iostream>
class printer_functor
{
public:
printer_functor(int i) : i(i){}
void operator()()
{std::cout << i;}
private:
int i;
};
int main()
{
printer_functor p(5); //Freate a functor called "a" initialized with 5
p(); //"call" a
}
In your case, you'd want to use a functor like this:
Code:
class Compare_Test_Id
{
public:
Compare_Test_Id(int i) : i(i){}
bool operator()(const Test* i_other)
{return i == i_other->id_;}
private:
int i;
};
...
int main()
{
...
it = find_if(testvec.begin(), testvec.end(), Compare_Test_Id(findtest->id_));
...
}
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 17th, 2012, 04:26 AM
#15
Re: best std container class for my application?
 Originally Posted by monarch_dodra
He wants update if already present, so... an (unordered_)map would be best, no?
This depends on how you interpret the information in post #3.
If it means keys are not unique then an unordered_multimap is best, otherwise an unordered_map.
Or why not just an (unordered_)set? If the key is in value...?
That depends on how the information is "packaged" when used with the container.
If using an unordered set/multiset results in an unnecessary creation of a CTestObject object just to access the container then the unordered map/multimap alternative is better, otherwise not.
Last edited by nuzzle; January 17th, 2012 at 04:30 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|