std::map的自定义分配器失败 我试图使用C++的STL容器使用自定义分配器,它使用向量,但是用MAP失败了。关于mmap\u分配器的一些奇怪错误,以及没有像我预期的那样使用mmap\u分配器 In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/map:60, from 4.cpp:2: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h: In member function ‘_Alloc std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::get_allocator() const [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:383: instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_destroy_node(std::_Rb_tree_node<_Val>*) [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’ /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:972: instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’ /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:614: instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’ /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_map.h:87: instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:354: error: no matching function for call to ‘mmap_allocator<std::pair<const int, int> >::mmap_allocator(const mmap_allocator<std::_Rb_tree_node<std::pair<const int, int> > >&)’ 4.cpp:37: note: candidates are: mmap_allocator<T>::mmap_allocator(const mmap_allocator<T>&) [with T = std::pair<const int, int>] 4.cpp:36: note: mmap_allocator<T>::mmap_allocator() [with T = std::pair<const int, int>] 在/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../include/c++/4.4.6/map:60中包含的文件中, 从4.cpp:2: /usr/lib/gcc/x86\u 64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:在成员函数“\u Alloc std::\u Rb_tree::get_allocator()const[with _Key=int,\u Val=std::pair,\u KeyOfValue=std::\u Select1st,\u Compare=std::less,\u Alloc=mmap@分配器]”: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:383:从“void std::_Rb_tree::_M_destroy_node(std:_Rb_tree_tree_node*)[带(Key=int,)Val=std::pair,(KeyOfValue=std:)std:(std:)std:)std:(KeyOfValue=std:41 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:972:从“void std::_Rb_tree::_M_erase(std:_Rb_tree_tree_node*)[带_Key=int,_Val=std::pair,_KeyOfValue=std:_select1; std:_Select1st:_select1; std:_Compare=std::less,_Alloc=mmap)分配器 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:614:从“std::_Rb_tree::~_Rb_tree()[带_Key=int,_Val=std::pair,_KeyOfValue=std::_Select1st,_Compare=std::less,_Alloc=mmap)分配器]”实例化 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_-map.h:87:从此处实例化 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../include/c++/4.4.6/bits/stl_tree.h:354:错误:调用“mmap_分配器::mmap_分配器(常量mmap_分配器&)”时没有匹配的函数 4.cpp:37:注:候选项为:mmap_分配器::mmap_分配器(常量mmap_分配器&)[带T=std::pair] 4.cpp:36:注意:mmap_分配器::mmap_分配器()[带T=std::pair]

std::map的自定义分配器失败 我试图使用C++的STL容器使用自定义分配器,它使用向量,但是用MAP失败了。关于mmap\u分配器的一些奇怪错误,以及没有像我预期的那样使用mmap\u分配器 In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/map:60, from 4.cpp:2: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h: In member function ‘_Alloc std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::get_allocator() const [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:383: instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_destroy_node(std::_Rb_tree_node<_Val>*) [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’ /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:972: instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’ /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:614: instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = int, _Val = std::pair<const int, int>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::less<int>, _Alloc = mmap_allocator<std::pair<const int, int> >]’ /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_map.h:87: instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:354: error: no matching function for call to ‘mmap_allocator<std::pair<const int, int> >::mmap_allocator(const mmap_allocator<std::_Rb_tree_node<std::pair<const int, int> > >&)’ 4.cpp:37: note: candidates are: mmap_allocator<T>::mmap_allocator(const mmap_allocator<T>&) [with T = std::pair<const int, int>] 4.cpp:36: note: mmap_allocator<T>::mmap_allocator() [with T = std::pair<const int, int>] 在/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../include/c++/4.4.6/map:60中包含的文件中, 从4.cpp:2: /usr/lib/gcc/x86\u 64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:在成员函数“\u Alloc std::\u Rb_tree::get_allocator()const[with _Key=int,\u Val=std::pair,\u KeyOfValue=std::\u Select1st,\u Compare=std::less,\u Alloc=mmap@分配器]”: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:383:从“void std::_Rb_tree::_M_destroy_node(std:_Rb_tree_tree_node*)[带(Key=int,)Val=std::pair,(KeyOfValue=std:)std:(std:)std:)std:(KeyOfValue=std:41 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:972:从“void std::_Rb_tree::_M_erase(std:_Rb_tree_tree_node*)[带_Key=int,_Val=std::pair,_KeyOfValue=std:_select1; std:_Select1st:_select1; std:_Compare=std::less,_Alloc=mmap)分配器 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_tree.h:614:从“std::_Rb_tree::~_Rb_tree()[带_Key=int,_Val=std::pair,_KeyOfValue=std::_Select1st,_Compare=std::less,_Alloc=mmap)分配器]”实例化 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../../include/c++/4.4.6/bits/stl_-map.h:87:从此处实例化 /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../../../../include/c++/4.4.6/bits/stl_tree.h:354:错误:调用“mmap_分配器::mmap_分配器(常量mmap_分配器&)”时没有匹配的函数 4.cpp:37:注:候选项为:mmap_分配器::mmap_分配器(常量mmap_分配器&)[带T=std::pair] 4.cpp:36:注意:mmap_分配器::mmap_分配器()[带T=std::pair],c++,linux,gcc,stl,g++,C++,Linux,Gcc,Stl,G++,代码如下: #include <vector> #include <map> #include <stdio.h> static size_t alloc; template <typename T> class mmap_allocator: public std::allocator<T> { public: typedef size_t size_

代码如下:

#include <vector>
#include <map>
#include <stdio.h>

static size_t alloc;

        template <typename T>
        class mmap_allocator: public std::allocator<T>
        {
public:
                typedef size_t size_type;
                typedef T* pointer;
                typedef const T* const_pointer;

                template<typename _Tp1>
                struct rebind
                {
                        typedef mmap_allocator<_Tp1> other;
                };

                pointer allocate(size_type n, const void *hint=0)
                {
                        fprintf(stderr, "Alloc %d bytes.\n", n);
                        alloc += n;
                        return std::allocator<T>::allocate(n, hint);
                }

                void deallocate(pointer p, size_type n)
                {
                        fprintf(stderr, "Dealloc %d bytes (%p).\n", n, p);
                        alloc -= n;
                        return std::allocator<T>::deallocate(p, n);
                }

                mmap_allocator() throw(): std::allocator<T>() { fprintf(stderr, "Hello allocator!\n"); }
                mmap_allocator(const mmap_allocator &a) throw(): std::allocator<T>(a) { }
                ~mmap_allocator() throw() { }
        };

int main(){

std::vector<int, mmap_allocator<int> > int_vec(1024, 0, mmap_allocator<int>());
std::map<int, int, std::less<int>,  mmap_allocator<std::pair<int,int> > >  x;
x[1] = 2;
printf("s=%lu\n", alloc);
return 0;
}
#包括
#包括
#包括
静态尺寸分配;
模板
类mmap_分配器:公共std::分配器
{
公众:
typedef size_t size_type;
typedef T*指针;
typedef const T*const_指针;
模板
结构重新绑定
{
类型定义mmap_分配器其他;
};
指针分配(大小\类型n,常量void*hint=0)
{
fprintf(stderr,“Alloc%d字节。\n”,n);
alloc+=n;
返回std::allocator::allocate(n,提示);
}
无效解除分配(指针p,大小\类型n)
{
fprintf(标准,“解除锁定%d字节(%p)。\n”,n,p);
alloc-=n;
返回std::allocator::deallocate(p,n);
}
mmap_分配器()throw():std::allocator(){fprintf(stderr,“Hello allocator!\n”);}
mmap_分配器(const mmap_分配器&a)throw():std::allocator(a){}
~mmap_分配器()抛出(){}
};
int main(){
std::vector int_vec(1024,0,mmap_分配器());
地图x;
x[1]=2;
printf(“s=%lu\n”,alloc);
返回0;
}

Linux,gcc 4.4.6。

我还没有尝试修复它,但您似乎还没有定义一个构造函数,它使用不同的模板参数接受分配器实例化。也就是说,你错过了类似的东西

template <typename T>
    template <typename O>
mmap_allocator<T>::mmap_allocator(mmap_allocator<O> const& other) {
    ...
}
模板
模板
mmap_分配器::mmap_分配器(mmap_分配器常量和其他){
...
}

从外观上看,错误源于试图构造从
重新绑定
获得的分配器类型与其他分配器。

我遇到了相同的问题,您找到问题所在了吗?