标签: unordered-map

以下c ++代码返回什么?

我有一段代码需要理解.但是我迷失了一点.这是代码:

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回报?我有点迷茫.

这不是所有代码,都有初始化等等.但是我没有把所有的代码放在这里.

谢谢,

c++ types iterator unordered-map map

-1
推荐指数
1
解决办法
83
查看次数

在数据结构中存储函数以便以后查找和调用它们

我正在考虑创建一个程序来将函数存储在一个无副本的映射中.是否有可能通过他们的钥匙找到他们并打电话给他们?我也想在其他模块中使用它作为头文件.它会解决相同的内存地址位置还是每次生成新的内存?

c++ unordered-map heap-memory data-structures

-1
推荐指数
1
解决办法
82
查看次数

unordered_map c ++错误

我写这段代码:

运行时,行中发生错误

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)

c++ unordered-map

-2
推荐指数
1
解决办法
997
查看次数

unordered_map似乎没有在[]运算符上返回引用

我目前有一个数据库类,其中包含一些包含数据库变量的无序映射.我编写了添加和删除变量的函数,但现在想要一种方法来轻松访问和修改它们.

我的功能是这样的:

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)

看起来无序地图没有返回引用,问题是什么?

c++ unordered-map c++14

-2
推荐指数
1
解决办法
475
查看次数

有没有办法从std :: unordered_map中获取所有值?

我有unordered_map一些数据,我希望能够将内容保存到文件并稍后检索数据.我对关键数据不感兴趣,只对地图值感兴趣.

如果我可以获得存储在地图中的值的数组,那么可以轻松地将其写入磁盘,但我不确定获取该数据的最佳方法是什么.

我已经考虑过使用.begin()指向开头的迭代器并迭代所有桶直到找​​到所有值.当然有更好的方法来做到这一点.

如果可能的话,我真的不想使用提升等.

c++ unordered-map

-2
推荐指数
1
解决办法
588
查看次数

在unordered_map中哪一个更快"找到"或"在"?

我知道对于无序映射,"find"返回迭代器,而"at"返回映射值.我只是好奇哪一个更快.

c++ unordered-map find

-2
推荐指数
2
解决办法
2614
查看次数

C++ unordered_map <pair <uint,uint>,UserClass> insertion

我试图使用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)

c++ unordered-map

-2
推荐指数
1
解决办法
153
查看次数

C ++ unordered_map :: insert无法编译

我只需要从一个浮动对象到另一个浮动对象的哈希图。应该很简单,不是吗?编译器只是不接受它:

宣言:

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)

c++ stl unordered-map insert

-2
推荐指数
1
解决办法
87
查看次数

标签 统计

c++ ×8

unordered-map ×8

c++14 ×1

data-structures ×1

find ×1

heap-memory ×1

insert ×1

iterator ×1

map ×1

stl ×1

types ×1