首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用SGI STL hash_map?

如何使用SGI STL hash_map?
EN

Stack Overflow用户
提问于 2008-12-04 17:27:41
回答 7查看 10K关注 0票数 0

我正在尝试使用我从他们的网站下载的SGI STL实现。我想使用hashmap,因为我必须存储大约5.000.000条记录,但它应该很好:我需要能够非常快地访问它。我尝试过stedext::hash_map,但它非常慢,因为我不能设置初始大小。顺便问一下,有没有可能这样做?如果我将额外的路径添加到我的MS Visual Studio中,我甚至无法从SGI站点编译示例。我收到一条错误消息:

代码语言:javascript
复制
error C2061: syntax error : identifier 'T'.

其他人有没有遇到过这样的问题?

EN

回答 7

Stack Overflow用户

发布于 2008-12-05 18:04:43

我承认我自己没有尝试过,但是VS2008应该支持TR1,它包含:

代码语言:javascript
复制
#include <tr1/unordered_map>

它是在一个“功能包”版本中。http://www.microsoft.com/downloads/details.aspx?FamilyId=D466226B-8DAB-445F-A7B4-448B326C48E7&displaylang=en

票数 2
EN

Stack Overflow用户

发布于 2008-12-04 17:36:41

我已经用过它很多次了,没有出现任何问题,不过我是在gcc ( windows和linux)而不是Visual Studio上使用它的。

对于实际使用,文档是here

您可以使用以下命令指定要预留的存储桶数量

代码语言:javascript
复制
void resize(size_type n)

关于标识符T的问题,我假设您忘记了将名为T的模板参数替换为实际的类型。如果您不能弄清楚,也许可以粘贴一段代码片段,说明您是如何使用hash_map的。

文档中的示例:

代码语言:javascript
复制
#include <hash_map>
#include <iostream>

struct eqstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) == 0;
  }
};

int main()
{
  std::hash_map<const char*, int, hash<const char*>, eqstr> months;

  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;

  std::cout << "september -> " << months["september"] << endl;
  std::cout << "april     -> " << months["april"] << endl;
  std::cout << "june      -> " << months["june"] << endl;
  std::cout << "november  -> " << months["november"] << endl;
}

当然,如果您愿意,您可以使用std::string代替char*:

代码语言:javascript
复制
std::hash_map<std::string, int, hash<std::string>, eqstr> months;
票数 1
EN

Stack Overflow用户

发布于 2008-12-04 19:28:48

当您尝试构建/编译您的项目时,是否出现了其他错误消息?

你提到你..。

向SGI STL所在的项目添加了一个额外的目录。

你能再详述一下吗?在visual studio项目设置中有许多地方可以添加目录。例如,添加附加的包含头路径、附加库路径等。您在哪里添加了目录?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/341429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档