标签: stdmap

c ++中的多键映射

我想知道你们是否知道c ++关联映射容器类型,我可以执行多个键查找.地图需要有恒定的时间查找,但我不在乎它是有序还是无序.它只需要快速.

例如,我想在地图中存储一堆std :: vector对象,其中包含整数和void*作为查找键.int和void*都必须匹配我要检索的向量.有这样的事情吗?或者我将不得不自己动手.如果有,有什么建议吗?我一直在尝试将boost :: unordered_map存储在另一个boost :: unordered_map中,但我还没有使用此方法取得任何成功.如果没有更简单的方法,也许我会继续使用这种方法.谢谢!

c++ containers dictionary stdmap

6
推荐指数
1
解决办法
7560
查看次数

std :: map :: insert(...)中的分段错误

我用过搜索但是我找不到满足我的答案...所以......这是一大堆代码:

 //VoteContainer.h    
    typedef uint32_t order_id_t;
    typedef int driver_id_t;

    class Vote {

        public:
            enum DriverVoteResponse {YES, NO, TIMEOUT};

            struct DriverResponse {
                driver_id_t driver_id;
                time_t time;
                DriverVoteResponse response;
            };

            Vote() : m_order_id(0), m_time_until(0) {};
            Vote(order_id_t inOrderId, std::vector<driver_id_t> inPermittedDrivers, int inSeconds);
            Vote(const Vote & other) : m_order_id(other.m_order_id), m_time_until(other.m_order_id) {
                m_drivers_responses = other.m_drivers_responses;
                m_permitted_drivers = other.m_permitted_drivers;
            };

            virtual ~Vote() {};

            virtual void addDriverVote(driver_id_t inDriverId, DriverVoteResponse inDriverResponse);
            virtual void getAppropriateDriverId(driver_id_t * inDriverId); //with min response time

        private:

            order_id_t m_order_id;
            time_t m_time_until;
            std::vector<DriverResponse> m_drivers_responses;
            std::vector<driver_id_t> m_permitted_drivers; …
Run Code Online (Sandbox Code Playgroud)

c++ stl stdmap segmentation-fault

6
推荐指数
1
解决办法
5511
查看次数

如何在std :: map中使用struct作为键

我想使用std::map其键和值元素是结构的.

我收到以下错误: error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const GUID

我明白我应该operator <为这种情况重载,但问题是我无法访问我想要使用的结构的代码(GUIDVC++中的结构).

这是代码片段:

//.h

#include <map>
using namespace std;

map<GUID,GUID> mapGUID;


//.cpp

GUID tempObj1, tempObj2;              
mapGUID.insert( pair<GUID,GUID>(tempObj1, tempObj2) );   
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

c++ stl visual-studio-2005 stdmap

6
推荐指数
1
解决办法
1万
查看次数

擦除元素时,映射迭代器如何失效?

使用擦除方法时,地图中的迭代器何时以及如何失效?

例如 :

std :: map < int , int > aMap ;

aMap [ 33 ] = 1 ;
aMap [ 42 ] = 10000 ;
aMap [ 69 ] = 100 ;
aMap [ 666 ] = -1 ;

std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ;

for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ;
      it != itEnd ; …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl stdmap std

6
推荐指数
1
解决办法
5107
查看次数

LevelDB与std :: map

在我们的应用程序中,我们使用std::map存储(键,值)数据并使用序列化将该数据存储在磁盘上.通过这种方法,我们发现磁盘I/O是性能瓶颈,使用密钥查找值不是很快.

我遇到了LevelDB并且正在考虑使用它.但我有一些问题.

  1. LevelDB的文档说明它是(字符串,字符串)键值对.这是否意味着我不能用于自定义键值对?
  2. 似乎std::map和LevelDB 之间的区别在于LevelDB是持久的并且std::map在内存中工作.这是否意味着磁盘I/O瓶颈对于levelDB来说会更成问题.

更具体地说,任何人都可以解释一下LevelDB是否可能是更好的选择std::map

PS:我尝试使用hash_maps但它看起来比较慢std::map

c++ stdmap boost-serialization leveldb

6
推荐指数
1
解决办法
3540
查看次数

字符数组作为C++映射中的值

我想定义类似的东西

Map<int, char[5] > myMap;
Run Code Online (Sandbox Code Playgroud)

上面的声明被c ++编译器接受,并且没有抛出错误,但是当我做这样的事情时

int main()
{
    char arr[5] ="sdf";
    map <int, char[5]> myMap;
    myMap.insert(pair<int, char[5]>(0,arr));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到错误:

In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.6/bits/char_traits.h:41,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from charMap.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = char [5]]’:
charMap.cpp:9:42:   instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:104:31: error: array used as initializer
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = char [5], _T1 …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary stdmap std c++-standard-library

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

使用一个值为std :: shared_ptr的map是一个很好的设计选择,用于拥有多索引的类列表?

问题很简单:我们有一个有成员a,b,c,d的类...我们希望能够快速搜索(键是一个成员的值)并通过提供当前值来更新具有新值的类列表或者b或c ......我想过要一堆
std::map<decltype(MyClass.a/*b,c,d*/),shared_ptr<MyClass>>.

1)这是个好主意吗?

2)boost multi index在各方面都优于这个手工解决方案吗?

出于简单/性能原因,PS SQL是不可能的.

c++ stdmap shared-ptr boost-multi-index c++11

6
推荐指数
1
解决办法
1209
查看次数

将元素插入std :: map而无需额外复制

考虑这个程序:

#include <map>
#include <string>
#define log magic_log_function // Please don't mind this.

//
// ADVENTURES OF PROGO THE C++ PROGRAM
//

class element;
typedef std::map<int, element> map_t;

class element {
public:
    element(const std::string&);
    element(const element&);
    ~element();
    std::string name;
};
element::element(const std::string& arg)
    : name(arg)
{
    log("element ", arg, " constucted, ", this);
}
element::element(const element& other)
    : name(other.name)
{
    name += "-copy";
    log("element ", name, " copied, ", this);
}
element::~element()
{
    log("element ", name, " destructed, ", this); …
Run Code Online (Sandbox Code Playgroud)

c++ optimization stdmap c++-standard-library

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

如何将std :: map作为c ++类函数中的默认构造函数参数传递

尝试std::map在Ubuntu 12.04上使用clang-3.3和clang-3.0 时遇到问题:

#include <iostream>
#include <map>
#include <string>

class A
{
public:
#if 0 //clang compiles ok
    typedef std::map<std::string,std::string> MapKeyValue_t;
    void PrintMap(const MapKeyValue_t &my_map 
        = MapKeyValue_t())
#else // clang compiles fail
    void PrintMap(const std::map<std::string,std::string> &my_map 
    = std::map<std::string,std::string>())
#endif
{
    std::map<std::string,std::string>::const_iterator it;
    for (it = my_map.begin(); it != my_map.end(); it++)
    {
        std::cout << it->first << " " << it->second << std::endl;
    }
}
};

int main()
{
    A a;
    a.PrintMap();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,当代码编译在这两个g++clang我不断收到这些错误的输出: …

c++ stdmap clang

6
推荐指数
1
解决办法
950
查看次数

std :: map在低内存情况下导致"堆栈溢出"

此应用程序正在使用C++在Windows XP上的VS2010中开发.

当计算机在物理内存上运行时非常低(并且因为它是我们的测试用例而禁用了页面文件),这行代码:

std::map<UINT, std::vector<void *>> MyMap;

在malloc.c中导致"堆栈溢出"错误

'return HeapAlloc(_crtheap, 0, size ? size : 1);'
Run Code Online (Sandbox Code Playgroud)

MyApp.exe中0x7c90e8e5处的未处理异常:0xC00000FD:堆栈溢出.

此调用是从应用程序的一个线程进行的.如果内存不足是错误,它应该抛出bad_alloc

有人可以在这里建议可能的原因.

编辑:

这就是实际堆栈的样子

ntdll.dll!7c90e8e5()    

[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 

ntdll.dll!7c9100d3()    

MyApp.exe!_heap_alloc_base(unsigned int size=72)  Line 55   C

MyApp.exe!_heap_alloc_dbg_impl(unsigned int nSize=36, int nBlockUse=1, const char * szFileName=0x00000000, int nLine=0, int * errno_tmp=0x0af3f0e4)  Line 431 + 0x9 bytes   C++

MyApp.exe!_nh_malloc_dbg_impl(unsigned int nSize=36, int nhFlag=0, int nBlockUse=1, const char * szFileName=0x00000000, int nLine=0, int * errno_tmp=0x0af3f0e4)  Line 239 …
Run Code Online (Sandbox Code Playgroud)

c++ stack-overflow stl stdmap

6
推荐指数
1
解决办法
625
查看次数