Solved

looking for something like a std::map

Posted on 2001-09-11
9
663 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is Codeanywhere used for and can EE provide example of use 9 214
is twain_32.dll cmpatible with windows 10 ? 10 163
Dialogbox API leak? 18 96
Why isn't object file created? 6 59
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 perform CRUD operations on a MySql database.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

810 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