Solved

looking for something like a std::map

Posted on 2001-09-11
9
665 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallā€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called eeā€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

749 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