我已经定义了unordered_map这样的
struct pht {
pht(int t, vector<bool> pat)
: tag(t), pattern(32) {}
private:
int tag;
vector<bool> pattern;
};
unordered_map< pair<int, int>, pht > predictor;
int main()
{
int pc, addr, offset, tag;
vector<bool> pat;
srand(time(0));
tag = 1000; pc = 100; offset = 10;
for ( int i = 0; i < 32; i++ )
pat.push_back( rand() % 2 );
predictor.insert(make_pair( make_pair(pc, offset), pht(tag, pat) ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我收到此错误:
(更新)
error C2440: 'type cast' : cannot …Run Code Online (Sandbox Code Playgroud) vector <unordered_map <string, DomainInfo *> *> victimDomains;
这是什么意思?
我得到了第一颗星(DomainInfo*),但第二颗是什么?
让我们说DomainInfo有两个属性ID和name.如果我想把第二个元素放在DomainInfo第三个unordered_map中victimDomains我怎么称它?我的问题不正确吗?
谢谢,
我试过这段代码
#include <iostream>
#include <utility>
#include <vector>
#include <unordered_map>
#include <stdexcept>
using namespace std;
int main() {
unordered_map<int,int> parent_map;
try {
int a = parent_map[0];
cout<<a<<endl;
} catch (out_of_range oe) {
cout<<"out of range"<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我认为应该抓住超出范围的例外.但是,输出是
0
Run Code Online (Sandbox Code Playgroud)
我很迷惑.我记得我之前有过这个工作.
我在if语句中使用map.find(key)和map.end()函数:
if( p_Repos->returnTypeMap().find(tc[0]) != p_Repos->returnTypeMap().end() )
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我得到一个Microsoft Visual C++运行时库错误,告诉我"表达式:列表迭代器不兼容".tc [0]只是一个字符串,我的地图中的键位置是一个字符串.
但是,它们应该兼容,对吗?
任何帮助是极大的赞赏.
谢谢,汤姆
编辑:根据这里找到的答案:在unordered_map中查找值,我会相信这应该是def.
第二次编辑:
这是returnTypeMap()函数:
std::unordered_map <std::string, std::pair<std::string, std::string>> returnTypeMap()
{
return typeTable;
}
Run Code Online (Sandbox Code Playgroud)
这是我的地图的定义:
std::unordered_map <std::string, std::pair<std::string, std::string>> typeTable;
Run Code Online (Sandbox Code Playgroud) 在以下示例中尝试写入std::stringan的键时std::unordered_map,键的写入顺序与初始化列表给出的顺序不同:
#include <iostream>
#include <unordered_map>
class Data
{
typedef std::unordered_map<std::string, double> MapType;
typedef MapType::const_iterator const_iterator;
MapType map_;
public:
Data(const std::initializer_list<std::string>& i)
{
int counter = 0;
for (const auto& name : i)
{
map_[name] = counter;
}
}
const_iterator begin() const
{
return map_.begin();
}
const_iterator end() const
{
return map_.end();
}
};
std::ostream& operator<<(std::ostream& os, const Data& d)
{
for (const auto& pair : d)
{
os << pair.first << " ";
}
return os; …Run Code Online (Sandbox Code Playgroud) 我对unordered_map的默认构造函数有一些疑问.
这是代码:
unordered_map<int, bool> m;
cout <<boolalpha << m[0] << endl;
Run Code Online (Sandbox Code Playgroud)
输出是真还是假?
我知道元素是使用其默认构造函数构造的,但映射的值是true还是false?这是未定义的行为吗?
在vs2013中,输出是false.
实际上,我想删除数组中的重复元素.我想解决这个问题:
int a[] = {1, 2, 3, 1, 2, 3, 4};
unordered_map<int, bool> m;
int j = 0;
for (int i = 0; i < 7; ++i)
{
if (!m[a[i]])
{
a[j++] = a[i];
m[a[i]] = true;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,我真的很感激你的回答.
我有一个继承自unordered_map的自定义类,如下所示:
class _map : public unordered_map<string, _pointer> {
public:
// STUFF ...
};
Run Code Online (Sandbox Code Playgroud)
让我们假设这_pointer是另一个自定义类,它的作用是帮助我处理指针的东西._pointers可以从许多不同的预定义类型构造,例如:
vector<...> v;
_pointer p = _pointer(v);
// p is now a _pointer holding v and vector<...> data
Run Code Online (Sandbox Code Playgroud)
现在,如果我想_pointer在我的地图上存储vector <...> ,我可以简单地做:
_map map;
vector<...> v;
map["a_vector"] = _pointer(v);
Run Code Online (Sandbox Code Playgroud)
它有效,但我希望能够做到这一点:
_map map;
vector<...> v;
map["a_vector"] = v;
Run Code Online (Sandbox Code Playgroud)
并将它_pointer(v)自动分配,也许通过重载map如何分配东西并明确告诉它vector<...>通过调用特定的_pointer构造函数来处理它.
我试图这样做(通过重载operator [],operator =)但似乎我不会去某个地方.任何人都可以建议我实现这种行为的方法吗?
谢谢.
常问问题:
问:为什么在标识符名称上使用_?难道你不知道这是不好的做法吗?
答:手头的问题无关紧要.问:您的代码不是RAII.
A:那么,什么?问:你为什么不使用unique_ptr/smart_ptr?你为什么直接处理指针?
答:因为我需要,而且对于手头的问题也无关紧要.问:你为什么要重载unordered_map?你为什么不用这个_OTHER_OPTION_?
答:不,谢谢,这是我需要做的.
我正在尝试使用C++的动态编程算法来解决旅行商问题.我试图解决25个城市的问题,这意味着我必须在每次迭代中存储多达500万个键值对. unordered_map
使用这个算法与Python和4GB ram我的进程由于内存不足而被杀死,所以我试图提高内存中的性能.
为了减少使用的内存量,我试图保留两个unordered_set,一个具有前一个迭代的值,另一个具有新值.
std::unordered_map<std::string, int> costs;
std::unordered_map<std::string, int> new_costs;
for (int m = 1; m <= n; m++) {
new_costs.clear();
while (something) {
// I build the content of new_costs based on the content of costs
}
// Here I want to make costs point to new_costs and free new_costs to
// build the next iteration
costs = new_costs; // ??
}
Run Code Online (Sandbox Code Playgroud)
我不知道是否可以避免复制所有内容new_costs,costs因为我们正在讨论数百万个元素.
我想知道我是否可以使用指针来 …
我正在研究一个编译器,我试图通过继承unordered_map来表示范围类,因为它本质上是声明符号的哈希表.我在符号中添加了一个自定义哈希函数,但是我收到一个错误,抱怨没有用于初始化std :: pair的默认构造函数.这是相关代码:
Symbol.hpp
#pragma once
#include <string>
#include <unordered_set>
class Symbol
{
friend class Symbol_table;
Symbol(std::string const* str) : m_str(str) { }
/// Constructs the symbol from `str`.
public:
Symbol() : m_str() { }
std::string const& str() const { return *m_str; }
/// Returns the spelling of the token.
friend bool operator==(Symbol a, Symbol b)
{
return a.m_str == b.m_str;
}
friend bool operator!=(Symbol a, Symbol b)
{
return a.m_str != b.m_str;
}
private:
std::string const* m_str;
};
class …Run Code Online (Sandbox Code Playgroud) 假设我有一个初始化但空的std :: unordered_map,以及两个将同时填充它的线程.这两个线程只会写入地图,在完成两个线程之前,不会从地图中读取任何内容.
此外,两个线程将永远不会在地图中的相同键上操作.例如,假设线程1将填充键"A"到"M",并且线程2将同时填充键"N"到"Z".
这个线程安全吗?
在我当前的实现中,我有8个线程以上述方式写入单个互斥的std :: unordered_map.互斥体显然会减慢进程(有近10,000个密钥被填充),所以我想知道我是否需要互斥锁.
谢谢大家!
c++ ×10
unordered-map ×10
c++11 ×3
vector ×2
algorithm ×1
g++ ×1
iterator ×1
linked-list ×1
memory ×1
overloading ×1
pointers ×1