Solved

looking for something like a std::map

Posted on 2001-09-11
9
662 Views
Last Modified: 2013-12-14
I am looking for a stl class which is like a std::map, but which doesn't sort the elements, i.e. like a vector of pairs, but where you can do a find like you can with a std::map. Obviously I am aware that I could write a class that is a vector of pairs with a find method, however there is probably a stl class that does exactly what I want already.
0
Comment
Question by:duncanlatimer
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6474342
You can try using a hash_map class.
hash_map is not part of the official C++ standards, but you can download a free copy of the class if you're intererested.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6474362
I don't think there is such a thing.

Maybe multimap is what you want, it allows you to have multiple values with the same key.  It is sorted, however.

If sorting is a problem you could also try the hash associative container that is part of the SGI (and STLport) implementation of the STL.

See: http://www.sgi.com/tech/stl/HashedAssociativeContainer.html

for details and www.stlport.org for the stlport download.

However, you should note that the entire point of these associative containers is to provide fast access by key value - so the classes need some way to store key values for efficient lookup.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6474367
Didn't see your comment there Axter.
0
 

Author Comment

by:duncanlatimer
ID: 6474420
As the order of the elemets in a hash_map are not guaranteed this will probably not work for me. I need to be able to retrieve the elements in the same order that I loaded them. And I also need to be able to do a find on a key.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:duncanlatimer
ID: 6474444
I envisage that the container I am after has two different types of iterators, one type providing sorted access for finds etc, the other providing load sequence order access.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6474448
The solution may well depend then on what your main concern is.  If speed of lookup is very important then you might consider storing maybe a vector and a map, maybe they could contain pointers to stored data structures, or maybe the vector could just contain key values.

i.e. something like:

vector<string> keyVector;
map<string,value>  mymap;

then you could implement a 'get by index' something like this:

value getByIndex(size_t index) const
{
    return mymap[keyVector[index]];
}

(you might want to change it add some error checking!).
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6474459
You can use a vector pair, and search for your data using the find_if function with a Predicate.

Example code:
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <algorithm>

class FindName {
public:
     FindName(std::string Name) : m_Name(Name){}
     bool operator()(const std::pair<std::string,std::string> &Src)
     {
          return (Src.first == m_Name);
     }
     std::string m_Name;
};

int main(int argc, char* argv[])
{
     std::vector<std::pair<std::string,std::string> > myset;
     myset.push_back(std::pair<std::string,std::string>("Sue", "girl"));
     myset.push_back(std::pair<std::string,std::string>("Tom", "boy"));
     myset.push_back(std::pair<std::string,std::string>("Kim", "girl"));
     myset.push_back(std::pair<std::string,std::string>("Jack", "boy"));
     myset.push_back(std::pair<std::string,std::string>("Betty", "girl"));
     myset.push_back(std::pair<std::string,std::string>("Jill", "girl"));
     myset.push_back(std::pair<std::string,std::string>("Bob", "boy"));
     myset.push_back(std::pair<std::string,std::string>("David", "boy"));
     myset.push_back(std::pair<std::string,std::string>("Candy", "girl"));
     std::vector<std::pair<std::string,std::string> >::iterator  who =
          std::find_if(myset.begin(),myset.end(),FindName("David"));
     if (who != myset.end())
     {
          std::string MaleOrFemale = who->second;
          std::cout << "David is a " << MaleOrFemale << std::endl;
     }
     return 0;
}


0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6474509
MFC has a CMap class
0
 

Author Comment

by:duncanlatimer
ID: 6474567
I am trying to avoid MFC and Axter's solution is exactly what I need so...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now