Jump to content
  • Advertisement
Sign in to follow this  
toddhd

Hash or dictionary for C++?

This topic is 5056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'd like to know if C++ has some sort of key/value function, such as a hash or dictionary object? I am familiar with arrays and vectors, but these don't support the key/value function I need. Also, if it supports it, I need it to be dynamic, like a vector object. That is, if I can add to the table without having to know how large it will be ahead of time, that would be a huge plus. Thanks

Share this post


Link to post
Share on other sites
Advertisement
#include <map>
std::map<KeyType,ValueType> a;
std::multimap<KeyType,ValueType> b; // duplicate keys allowed


hash_map and hash_multimap are a non-standard but commonly provided extension (look for a hash_map header), based on hash tables instead of balanced binary trees.

Share this post


Link to post
Share on other sites
There are the classes "map" and "multimap" on the STL that creates pairs key/value.

Map supports only one value per key.
Multimap supports more than one value per key.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfmultimap_class.asp

Example codes:


// Multimap
#include <iostream>

using std::cout;
using std::endl;

#include <map>

int main()
{
// key is int, associated value is double, sort by less
typedef std::multimap< int, double, std::less< int > > mmid;
mmid pairs;

cout << "There are currently " << pairs.count( 15 )
<< " pairs with key 15 in the multimap\n";
pairs.insert( mmid::value_type( 15, 2.7 ) );
pairs.insert( mmid::value_type( 15, 99.3 ) );
cout << "After inserts, there are "
<< pairs.count( 15 )
<< " pairs with key 15\n";
pairs.insert( mmid::value_type( 30, 111.11 ) );
pairs.insert( mmid::value_type( 10, 22.22 ) );
pairs.insert( mmid::value_type( 25, 33.333 ) );
pairs.insert( mmid::value_type( 20, 9.345 ) );
pairs.insert( mmid::value_type( 5, 77.54 ) );
cout << "Multimap pairs contains:\nKey\tValue\n";

for ( mmid::const_iterator iter = pairs.begin();
iter != pairs.end(); ++iter )
cout << iter->first << '\t'
<< iter->second << '\n';

cout << endl;

system("pause");
return 0;
}

// MAP
#include <iostream>

using std::cout;
using std::endl;

#include <map>

int main()
{
typedef std::map< int, double, std::less< int > > mid;
mid pairs;

pairs.insert( mid::value_type( 15, 2.7 ) );
pairs.insert( mid::value_type( 30, 111.11 ) );
pairs.insert( mid::value_type( 5, 1010.1 ) );
pairs.insert( mid::value_type( 10, 22.22 ) );
pairs.insert( mid::value_type( 25, 33.333 ) );
pairs.insert( mid::value_type( 5, 77.54 ) ); // dupe ignored
pairs.insert( mid::value_type( 20, 9.345 ) );
pairs.insert( mid::value_type( 15, 99.3 ) ); // dupe ignored
cout << "pairs contains:\nKey\tValue\n";

mid::const_iterator iter;

for ( iter = pairs.begin(); iter != pairs.end(); ++iter )
cout << iter->first << '\t'
<< iter->second << '\n';

pairs[ 25 ] = 9999.99; // change existing value for 25
pairs[ 40 ] = 8765.43; // insert new value for 40
cout << "\nAfter subscript operations, pairs contains:"
<< "\nKey\tValue\n";

for ( iter = pairs.begin(); iter != pairs.end(); ++iter )
cout << iter->first << '\t'
<< iter->second << '\n';

cout << endl;

system("pause");
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!