我在使用时遇到段错误std::unordered_map::emplace()。这是最小的可重现示例:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
class WordTable {
public:
WordTable() {
total = 0;
}
~WordTable() {}
void addWord(const string word, const int incr = 1) {
cout << "begin emplace" << endl;
table.emplace(word, Node()); //this is where the seg fault occurs
cout << "emplace succeeded" << endl;
if (incr) {
table[word].incrementCount();
incrementTotal();
}
}
private:
struct Node {
public:
Node() {
count = 0;
kids = new WordTable();
}
~Node() {
delete …Run Code Online (Sandbox Code Playgroud) 我在某处读到,一旦一个桶容纳了超过 8 个元素,它就会变成一棵红黑树,而不是一个链表。我知道 java 使用此策略,但我确定 c++
考虑如下代码:
#include <iostream>
#include <unordered_map>
std::unordered_map<char, int> get_letter_frequencies(const std::string& str) {
std::unordered_map<char, int> freqs;
for (char ch : str) {
auto iter = freqs.find(ch);
if (iter == freqs.end()) {
freqs[ch] = 1;
} else {
iter->second++;
}
}
return freqs;
}
int main()
{
std::string str = "AABBDBCABDA";
auto freqs = get_letter_frequencies(str);
std::cout << freqs['B'] << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它存储 unordered_map 中的字母计数。我的问题是有一段简洁/更惯用的代码可以用来替换
auto iter = freqs.find(ch);
if (iter == freqs.end()) {
freqs[ch] = 1;
} else {
iter->second++;
} …Run Code Online (Sandbox Code Playgroud) 这个脚本
#include <iostream>
#include <unordered_map>
#include <any>
using namespace std;
int main() {
unordered_map<int, any> test;
test[5] = "Hey!";
cout << test[5];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用?
candidate function not viable: no known conversion from 'std::__ndk1::unordered_map<int, std::__ndk1::any, std::__ndk1::hash<int>, std::__ndk1::equal_to<int>, std::__ndk1::allocator<std::__ndk1::pair<const int, std::__ndk1::any> > >::mapped_type' (aka 'std::__ndk1::any') to 'const void *' for 1st argument; take the address of the argument with &
basic_ostream& operator<<(const void* __p);
Run Code Online (Sandbox Code Playgroud)
抱歉,如果这听起来有点愚蠢
在我的程序中,如果我的无序映射中不存在某个键,我希望默认值将是 而std::numeric_limits<double>::max()不是 0。
我的代码如下所示:
class Pair
{
public:
Graph::NodeId node;
bitset<64> bits;
Pair(Graph::NodeId node1, double bits1)
{
node = node1;
bitset<64> temp(bits1);
bits = temp;
}
};
bool operator==(const Pair &P1, const Pair &P2)
{
return P1.node == P2.node && P1.bits == P2.bits;
}
template <>
struct std::hash<Pair>
{
std::size_t operator()(const Pair &pair) const noexcept
{
std::hash<decltype(pair.node)> ihash;
std::hash<decltype(pair.bits)> bhash;
return ihash(pair.node) * 31 + bhash(pair.bits);
}
};
unordered_map<Pair, double> lFunction;
Run Code Online (Sandbox Code Playgroud)
因此,如果我想访问该元素lFunction[Pair(3,3)]并且键不存在,则应该存在 value std::numeric_limits<double>::max()。
int start[3]我的程序需要一个哈希图,其输入是由两个 3D 坐标 ( , )定义的范围int stop[3],其输出是动态整数数组,定义为std::vector<int>. 为了实现这个哈希表,我声明了std::tuple<int*, int*>一个名为 的自定义类型CoordsKey。然后我重新实现了operator()基于start[3]和计算哈希值(为了快速原型设计,我只需根据和的值stop[3]创建一个,因此它可以通过 Boost 进行哈希处理)。std::tuple<int, int, int, int, int, int>int *startint *stop
#include <iostream>
#include <tuple>
#include <unordered_map>
#include <boost/container_hash/extensions.hpp>
using CoordsKey = std::tuple<int*, int*>;
struct CoordsKeyHash {
std::size_t operator()(const CoordsKey& key) const
{
/* std::tuple is hashable by Boost */
std::tuple<int, int, int, int, int, int> t = {
std::get<0>(key)[0], std::get<0>(key)[1], std::get<0>(key)[2],
std::get<1>(key)[0], …Run Code Online (Sandbox Code Playgroud) 我试图unordered_map<int, int>按其值对 an 进行反向排序,但我不明白为什么它不能正常工作。
这是我正在使用的代码:
static bool comp(const pair<int,int>& a, const pair<int,int>& b) { return a.second < b.second; }
unordered_map<int,int> sort_map(unordered_map<int,int>& m) {
vector<pair<int,int>> v;
unordered_map<int,int> sorted_m;
for (const auto& it : m) {
v.push_back(it);
}
sort(v.begin(),v.end(),comp);
std::cout << "sorted vector:" << std::endl;
for (const auto& it : v) {
std::cout << it.first <<":" << it.second <<std::endl;
sorted_m.emplace(it);
}
return sorted_m;
}
Run Code Online (Sandbox Code Playgroud)
这是地图的输入示例:[5,-3,9,1,7,7,9,10,2,2,10,10,3,-1,3,7,-9,-1,3,3]
这是输出:
sorted vector:
-9:1
1:1
-3:1
5:1
-1:2
2:2
9:2
10:3
7:3
3:4
sorted …Run Code Online (Sandbox Code Playgroud) 我有一个 std::unordered_map ,我发现我插入其中的对象与我通过使用范围遍历从中获得的对象不同。
我怀疑这里可能发生了一些对象复制,但是在我向复制构造函数添加了一些转储之后,它根本没有被调用。
谁能告诉我插入和遍历 std::unordered_map 时后台发生了什么?
我尝试了以下代码并转储:
[结果]
mypair constuctor
mypair constuctor
string1 address:0x7ffccb813ba0
string2 address:0x7ffccb813bd0
++++++++++++++++
auto &x address:0x55fb40529378
string2: 0.5
auto &x address:0x55fb405292c8
string1: 0.3
++++++++++++++++
auto x address:0x7ffccb813c00
string2: 0.5
auto x address:0x7ffccb813c00
string1: 0.3
++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
[来源]
#include <iostream>
#include <string>
#include <unordered_map>
class mypair : public std::pair<std::string,double> {
public:
mypair(std::string str, double num):std::pair<std::string,double>(str, num) {
std::cout<<"mypair constuctor"<<std::endl;
}
mypair( const mypair& ) {
std::cout<<"mypair copy constuctor"<<std::endl;
}
mypair& operator=(const mypair&) {
std::cout<<"mypair copy assignment"<<std::endl;
return …Run Code Online (Sandbox Code Playgroud) 我发现,BOOST_REVERSE_FOREACH与BOOST_FOREACH的工作方式不同.
我的代码:
#include <boost\unordered_map.hpp>
#include <boost\foreach.hpp>
#include <iostream>
#include <string>
typedef boost::unordered_map<std::string, int> map;
int main()
{
map MyMap;
MyMap["two"] = 2;
MyMap["three"] = 3;
MyMap["one"] = 1;
std::cout << MyMap["one"] << MyMap["two"] << MyMap["three"] << std::endl;
BOOST_FOREACH (map::value_type value, MyMap)
{
std::cout << value.second;
}
std::cout << std::endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我也想使用反向迭代.所以我补充说:
BOOST_REVERSE_FOREACH (map::value_type value, MyMap)
{
std::cout << value.second;
}
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)
在此之后它无法编译,是否有人可以告诉如何在无序地图上使用反向foreach.
编译器给出:
1>c:\boost_1_52_0\boost\iterator\reverse_iterator.hpp(45): error C2675: unary '--' : 'boost::unordered::iterator_detail::iterator<NodePointer,Value>' does not define this operator …Run Code Online (Sandbox Code Playgroud) 我试图call_function使用一个字符串参数,并调用与该映射中的字符串相对应的函数,但出现错误。我该如何解决?为什么不起作用?
#include <iostream>
#include <unordered_map>
using namespace std;
class xyz {
public:
unordered_map< std::string, void(xyz::*)()> arr{
{ "user", &xyz::func_user},
{ "pwd", &xyz::func_pwd},
{ "dir", &xyz::func_dir}
};
void call_function(std::string x) {
arr.at( x)();// Error: term does not evaluate a function taking 0 arguments
}
void func_user(){
cout << "func_user" << endl;
}
void func_pwd(){
cout << "func_pwd" << endl;
}
void func_dir(){
cout << "func_dir" << endl;
}
};
int main(){
xyz a;
a.call_function( "dir");
}
Run Code Online (Sandbox Code Playgroud)