使用unique_ptr的字符的C++数组
C++ array of char using unique_ptr
首先,我知道这不是最好的方法,我只是想看看应该怎么做。我创建了一个名为bord的类,它包含一个成员
std::unique_ptr<std::unique_ptr<char>[] > char_bord;
这应该是正确的语法,然后我尝试在构造函数中初始化它:
bord::bord():char_bord(new std::unique_ptr<char>[10])
{
//char_bord=new std::unique_ptr<char>[10]; //This did not seem to work aswell.
for(int i=0;i<10;i++)
char_bord[i]=new std::unique_ptr<char>[](new char[10]);
return;
}
这导致了下面的一堆错误,我没能破译这些错误。
jelmer@jelmer-N56JN:~/Git/Board/lib$ g++ -std=c++0x bord.c
In file included from bord.c:1:0:
bord.h:20:1: error: new types may not be defined in a return type
class bord
^
bord.h:20:1: note: (perhaps a semicolon is missing after the definition of ‘bord’)
bord.c:3:12: error: return type specification for constructor invalid
bord::bord():char_bord(new std::unique_ptr<char>[10])
^
bord.c: In constructor ‘bord::bord()’:
bord.c:7:46: error: expected primary-expression before ‘]’ token
char_bord[i]=new std::unique_ptr<char>[](new char[10]);
^
bord.c:7:60: error: parenthesized initializer in array new [-fpermissive]
char_bord[i]=new std::unique_ptr<char>[](new char[10]);
^
bord.c:7:19: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<char>’ and ‘std::unique_ptr<char>*’)
char_bord[i]=new std::unique_ptr<char>[](new char[10]);
^
bord.c:7:19: note: candidates are:
In file included from /usr/include/c++/4.9/memory:81:0,
from bord.h:19,
from bord.c:1:
/usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = char; _Dp = std::default_delete<char>]
operator=(unique_ptr&& __u) noexcept
^
/usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::unique_ptr<char>&&’
/usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> > >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = char; _Dp = std::default_delete<char>]
operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
^
/usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template argument deduction/substitution failed:
bord.c:7:19: note: mismatched types ‘std::unique_ptr<_Tp, _Dp>’ and ‘std::unique_ptr<char>*’
char_bord[i]=new std::unique_ptr<char>[](new char[10]);
^
In file included from /usr/include/c++/4.9/memory:81:0,
from bord.h:19,
from bord.c:1:
/usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = char; _Dp = std::default_delete<char>; std::nullptr_t = std::nullptr_t]
operator=(nullptr_t) noexcept
^
/usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::nullptr_t’
我做错了什么,假设我做错了。
以下是一些代码,演示了我认为您想要的东西:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <memory>
using namespace std;
using array_ptr_type = std::unique_ptr<char[]>;
using array_of_arrays_type = std::unique_ptr<array_ptr_type[]>;
auto main() -> int
{
auto x = array_ptr_type(new char[10]);
auto y = array_ptr_type(new char[10]);
for (int i = 0 ; i < 10 ; ++i)
{
x[i] = char('0' + i);
y[i] = char('0' + 9 - i);
}
auto pxy = array_of_arrays_type(new array_ptr_type[2]);
pxy[0] = move(x);
pxy[1] = move(y);
for (int i = 0 ; i < 2 ; ++i) {
copy(&pxy[i][0], &pxy[i][10], ostream_iterator<char>(cout, ", "));
cout << endl;
}
return 0;
}
预期输出:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
当然,正如您所知,不建议使用这些方法——vector<vector<char>>
会更干净、更易于维护。
您必须使用专业化:
std::unique_ptr<char[]> chars(new char[1024]);
这是因为std::unique_ptr
不像std::shared_ptr
那样支持自定义deleter(在这种写作风格中)。
std::unique_ptr
使用std::default_delete
作为删除器。很快,如果您将参数类型指定为class T
,它将使用默认的delete
,但如果您编写class T[]
(在此专门化中),std::unique_ptr
将使用delete[]
。
但是最好使用一些容器,而不是c样式的数组。
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?