我想使用自定义分配器从空闲列表中为std::basic_ostringstream. 这是我要使用的自定义分配器:
template <class Tp>
struct NAlloc {
typedef Tp value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
NAlloc() = default;
template <class T> NAlloc(const NAlloc<T>&) {}
Tp* allocate(std::size_t n) {
n *= sizeof(Tp);
memoryPool *memPool = memoryPool::GetInstance(10);//get memory pool instance
std::cout << "allocating " << n << " bytes\n";
return static_cast<Tp*>(memPool->allocate(n)); //get memory from pool
}
void deallocate(Tp* p, std::size_t n) {
std::cout << …Run Code Online (Sandbox Code Playgroud) 我试图使用固定长度的数组作为a的键std::map,如下面的非编译代码所示:
#include <cstdlib>
#include <iostream>
#include <map>
typedef char myuuid[ 16 ];
template <class T>
class MyAlloc
{
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
template <class U> struct rebind { typedef MyAlloc<U> other; };
MyAlloc() {}
MyAlloc( const MyAlloc& other ) {}
T* allocate( std::size_t n ) { return static_cast<T*>( std::malloc( n * sizeof( value_type ) ) ); }
void deallocate( …Run Code Online (Sandbox Code Playgroud) 我试图使用自己的分配器来测量C++中的内存使用情况std::set.不幸的是,我在链接时遇到错误.为了简化问题,我有以下程序:
#include<set>
#include<vector>
#include<memory>
//using Container = std::vector<int, std::allocator<int>>;
using Container = std::set<int, std::allocator<int>>;
int main() {
Container container;
container.push_back(4711);
container.insert(4711);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果可以在wandbox https://wandbox.org/permlink/R5WcgSvSWiqstYxL#wandbox-resultwindow-code-body-1中找到
我试过gcc 6.3.0,gcc 7.1.0,clang 4.0.0和clang 6.0.0HEAD.在所有情况下,我使用a时都会出错std::set,但是当我使用a时却没有std::vector.
如何声明我的设置使用分配器?
我想使用C++ 17,但C++ 14中的答案也很好.
许多STL容器都有一个范围构造函数,可用于初始化容器元素:
char ary[] = {'a', 'b', 'c'};
vector<char> v(ary, ary + 3);
Run Code Online (Sandbox Code Playgroud)
我将特定的STL容器子类化,仅用于默认使用自定义分配器:
template<class _Ty, class _Ax = stl_allocator<_Ty> >
class xvector
: public std::vector<_Ty, _Ax>
{
};
Run Code Online (Sandbox Code Playgroud)
这很好用,但我不能再使用范围构造函数了:
xvector<char> v2(ary, ary + 3);
Run Code Online (Sandbox Code Playgroud)
生成以下错误:
error C2661: 'xvector<_Ty>::xvector' : no overloaded function takes 2 arguments
with
[
_Ty=char
]
Run Code Online (Sandbox Code Playgroud)
但是,我可以使用assign方法:
xvector<char> v3;
v3.assign(ary, ary + 3);
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么范围构造函数在赋值方法时不起作用?我正在使用VC++ 2008,从我可以告诉范围构造函数的参数列表和assign方法完全相同.
更新 感谢您的回答,但对我而言,关键是我忘记了构造函数不是继承的,因此只有默认构造函数可用(它与范围构造函数没有任何关系).
我也应该提到我不得不坚持使用VS2008/C++ 03,因为目标平台不支持更新的编译器.
当std :: allocator超出范围时会发生什么?它管理的资源会怎样?如果没有提前释放,是否泄漏了?
我有一个std::map<std::string,std::size_t>键映射计数器。当我增加一个计数器时,我不知道它是否已经存在。如果是,则递增。如果不是,则设置为 1。这很容易做到:
std::map<std::string,std::size_t> counters;
//...
auto c_it = counters.find(key);
if(c_it == counters.end())
counters.insert(key,1);
else
(*c_it)++;
Run Code Online (Sandbox Code Playgroud)
这是一个简单的事情的很多代码......我想这样做:
counters[key]++;
Run Code Online (Sandbox Code Playgroud)
但这会产生未定义的行为,因为std::size_t()在映射中不存在计数器时调用,并且不能保证std::size_t()初始化为 0。
我看到了两个潜在的解决方案:
std::size_t使用默认构造函数创建时强制初始化为 0的类型。std或 中有这样的类型boost吗?std::allocator<std::pair<const Key,T>>作为std::map. 但我不知道该怎么做。注意:我只使用 C++11(我不想要 C++>=14 的解决方案)
给定一个自定义向量并使用 std::allocator 进行分配,在 C++17 及更高版本下,我们仍然需要使用alignas创建内部过度对齐类型OT,然后为OT分配,并在取消引用指针之前将reinterpret_cast OT* 转换为T*在迭代器运算符*上,为了支持类型的过度对齐?
或者 std::allocator 是否正确对齐,并且取消引用 T* 是否可以完美地返回对元素的正确过度对齐引用?
感谢您的任何澄清。
vector<int> data(istream_iterator<int>(cin),
istream_iterator<int>{}); cout<<"Size is : " << data.size() << endl; //compile success
vector<int> data1(istream_iterator<int>(cin),
std::allocator<int>{}); cout<<"Size is : " << data1.size() << endl; //compile failure
Run Code Online (Sandbox Code Playgroud)
error: no matching function for call to ‘std::vector<int>::vector(std::istream_iterator<int>, std::allocator<int>)’ vector<int> data1(istream_iterator<int>(cin), std::allocator<int>{});
Run Code Online (Sandbox Code Playgroud)
为什么第一个语句很好,但第二个呢?int在这种情况下,向量不采用类型分配器吗?我正在试验allocators。