Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

looking for something like a std::map

Posted on 2001-09-11
9
664 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
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 how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

856 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