相关疑难解决方法(0)

Pretty-print C++ STL containers

Please take note of the updates at the end of this post.

Update: I have created a public project on GitHub for this library!


I would like to have a single template that once and for all takes care of pretty-printing all STL containers via operator<<. In pseudo code, I'm looking for something like this:

template<container C, class T, String delim = ", ", String open = "[", String close = "]">
std::ostream & operator<<(std::ostream & o, const C<T> …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading pretty-print c++11

379
推荐指数
5
解决办法
4万
查看次数

有一个很好的哈希函数的C++哈希表?

我需要在C++中使用面向性能的哈希函数实现来实现我将要编码的哈希表.我已经环顾四周,只发现了一个问题,询问什么是"一般"的好散列函数.我已经考虑过CRC32(但在哪里可以找到很好的实现?)和一些加密算法.不过,我的桌子有非常具体的要求.

这是表格的样子:

100,000 items max
200,000 capacity (so the load is 0.5)
hashing a 6-character string which is a part of English sentence
     examples: "become"    "and he"    ", not "
Run Code Online (Sandbox Code Playgroud)

首要任务我哈希表的是快速搜索(检索).快速插入并不重要,但它会伴随快速搜索.删除并不重要,重新哈希不是我要研究的东西.为了处理冲突,我可能会使用这里描述的单独链接.我已经看过这篇文章了,但是想要对那些曾经处理过这样的任务的人提出意见.

c++ hash hashtable

34
推荐指数
3
解决办法
4万
查看次数

unordered_map/unordered_set中元组的通用哈希

为什么不std::unordered_map<tuple<int, int>, string>开箱即用?必须为tuple<int, int>例如定义散列函数是繁琐的

template<> struct do_hash<tuple<int, int>>                               
{   size_t operator()(std::tuple<int, int> const& tt) const {...}  }; 
Run Code Online (Sandbox Code Playgroud)

构建一个以元组为键的无序映射(Matthieu M.)展示了如何自动执行此操作boost::tuple.有没有为c ++ 0x元组执行此操作而不使用可变参数模板?

当然这应该在标准:(

c++ unordered-map tuples unordered-set c++11

27
推荐指数
4
解决办法
2万
查看次数

任何枚举类型的C++ 11哈希函数

我正在为我的对象编写一个哈希函数.由于所有STL容器的Generic Hash功能,我已经可以散列容器并组合散列.但我的课程也有枚举.当然我可以为每个枚举创建一个哈希函数,但这似乎不是一个好主意.是否可以为其创建一些通用规范std::hash,以便它可以应用于每个枚举?类似的东西,使用std::enable_ifstd::is_enum

namespace std {
  template <class E>
  class hash<typename std::enable_if<std::is_enum<E>::value, E>::type> {
  public:
    size_t operator()( const E& e ) const {
      return std::hash<std::underlying_type<E>::type>()( e );
    }
  };
};
Run Code Online (Sandbox Code Playgroud)

PS.此代码无法编译

error: template parameters not used in partial specialization:
error:         ‘E’
Run Code Online (Sandbox Code Playgroud)

c++ enums templates hash-function c++11

10
推荐指数
1
解决办法
6386
查看次数

创建unordered_set的unordered_set

我想创建一个容器,用于存储唯一的整数集.

我想创造类似的东西

std::unordered_set<std::unordered_set<unsigned int>>
Run Code Online (Sandbox Code Playgroud)

但是g ++不允许我这样做并说:

invalid use of incomplete type 'struct std::hash<std::unordered_set<unsigned int> >'
Run Code Online (Sandbox Code Playgroud)

我想要实现的是拥有独特的无符号整数集.

我怎样才能做到这一点?

c++ hash c++11

6
推荐指数
2
解决办法
2079
查看次数

C ++从3个浮点值生成unordered_map的键

我真的不明白:

我正在读取点,每个点都从二进制文件中保存3个浮点值。将此点保存在unordered_map中

因此,我尝试从以下3个float值中创建一个键:

第一个意图:仅使用确切的位作为键:

unordered_map<string, vector<float>> points;
string vecToKey( float* a ) {
char bytes[12];
memcpy(bytes, a, 12);
return string(bytes);
}
Run Code Online (Sandbox Code Playgroud)

关键是我肯定想以这种方式消除相同的观点,但是

在示例项目中,读取的结果约为21374点,地图结果大小= 10640点

使用以下方法作为密钥创建的结果是10687分的正确结果

string vec3ToKey( float a[3] ) {
float a1[3];
a1[0] = a[0];
a1[1] = a[1];
a1[2] = a[2];
stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << a1;
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

问题是速度。第二种方法大约需要16秒,第一种方法只需要1-2秒...我无法自我解释为什么甚至会有差异...

我感谢每个主意:)

c++ floating-point unordered-map key

4
推荐指数
1
解决办法
2023
查看次数