Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please join our community today!

[C++] TR1 Unordered_Map

Display your little helpful snippets of code that may help us all get something done

[C++] TR1 Unordered_Map

Postby Vevix » Sat Nov 14, 2009 4:10 pm

Code: Select all
#include <iostream>
#include <string>
#include <unordered_map>

int main()
{
   // TR1 unordered_map Example, An unordered_map is exactly that
   // an STL container that is unordered
   
   // Create our map, The key will be an integer value and each key will hold a string value
   typedef std::tr1::unordered_map<int, std::string> NewMap;
   NewMap TestMap;

   // Let's add some data, Method 1
   TestMap.insert(std::pair<int, std::string>(0, "Hello"));
   // More Data, Method 2
   TestMap[1] = "World";

   // Let's print out the map
   for (NewMap::const_iterator It = TestMap.begin(); It != TestMap.end(); ++It)
   {
      // Print both the key and the value of that key
      // First = Key (Int), Second = Value of that key (String)
      std::cout << (*It).first << " equals " << (*It).second << std::endl;
   }

   return 0;
}


Output:
Code: Select all
0 equals Hello
1 equals World
Press any key to continue . . .


It's pretty much self explanatory code, It works like any other container you know (Vector etc.) just it has a key with a corresponding value - You can expand on the value by using a struct, so each key could have multiple values/variables.

You could also have the key as a string and then retrieve data that you need by a name rather than looping thru each member until you find the one you want making life easier.
Think Globally. Act Within Local Variable Scope.
User avatar
Vevix
teh awesome
 
Posts: 535
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll

 

Re: [C++] TR1 Unordered_Map

Postby vEjEsE » Fri Mar 12, 2010 3:04 pm

Is there any way to check if the Key exists? Or the program wont crash if I use a key that dosen't exist?

I may want to do something like this:
Code: Select all
unordered_map<int, int> Map;
Map.insert(1, 1);
Map.insert(2, 2);

if ( Map.checkkey(3) ) // assuming it returns true if the key exists
    Map.erase(3);
"The generation of random numbers is too important to be left to chance."
User avatar
vEjEsE
teh awesome
 
Posts: 515
Joined: Mon Aug 03, 2009 12:48 am

Re: [C++] TR1 Unordered_Map

Postby Vevix » Fri Mar 12, 2010 8:37 pm

Well one way would be just to check the count.

Code: Select all
if ( Map.count( Key ) > 0 ) then
    do stuff
Think Globally. Act Within Local Variable Scope.
User avatar
Vevix
teh awesome
 
Posts: 535
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll

Re: [C++] TR1 Unordered_Map

Postby vEjEsE » Fri Mar 12, 2010 9:42 pm

Good point, but how would that return something like more than 0? There are some concepts I must understand. But why use unordered_map if there is no check of a key
"The generation of random numbers is too important to be left to chance."
User avatar
vEjEsE
teh awesome
 
Posts: 515
Joined: Mon Aug 03, 2009 12:48 am

Re: [C++] TR1 Unordered_Map

Postby Vevix » Fri Mar 12, 2010 9:49 pm

That is a check, Keys are unique so there can only be 0 or 1, 1 being exists.
Last edited by Vevix on Fri Mar 12, 2010 10:19 pm, edited 1 time in total.
Think Globally. Act Within Local Variable Scope.
User avatar
Vevix
teh awesome
 
Posts: 535
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll

Re: [C++] TR1 Unordered_Map

Postby vEjEsE » Fri Mar 12, 2010 10:19 pm

Some indepth information? Or is that the only satisfying information you can give? I don't really undestand how those kind of "keys are unique so they're can only be 0 or 1" thing works. I undestand how it may work, but... Don't think it's satisfying to know as a programmer.

Thanks in advanced, the unordered_map help alot in my project.
"The generation of random numbers is too important to be left to chance."
User avatar
vEjEsE
teh awesome
 
Posts: 515
Joined: Mon Aug 03, 2009 12:48 am

Re: [C++] TR1 Unordered_Map

Postby Vevix » Fri Mar 12, 2010 10:24 pm

Maps are like this

Code: Select all
Map = {
      [Key] = { Data },
      [Key] = { Data },
      [Key] = { Data },
};


You cannot have duplicate keys so they are unique, each key holds its specific data that you access by 'Map[Key]' or 'Map.at(Key)'

So if you check the 'count' of a specific Key and it returns 1 it means it exists, if it returns 0 it doesn't exist.


Code: Select all
std::tr1::unordered_map<string, string> map;

map["Hello"] = "Chicken";
map["World"] = "Nuggets";

if ( map.count( "Hello123" ) > 0 )
   std::cout << "Key Exists\n";
else
   std::cout << "Key doesn't exist\n";


So for the above, the two keys would be "Hello" and "World" so when we check for "Hello123" it will return 0 as it doesn't exist.
Think Globally. Act Within Local Variable Scope.
User avatar
Vevix
teh awesome
 
Posts: 535
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll

Re: [C++] TR1 Unordered_Map

Postby Vevix » Fri Mar 12, 2010 10:35 pm

I had a function for this I used awhile back

Code: Select all
bool IsValid(const std::wstring& CvarName)
{
   // Does this key exist?
   if (m_mCvars.count(CvarName) > 0)
      return true;

   std::wcout << L"Cvar isn't valid\n";
   return false;
}
Think Globally. Act Within Local Variable Scope.
User avatar
Vevix
teh awesome
 
Posts: 535
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll

Re: [C++] TR1 Unordered_Map

Postby vEjEsE » Sat Mar 13, 2010 10:06 am

Thanks for the help. Unordered_map is the way to go if you want an easy to acces container, and key checking really improves things.
"The generation of random numbers is too important to be left to chance."
User avatar
vEjEsE
teh awesome
 
Posts: 515
Joined: Mon Aug 03, 2009 12:48 am

Re: [C++] TR1 Unordered_Map

Postby vEjEsE » Fri Mar 19, 2010 11:55 am

Vevix wrote:You can expand on the value by using a struct, so each key could have multiple values/variables.

How would I do that?
"The generation of random numbers is too important to be left to chance."
User avatar
vEjEsE
teh awesome
 
Posts: 515
Joined: Mon Aug 03, 2009 12:48 am

Re: [C++] TR1 Unordered_Map

Postby Vevix » Fri Mar 19, 2010 12:24 pm

Code: Select all
#include <iostream>
#include <unordered_map>
#include <string>

int main()
{
   struct val
   {
      std::string name;
      int value;
   };

   std::tr1::unordered_map<std::string, val> map;

   map["One"].name = "Hello World";
   map["One"].value = 11;

   for (auto i = map.begin(); i != map.end(); ++i)
   {
      std::cout << "ID: " << i->first << "\nName: " << i->second.name
         << "\nValue: " << i->second.value << "\n";
   }

      return 0;
}


ID: One
Name: Hello World
Value: 11
Press any key to continue . . .
Think Globally. Act Within Local Variable Scope.
User avatar
Vevix
teh awesome
 
Posts: 535
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll

Re: [C++] TR1 Unordered_Map

Postby vEjEsE » Fri Mar 19, 2010 12:29 pm

I thought the key could be a struct, not the value.
so each key could have multiple values/variables.
"The generation of random numbers is too important to be left to chance."
User avatar
vEjEsE
teh awesome
 
Posts: 515
Joined: Mon Aug 03, 2009 12:48 am

Re: [C++] TR1 Unordered_Map

Postby Vevix » Fri Mar 19, 2010 12:45 pm

No I've said that keys are completely unique you can't change that using unordered_map it goes against it's design.

You can expand on the value by using a struct, so each key could have multiple values/variables.


It says you can expand on the value with a struct not the key.
Think Globally. Act Within Local Variable Scope.
User avatar
Vevix
teh awesome
 
Posts: 535
Joined: Sat Oct 10, 2009 9:24 pm
Location: ntdll.dll

Re: [C++] TR1 Unordered_Map

Postby vEjEsE » Fri Mar 19, 2010 1:01 pm

Yes, my fault :">
"The generation of random numbers is too important to be left to chance."
User avatar
vEjEsE
teh awesome
 
Posts: 515
Joined: Mon Aug 03, 2009 12:48 am

Re: [C++] TR1 Unordered_Map

Postby magg » Fri Mar 26, 2010 7:15 pm

hey Vevix, I cant compile your code... can you help me


Code: Select all

#include <iostream>
#include <tr1/unordered_map>
#include <string>

using namespace std;
using namespace std::tr1;

int main() {
       struct val{
          string name;
          int value;
       };

       unordered_map<string, val> map;

       map["One"].name = "Hello World";
       map["One"].value = 11;

       for (auto i = map.begin(); i != map.end(); ++i)
       {
          cout << "ID: " << i->first << "\nName: " << i->second.name
             << "\nValue: " << i->second.value << "\n";
       }

return 0;
}



these are the errors that appear


Code: Select all



hashtable.cpp: In function ‘int main()’:
hashtable.cpp:14: error: template argument for ‘template<class _T1, class _T2> struct std::pair’ uses local type ‘main()::val’
hashtable.cpp:14: error:   trying to instantiate ‘template<class _T1, class _T2> struct std::pair’
hashtable.cpp:14: error: template argument 5 is invalid
hashtable.cpp:14: error: invalid type in declaration before ‘;’ token
hashtable.cpp:16: error: request for member ‘name’ in ‘"One"[map]’, which is of non-class type ‘const char’
hashtable.cpp:17: error: request for member ‘value’ in ‘"One"[map]’, which is of non-class type ‘const char’
hashtable.cpp:19: error: ISO C++ forbids declaration of ‘i’ with no type
hashtable.cpp:19: error: request for member ‘begin’ in ‘map’, which is of non-class type ‘int’
hashtable.cpp:19: error: request for member ‘end’ in ‘map’, which is of non-class type ‘int’
hashtable.cpp:21: error: base operand of ‘->’ is not a pointer
hashtable.cpp:21: error: base operand of ‘->’ is not a pointer
hashtable.cpp:22: error: base operand of ‘->’ is not a pointer


can you help me?
magg
 
Posts: 1
Joined: Fri Mar 26, 2010 6:39 pm

Next

Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests

suspicion-preferred