我试图从std :: unordered_map(VS2010)切换到boost :: unordered_map(版本1.48),令人惊讶的是,一些重要的测试用例在我的项目中失败了.我找到了原因并得出结论:boost :: unordered_map不遵守我的不区分大小写的相等提供程序:
struct StringEqualityCaseInsensitive : public std::equal_to<String>
{
bool operator ()(const String& a, const String& b) const { return boost::iequals<String, String>(a, b); }
};
boost::unordered_map<string, int, boost::hash<string>, StringEqualityCaseInsensitive> map;
Run Code Online (Sandbox Code Playgroud)
现在我只添加一些大写元素并搜索它们的小写对应物(使用find()成员方法).如果我使用std :: unordered_map它可以正常工作并且使用boost它不会.残酷的是,如果我查找大写元素,则会调用相等比较器,当我查找小写时,它不会被调用...
任何人都有线索,这是为什么?(不确定这是否重要,但我使用的是启用了C++ 0x支持的英特尔编译器12.1)
编辑:该死的,现在它恍然大悟.也许我还需要调整哈希类以返回与低/大写无关的相同值.但他们有不同的行为仍然很奇怪?!
谢谢!
我在类C中有一个静态unordered_map.如果我将类定义和声明放在包含函数main的文件的不同文件中,我会遇到行为上的差异.
问题是我观察到如果类C与函数main在同一个编译单元中,一切都很好,我只看到一次文本"new string created:c".但是,如果我将我的代码分成三个文件(参见下面的清单),我会看到"new string created:c"两次,这意味着我的静态unordered_map在进入main之前被擦除了.
我的问题是:为什么会发生这种情况?(只有在使用Apple LLVM编译器4.1进行编译时才会出现这种差异.我已经使用g ++ 4.7 -std = c ++ 11测试了它,并且拆分代码运行得很好.)
提前感谢任何想法!
// would go to My_header.h
#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;
class C{
public:
C(const string & s);
private:
static unordered_map<string, string*> m;
string *name;
};
// would go to My_code.cpp
// (when separated, add #include "My_header.h")
unordered_map<string, string*> C::m;
C::C(const string & s):
name(NULL)
{
string*& rs = m[s];
if(rs)
{
name = rs; …Run Code Online (Sandbox Code Playgroud) 我正在尝试同时使用列表和 unordered_map 来存储同一组对象。我是 C++ 的新手,所以仍然对迭代器感到满意。
假设我有以下测试代码:
class Test {
public:
int x;
int y;
int z;
Test (int, int, int);
}
Test t1 = Test(1,2,3);
Test t2 = Test(2,4,6);
Test t3 = Test(3,6,9);
std::list<Test> list;
std::unordered_map<int, Test> map;
list.push_back(t3);
list.push_back(t2);
list.push_back(t1);
map[101] = t1;
map[102] = t2;
map[103] = t3;
Run Code Online (Sandbox Code Playgroud)
是否可以通过键查找对象,然后从对象的引用(或从 unordered_map 生成器?)生成列表迭代器?
因此,如果我有密钥 102,我可以在恒定时间内查找 t2。然后我想相对于 t2 在列表中的位置迭代向前/向后/插入/删除。
我可以使用 find 来获取指向 t2 的 unordered_map 迭代器。我不知道如何生成一个从 t2 开始的列表迭代器(我只能在列表的开头或结尾生成迭代器,然后迭代。)
感谢任何人向我指出有关 STL 和迭代器的优秀教程。
谢谢!
事后思考:这是一种可以接受的方法吗?我有很多对象,需要通过整数键有效地查找它们。我还需要保留它们的顺序(与这些整数键无关)并有效地插入/删除/遍历。
我试图std::hash<T>通过提供专业化来扩展const char,以便我可以const char*用作键类型std::unordered_map.
这是我试过的:
#include <unordered_map>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
namespace std
{
template<>
struct hash<const char*>
{
size_t operator()(const char* const& s) const
{
size_t h = 0;
const char* tmp = s;
while (*tmp)
h = (h << 5) + h + (unsigned char)toupper(*tmp++);
printf("hash of '%s' is (%ld)\n", s, h);
return h;
}
};
}
int main(int argc, char* argv[])
{
const char* name1= "Mark …Run Code Online (Sandbox Code Playgroud) 我有以下问题.我有一个std::unordered_map包含对象作为值.现在我想修改我之前插入的对象.
class Point
{
public:
Point(float _x, float _y) : x(_x), y(_y) {}
float x;
float y;
};
Run Code Online (Sandbox Code Playgroud)
std::unordered_map<int, Point> points;
// ... add some values to it ...
points[1].x = 20.f; // error?
Run Code Online (Sandbox Code Playgroud)
我得到一个奇怪的长编译错误关于点不能默认构造.我理解它的方式operator []返回对映射类型(也就是值)的引用,那么为什么我不能修改它?
#include<boost/unordered_map.hpp>
#include<string>
#include<iostream>
#include<boost/unordered_set.hpp>
using namespace std;
typedef boost::unordered_map<string, boost::unordered_map<string, boost::unordered_set<string>>> nfa;
const boost::unordered_map<string, boost::unordered_set<string>>&
get_second(const std::pair<string,
boost::unordered_map<string, boost::unordered_set<string>>>& p)
{return p.second;}
int main()
{
nfa a;
a["A"]["0"] = {"B", "C"};
a["A"]["1"] = {"B"};
a["B"]["0"] = {"B"};
a["B"]["1"] = {"C"};
cout << "Printing using direct reference" << endl;
for (auto tr_table : a)
{
for (auto tr : tr_table.second)
cout << tr_table.first << " " << tr.first << " " << tr.second.size() << endl;
}
cout << "Printing using function …Run Code Online (Sandbox Code Playgroud) 我有一个无序的字符串和字符串向量的地图,即un_map<string,vector<string> >.使用find函数搜索特定字符串时:
find((un_map[A].begin(),un_map[A].end(),field)==un_map[A].end())
Run Code Online (Sandbox Code Playgroud)
无序映射和有序映射的查找函数的执行时间是相同的.谁能解释为什么这样?到目前为止,我知道由于散列,无序地图应该比有序地图快得多.我想优化查找功能.请帮忙
我正在尝试在 stl unordered_map 上创建一个模板化的包装类。我将哈希函数类作为模板参数传递给包装类,并提供了字符串特化。下面的代码编译并工作,但如果包含注释部分,则编译错误说:
“/usr/include/c++/6/bits/unordered_map.h:143:28: 错误:没有匹配的函数调用'HashFunction
::HashFunction()' const hasher& __hf = hasher(),"。
但是,我一定会有散列函数类的ctor。我尝试了各种方法,但无法使其正常工作。请提供您的想法/意见。
#include <iostream>
#include <unordered_map>
using namespace std;
template< class Key >
class HashFunction
{
public:
//HashFunction( const Key & inKey );
size_t operator()(const Key &inKey) const;
private:
unsigned mHashCode;
};
//template<>
//HashFunction< string >::HashFunction( const string & inKey )
//{
//mHashCode=std::hash<string>{}(inKey);
//}
template <>
size_t HashFunction< string >::operator()(const string &inKey) const
{
return std::hash<string>{}(inKey);
//return mHashCode;
}
template< class Key, class Val, class Hash = HashFunction< Key …Run Code Online (Sandbox Code Playgroud) 我知道如何std::map通过使用检索 a 的最大元素std::max_element,但std::unordered_map由于容器类型之间的差异,我无法使用 a 实现相同的效果。
如何找到 a 中的最大值std::unordered_map并返回相应的std::pair?
显示了我当前使用 a 执行此操作的方法std::map(基于此答案)。我似乎无法弄清楚如何对std::unordered_map.
template <typename KEY_T, typename VALUE_T>
std::pair<KEY_T, VALUE_T> findMaxValuePair(
std::map<KEY_T, VALUE_T> const &x)
{
return *std::max_element(x.begin(), x.end(),
[](const std::pair<KEY_T, VALUE_T> &p1,
const std::pair<KEY_T, VALUE_T> &p2)
{
return p1.second < p2.second;
});
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在std::unorderd_map( 替换为 ) 上使用上述函数std::map时std::unordered_map,我收到一个Segmentation fault (core dumped).
我有一个带有它们的吸气剂的向量模式,如下所示:
vector<A>& getA() const { return a; }
vector<B>& getB() const { return b; }
vector<C>& getC() const { return c; }
...
Run Code Online (Sandbox Code Playgroud)
我对拥有某种函数很感兴趣,vector<T>& getByName(string s) const这样我就可以这样调用函数:getByName("A"),getByName("B")等等。
我一直在使用一个无序地图试过,但我还没有找到任何合适的方法,使这样的工作线:unordered_map< string, vector<T> >。
A, B, C ... 是完全不同的结构,所以多态解决方案不是我尽可能寻找的。
由于vector<T>是一个固定大小的容器(如24个字节我不是错了),我不明白为什么地图上不能够存储这些地方字节内存无论是矢量的类型。