我构建了一个简单的类,它应该模仿std :: string类的功能(作为练习!):
#ifndef _STR12_1_H
#define _STR12_1_H
#include <string>
#include <iostream>
class Str12_1
{
public:
typedef char* iterator;
typedef const char* const_iterator;
typedef long size_type;
Str12_1();
Str12_1(const Str12_1& str);
Str12_1(const char *p);
Str12_1(const std::string& s);
size_type size() const;
//Other member functions
private:
iterator first;
iterator onePastLast;
iterator onePastAllocated;
};
Run Code Online (Sandbox Code Playgroud)
为了避免与"new"相关的开销(并增加我对<memory>标题的熟悉程度),我选择使用库的allocator模板类为我的字符串分配内存.以下是我在复制构造函数中使用它的示例:
#include <memory>
#include <algorithm>
using std::allocator;
using std::raw_storage_iterator;
using std::uninitialized_copy;
Str12_1::Str12_1(const Str12_1& str)
{
allocator<char> charAlloc;
first = charAlloc.allocate(str.size());
onePastLast = onePastAllocated = first + str.size();
*onePastLast = …Run Code Online (Sandbox Code Playgroud) 我已经获得了一个自定义分配器,它实现了它自己的分配策略,它自己的malloc/free等等.现在,我被要求使用这个自定义分配器和一个STL容器(它是一个向量或其他).我创建了一个类,比如说my_stdAllocator,这是一个符合ISO C++标准的接口.通过这个类我调用我的分配器的方法.例如:
template <class T>
class my_stdAllocator {
// ...other required stuff...
// allocate 'num' elements of type T
pointer allocate(size_type num, const_pointer = 0) {
return static_cast<T*>(MYAllocator::instance()->malloc(num));
}
// deallocate memory at 'p'
void deallocate(pointer p, size_type num=0) {
MYAllocator::instance()->free(p);
}
// initialize allocated memory at 'p' with value 'value'
void construct(pointer p, const T& value) {
::new ((void*)p) T(value);
}
// destroy elements of initialized memory at p
void destroy(pointer p) {
p->~T();
} …Run Code Online (Sandbox Code Playgroud) 我正在使用这里提到的 STL 分配器。
我所做的唯一更改是我从一个名为 Object 的基类继承,并且我使用基类的 new 和 delete 函数进行分配。
class MyAlloc :public Object{
......
}
Run Code Online (Sandbox Code Playgroud)
我想使用基类的参数化构造函数,它将基于发送到 STLAllocator 的参数,这将是这样的。
MyAlloc(A *a) : Object(a) {
... }
Run Code Online (Sandbox Code Playgroud)
然后使用这个构造函数,如:
A *a = new A();
std::vector<int,MyAlloc<int> (a) > v;
Run Code Online (Sandbox Code Playgroud)
我无法实现这一目标。它导致编译错误:
'a'不能出现在常量表达式
模板参数 2 中无效
提前致谢..:)
这个open-std文档表明std :: function支持自定义分配器,但是我无法在互联网上找到任何关于如何提供自定义分配器的常用参考.
我的研究倾向于让我相信自定义分配器已经实现boost::function但std::function尚未实现.
所以问题是std::function在C++ 11中使用自定义分配器吗?如果没有,它最有可能支持C++ 14中的一个?
我们有一个提供C接口的库extern "C",并且是从C代码中使用的,但是它内部使用STL容器和一些C++功能(如RAII)以方便使用.
现在有一个新的要求,即库应该能够指向来自客户端代码的自定义malloc和free函数,并将其用于内部的分配.我可以将它们放入库的上下文结构中,并在需要的地方使用它们,但是将它们与STL一起使用是令人费解的......
我查看了allocator类,但似乎STL容器必须能够使用默认构造函数来创建分配器,似乎没有办法将这些指针放入它们中,让它们通过它们调用来进行分配.
是否有可能以线程安全的方式(不使用全局变量)来解决这个问题?
#include <bits/stdc++.h>
using namespace std;
vector<int> func()
{
vector<int> a(3,100);
return a;
}
int main()
{
vector<int> b(2,300);
//b.swap(func()); /* why is this not working? */
func().swap(b); /* and why is this working? */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,b.swap(func())没有编译.它给出了一个错误:
调用'std :: vector <int,std :: allocator <int >>> :: swap(std :: vector <int,std :: allocator <int >>)'
/usr/include/c ++/没有匹配函数4.4/bits/stl_vector.h:929:注意:候选者是:void std :: vector <_Tp,_Alloc> :: swap(std :: vector <_Tp,_Alloc>&)[with _Tp = int,_Alloc = std: :分配器<int>的]
但是,当写成时func().swap(b),它会编译.
他们之间究竟有什么区别?
我认为在 MSVC++ 中发现了一个错误。或者,这可能是我缺乏知识,我错过了代码中的某些内容。我创建了一个自定义分配器:
#include <forward_list>
#include <iostream>
template <class T>
class Allocator
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef T &reference;
typedef const T &const_reference;
typedef T value_type;
template <class U>
struct rebind
{
typedef Allocator<U> other;
};
Allocator()
{
std::cout << (ptrdiff_t) this << " Allocator()" << std::endl;
}
Allocator(const Allocator &allocator)
{
std::cout << (ptrdiff_t) this << " Allocator(const Allocator &allocator)" << std::endl;
}
template <class U>
Allocator(const Allocator<U> …Run Code Online (Sandbox Code Playgroud) 当a std::map将Allocator作为构造函数中的参数时,它通过引用获取其类模板参数中的类型:
explicit map(const Allocator& alloc);
Run Code Online (Sandbox Code Playgroud)
它是将此引用存储在对象中,还是采用副本(按值存储),还是不执行任何操作,仅将模板参数用作类型?你是怎么决定的?
请参阅以下代码片段。
根据我的理解:
a) 'p1' 和 'p2' 对象在堆栈中创建,并在 getPoints() 方法结束时销毁。
b)当使用 push_back( )将p1和p2添加到向量时,默认分配器会创建 Point 的新实例并将p1和p2的值 (x,y) 复制到这些新创建的实例中。
我的问题是:
1)我的理解正确吗?
如果是这样的话 ;
2) 如果分配器创建了新的 Point 对象,为什么我只看到两行“Points created”?
因为我希望看到p1和p2 的两行以及分配器新创建的对象的两行。
3)分配器如何为新创建的对象的 x,y 字段分配原始值?它是否使用原始内存复制?
4) 共享指针是从方法返回向量的推荐方式吗?
#include <iostream>
#include <vector>
using namespace std;
struct Point {
Point() {
std::cout<< "Point created\n";
x=0;
y=0;
}
int x;
int y;
};
std::shared_ptr< vector<Point> > getPoints() {
std::shared_ptr< vector<Point> > ret = std::make_shared< …Run Code Online (Sandbox Code Playgroud) 我想创建一个自定义分配器basic_string,允许我获得分配的字符串内部数组的所有权。我的具体用例是 .NET 互操作场景,其中将字符串封送回托管代码的成本很高,因为它需要在特定池中分配字符串(至少在Windows 中),更重要的是堆中数组的所有权必须是转入。我能够成功地为std::vector主要编译器(MSVC、gcc、clang)编写此类自定义分配器并验证其兼容性。我现在尝试使用相同的分配器basic_string,我观察到奇怪的行为,因为所有主要的 STL 实现似乎都没有使用提供的分配器进行第一次分配,特别是前 16 个字节。它遵循我正在使用的代码:
#include <memory>
#include <stdexcept>
#include <vector>
#include <iostream>
// The requirements for the allocator where taken from Howard Hinnant tutorial:
// https://howardhinnant.github.io/allocator_boilerplate.html
template <typename T>
struct MyAllocation
{
size_t Size = 0;
std::unique_ptr<T> Ptr;
MyAllocation() { }
MyAllocation(MyAllocation && other) noexcept
: Ptr(std::move(other.Ptr)), Size(other.Size)
{
other.Size = 0;
}
};
// This allocator keep ownership of the last allocate(n)
template <typename T>
class MyAllocator …Run Code Online (Sandbox Code Playgroud) allocator ×10
c++ ×10
stl ×6
c++11 ×2
c++14 ×1
c++17 ×1
char ×1
dictionary ×1
shared-ptr ×1
std-function ×1
stdstring ×1
stdvector ×1
vector ×1