我想用a替换a vector<string>和boost::unordered_map<string, size_t>映射字符串到前者的索引boost::bimap.
我bimap应该使用什么实例?到目前为止,我已经想到了
typedef bimap<
unordered_set_of<size_t>,
vector_of<string>
> StringMap;
Run Code Online (Sandbox Code Playgroud)
但我不确定我现在是否已经改变了收藏类型.另外,我想知道我是否应该更改关系类型的集合.一个vector_of_relation是我最好的选择,还是一个set_of_relation,或者只是默认?
所以我正在玩unordered_mapSTL 的新标准化.我的代码有点像这样,我只是创建一个unordered_map,填充并打印出来:
unordered_map<int,string> m1;
m1[5]="lamb";
m1[2]="had";
m1[3]="a";
m1[1]="mary";
m1[4]="little";
m1[7]="fleece";
m1[6]="whose";
m1[10]="fleecey";
m1[8]="was";
m1[9]="all";
for(unordered_map<int,string>::const_iterator i = m1.begin(); i != m1.end(); ++i)
cout<<i->first<<" "<<i->second<<endl;
Run Code Online (Sandbox Code Playgroud)
但是,我获得的输出因此被订购:
1 mary
2 had
3 a
4 little
5 lamb
6 whose
7 fleece
8 was
9 all
10 fleecey
Run Code Online (Sandbox Code Playgroud)
但是我不想为我的地图订购付出代价!这就是我使用unordered_map的原因......这里发生了什么?
附加说明:我正在使用gcc version 4.3.4 20090804 (release) 1 (GCC)并正在编译这样的g++ -std=c++0X maptest.cpp
我正在使用stl unordered_map,我似乎无法使count方法起作用.这是我的计划:
typedef unordered_map<char, int> Mymap;
int main()
{
Mymap m;
m.insert(Mymap::value_type('a', 1));
m.insert(Mymap::value_type('b', 2));
m.insert(Mymap::value_type('c', 3));
m.insert(Mymap::value_type('b', 4));
m.insert(Mymap::value_type('b', 5));
cout << m.count('b') << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
unordered_map的文档说明了unordered_map::count(const Key& k)返回带键的元素数k.所以我希望这里的输出是3,而真正的输出是1.为什么?
我试图以相反的顺序打印unordered_map的内容,但它没有rbegin或rend,所以你不能使用reverse_iterator.如何逆转unordered_map?
编辑(来自评论):我希望密钥按插入顺序排列,因此我无法使用地图.键似乎保持在插入顺序,但我需要它们反转.
对于调试,我需要查看地图(std::unordered_map或std::map)的内容,Eclipse CDT调试器没有以可读的形式提供给我,例如{1: 2, 3:4, ...}
调试时检查地图内容的最佳方法是什么?
我是否必须使用print-statements返回调试?如果是的话,简洁的宏或函数如何将任何地图的内容打印为字符串?
如何创建不区分大小写的unordered_map<string, int>?
覆盖key_equal是否足够或我还需要更新hasher?
我写了一个程序,它读取输入,直到你点击',' - 输入的COMA.然后它计算你输入的字母数,
我想迭代这个地图,但它说it无法定义,没有类型:
#include <iostream>
#include <conio.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <tr1/unordered_map>
using namespace std;
int main(){
cout<<"Type '.' when finished typing keys: "<<endl;
char ch;
int n = 128;
std::tr1::unordered_map <char, int> map;
do{
ch = _getch();
cout<<ch;
if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){
map[ch] = map[ch] + 1;
}
} while( ch != '.' );
cout<<endl;
for ( auto it = map.begin(); it …Run Code Online (Sandbox Code Playgroud) 我试图对unordered_map使用sort()函数进行排序,但我不断收到编译器错误.有人可以帮忙吗?
bool comp(pair<char,int> a, pair<char,int> b) {
return a.second < b.second;
}
void rearrangeKDist(char str[], int d) {
int n = strlen(str);
unordered_map<char, int> table;
for (int i=0; i<n; i++) {
unordered_map<char, int>::iterator it = table.find(str[i]);
if (it == table.end()) {
table.insert(make_pair(str[i], 1));
} else {
it->second = it->second+1;
}
}
for (unordered_map<char, int>::iterator it=table.begin(); it!=table.end(); it++)
cout<<it->first<<" "<<it->second<<endl;
sort(table.begin(), table.end(), comp);
for (unordered_map<char, int>::iterator it=table.begin(); it!=table.end(); it++)
cout<<it->first<<" "<<it->second<<endl;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用外部网络库,它返回一些代表打开的套接字的魔术结构,文档说当将它们插入STL容器时,应该使用它们进行比较std::owner_less.
std::map<MagicStructure, std::shared_ptr<Client>, std::owner_less<MagicStructure>> sockets;
Run Code Online (Sandbox Code Playgroud)
不过我想unordered_map改用.我该怎么做?std::owner_less是一个比较器,它对哈希映射没用.挖掘源代码,MagicStructure似乎是一个typedef std::shared_ptr.
我正在将一个{string, MyStruct}对象插入到unordered_map中,稍后迭代unordered_map并选择擦除该元素.但是,在擦除元素之前,我有一个断言,它显示unordered_map为空.
这是我的插入内容:
my_umap.insert(std::make_pair(key.toString(), my_struct));
Run Code Online (Sandbox Code Playgroud)
该结构包含一个记录插入时间的成员.然后我定期检查地图并删除已经在unordered_map中的元素太长时间:
for(auto it = my_umap.begin(); it != my_umap.end(); ++it){
MyStruct& myStruct = it->second;
const bool deleteEntry = myStruct.ts.IsElapsed(std::chrono::seconds(5));
if(deleteEntry){
const string& key = it->first; // Cannot access memory for 'key'
assert(my_umap.size() >= 1); // This is failing
my_umap.erase(key);
}
}
Run Code Online (Sandbox Code Playgroud)
我在gdb中运行代码,断言失败.当我查询key它的值时说
无法访问内存
当我查询my_umap它的大小时,表示大小为零.
如果unordered_map的大小为零,for循环如何检测元素?没有其他线程访问此容器.我想unordered_map::insert()将对象复制到容器中,所以删除的原始对象应该无关紧要?