我有一段代码需要理解.但是我迷失了一点.这是代码:
typedef unordered_map <string, TimeStampSet *> HIEMap;
typedef set <TimeStamp> TimeStampSet;
struct HostInfo {
HostActivity *hostActivity;
HIEMap *hieMapArr;
};
typedef unordered_map <uint32_t, HostInfo *> HostInfoMap;
HIEMap::iterator hieMapIt;
void method(...){
for (hieMapIt = hostInfoIt -> second -> hieMapArr -> begin();
hieMapIt != hostInfoIt -> second -> hieMapArr -> end();
hieMapIt = nextMapIt)
{
if (hieMapIt -> second == NULL) {
//what does *hieMapIt -> second* returns?
}
}
}
Run Code Online (Sandbox Code Playgroud)
什么hieMapIt -> second回报?我有点迷茫.
这不是所有代码,都有初始化等等.但是我没有把所有的代码放在这里.
谢谢,
我正在考虑创建一个程序来将函数存储在一个无副本的映射中.是否有可能通过他们的钥匙找到他们并打电话给他们?我也想在其他模块中使用它作为头文件.它会解决相同的内存地址位置还是每次生成新的内存?
我写这段代码:
运行时,行中发生错误
cout<<it2->first;
Run Code Online (Sandbox Code Playgroud)
test3.exe中0x00411edd处的未处理异常:0xC0000005:访问冲突读取位置0x00000004."
我有Visual Studio Express 2008和Boost 1_47_0;
这是我的完整代码:
#include "stdafx.h"
#include <iostream>
#include <boost/unordered_map.hpp>
using namespace std;
typedef boost::unordered_map<int,int > MAP;
MAP map2;
boost::unordered_map<int,int>::iterator it2;
void gen_random(char *s ,char *p,int* r,const int len);
void inline insert2(int i_key,int i_value);
void print();
//-----------main------------------------------------
void main()
{
char* s_key=new char[8];
char* s_value=new char[8];
int i_value=0, size_random=8;
for(int i=0;i<12;i++)
{
gen_random(s_key,s_value,&i_value,size_random);
insert2(i_value,i_value);
}
print();
int a;
std::cin>>a;
}
//-------------end main--------------------------------
//--------my function ---------------------------
//-------random--------------
void gen_random(char* s,char* p,int *r, const int len) …Run Code Online (Sandbox Code Playgroud) 我目前有一个数据库类,其中包含一些包含数据库变量的无序映射.我编写了添加和删除变量的函数,但现在想要一种方法来轻松访问和修改它们.
我的功能是这样的:
template<typename T>
T &get(const std::string &name){
if(typeid(T) == typeid(int))
return this->ints[name];
else if(typeid(T) == typeid(float))
return this->floats[name];
... ect ...
}
Run Code Online (Sandbox Code Playgroud)
如果给出了无效类型,则抛出错误.
ints是类型std::unordered_map<std::string, int>
并且floats同样定义.
对我来说,这看起来是正确的.但是当我尝试运行它时,我收到以下错误:
database.hpp:98: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
Run Code Online (Sandbox Code Playgroud)
看起来无序地图没有返回引用,问题是什么?
我有unordered_map一些数据,我希望能够将内容保存到文件并稍后检索数据.我对关键数据不感兴趣,只对地图值感兴趣.
如果我可以获得存储在地图中的值的数组,那么可以轻松地将其写入磁盘,但我不确定获取该数据的最佳方法是什么.
我已经考虑过使用.begin()指向开头的迭代器并迭代所有桶直到找到所有值.当然有更好的方法来做到这一点.
如果可能的话,我真的不想使用提升等.
我知道对于无序映射,"find"返回迭代器,而"at"返回映射值.我只是好奇哪一个更快.
我试图使用unordered_map带键的to作为类的一对uint和值Tile.
#include <iostream>
#include <unordered_map>
#include <boost/functional/hash.hpp>
class Tile {
public:
std::pair<uint, uint> coordinate;
bool operator==(const Tile &t) {
return this->coordinate.first == t.coordinate.first
&& this->coordinate.second == t.coordinate.second;
}
Tile (uint x, uint y) {
this->coordinate.first = x;
this->coordinate.second = y;
}
std::pair<uint, uint> GetCoor() const
{return this->coordinate;}
uint GetX() const
{return coordinate.first;}
uint GetY() const
{return coordinate.second;}
};
struct TileHash {
std::size_t operator()(const Tile &t) const
{
size_t seed = 0;
boost::hash_combine (seed, t.GetX()); …Run Code Online (Sandbox Code Playgroud) 我只需要从一个浮动对象到另一个浮动对象的哈希图。应该很简单,不是吗?编译器只是不接受它:
宣言:
unordered_map<float, float> m_mffPhotoPeakMap;
Run Code Online (Sandbox Code Playgroud)
使用:
float CProductSpecs::AddToMap(float fEnergy, float returnedValue) const
{
auto pair = make_pair(fEnergy, returnedValue);
m_mffPhotoPeakMap.insert(pair); // Error! (First attempt)
m_mffPhotoPeakMap[fEnergy] = returnedValue; // Error! (Second attempt)
return returnedValue;
}
Run Code Online (Sandbox Code Playgroud)
错误消息(第一次尝试):
Severity Code Description Project File Line Suppression State
Error C2663 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::insert': 6 overloads have no legal conversion for 'this' pointer
Run Code Online (Sandbox Code Playgroud)
错误消息(第二次尝试):
Severity Code Description Project File Line Suppression State
Error C2678 binary '[': no operator found which takes a left-hand operand of type 'const std::unordered_map<float,float,std::hash<float>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' …Run Code Online (Sandbox Code Playgroud)