CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Jan 2012
    Posts
    49

    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!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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

  3. #3
    Join Date
    Jan 2012
    Posts
    49

    Re: best std container class for my application?

    Every instance may not be unique...

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    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.

  5. #5
    Join Date
    Jan 2012
    Posts
    49

    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.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: best std container class for my application?

    Quote Originally Posted by aseminov View Post
    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

  7. #7
    Join Date
    Jan 2012
    Posts
    49

    Re: best std container class for my application?

    so basically you are sarcastic in general and not helpful?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: best std container class for my application?

    Quote Originally Posted by aseminov View Post
    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

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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 ?

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    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

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: best std container class for my application?

    From the way you've described the requirements, multimap sounds like the best container.

  12. #12
    Join Date
    May 2009
    Posts
    2,413

    Re: best std container class for my application?

    Quote Originally Posted by GCDEF View Post
    From the way you've described the requirements, multimap sounds like the best container.
    If you listen again unordered_multimap sounds even better.

  13. #13
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: best std container class for my application?

    Quote Originally Posted by GCDEF View Post
    From the way you've described the requirements, multimap sounds like the best container.
    Quote Originally Posted by nuzzle View Post
    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.

  14. #14
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: best std container class for my application?

    Quote Originally Posted by aseminov View Post
    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.

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: best std container class for my application?

    Quote Originally Posted by monarch_dodra View Post
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured