开发者

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开发者_StackOverflow社区 error: error C2057: expected constant expression

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

Thanks!


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.


#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.


The code was before main function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜