Solved

looking for something like a std::map

Posted on 2001-09-11
9
661 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
Comment Utility
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
Comment Utility
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
Comment Utility
Didn't see your comment there Axter.
0
 

Author Comment

by:duncanlatimer
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:duncanlatimer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
MFC has a CMap class
0
 

Author Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

17 Experts available now in Live!

Get 1:1 Help Now