How do you create a hash table in C++?

I am creating a simple hash table in VS 2008 C++.

#include <map>
std::map <string, char> grade_list;
grade_list["John"] = 'B';

I am getting the error: error C2057: expected constant expression

What does that mean? Does boost library have something better?

Thanks!

Answers


#include <map>
#include <iostream>
#include <string>

int main() {
    std::map<std::string, char> grade_list;
    grade_list["John"] = 'B';
    std::cout << grade_list["John"] << std::endl;
    return 0;
}

This works great with g++. You should specify std:: before string in your map declaration, as I did in my code.


First of all std::map is a treemap, not a hashmap.

The reason you get the error is that you did not #include <string> nor qualify the reference to string and thus the compiler does not know that string is a class.


Need Your Help

XML parser-writer that keeps Attributes order

python xml order

I need to parse XML document and then write every node to separate files keeping exact order of attributes.

Bound Mismatch : Generic class (of a Generic class extending Comparable (of a Generic class extending Comparable))

java generics comparable

I know it sounds confusing, but that's the best I could explain. (You can suggest a better title). I have 3 classes:-

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.