C++ 如何最好地在C+中的唯一ptr中为普通数组生成迭代器+;11?

C++ 如何最好地在C+中的唯一ptr中为普通数组生成迭代器+;11?,c++,arrays,c++11,iterator,unique-ptr,C++,Arrays,C++11,Iterator,Unique Ptr,我想用unique_ptr所拥有的丰富的数组。 下面是我想写的代码,也是我目前必须写的代码: void question() { const int len = 10; int a[len]; unique_ptr<int[]> p(new int[len]); // i can do this with a bare array for_each(begin(a), end(a), [](int& v) { v = 0; }); // but this do

我想用unique_ptr所拥有的丰富的数组。 下面是我想写的代码,也是我目前必须写的代码:

void question() {
 const int len = 10;
 int a[len];
 unique_ptr<int[]> p(new int[len]);

 // i can do this with a bare array
 for_each(begin(a), end(a), [](int& v) { v = 0; });

 // but this doesn't compile cuz unique_ptr<T[]> doesn't implement dereference
 //  for_each(begin(*p), end(*p), [](int& v) { v = 0; });

 // this works, but ugly, and begin() and end() are not abstracted.
 for_each(&p[0], &p[len], [](int& v) { v = 0; });

 // how best to make iterators for an array within a unique_ptr?
}
void问题(){
常数int len=10;
int a[len];
唯一的(新的整数[len]);
//我可以用一个裸阵列来实现这一点
对于每个(开始(a),结束(a),[](int&v){v=0;});
//但这并不编译,因为unique_ptr不实现解引用
//对于每个(开始(*p),结束(*p),[](int&v){v=0;});
//这是可行的,但很难看,begin()和end()都不是抽象的。
对于_-each(&p[0],&p[len],](int&v){v=0;});
//如何最好地在唯一的\u ptr中为数组生成迭代器?
}
或者使用容器类而不是数组会更好吗

精化:

我的完整用例是一个缓冲区对象,它包含将传递给音频设备的音频样本的原始数组。数组的长度在缓冲区构造时确定,然后保持不变

我没有使用容器类,因为数据在内存中必须是连续的。但是我想迭代缓冲区,用数据填充它

#include <iostream>
#include <algorithm>
#include <cmath>
#include <iterator>

using namespace::std;

struct Buffer {
 unique_ptr<double[]> buf;
 size_t len;
 int frameRate;
 Buffer(size_t len) : buf(new double[len]), len(len) {}
};

class Osc {
 double phase, freq;
public:
 Osc(double phase, double freq) : phase(phase), freq(freq) {}
 void fill(Buffer& b) {
  double ph = phase;
  for_each(b.buf.get(), next(b.buf.get(), b.len), [&ph, &b](double& d) {
   d = sin(ph);
   ph += 1.0/b.frameRate;
  });
 }
};

int main() {
 Buffer buf(100);
 Osc osc(0, 440);
 osc.fill(buf);
 return 0;
}
#包括
#包括
#包括
#包括
使用namespace::std;
结构缓冲区{
独特的_ptrbuf;
尺寸透镜;
整数帧率;
缓冲区(size_t len):buf(新双精度[len]),len(len){
};
类Osc{
双相,频率;
公众:
Osc(双相,双频):相(相),频(频){
空隙填充(缓冲区和b){
双ph=相位;
对于每一个(b.buf.get(),下一个(b.buf.get(),b.len),[&ph,&b](double&d){
d=sin(ph);
ph+=1.0/b.帧率;
});
}
};
int main(){
缓冲器buf(100);
Osc Osc(0440);
osc.填充(buf);
返回0;
}
#包括
#包括
#包括
无效问题(){
常数int len=10;
std::unique_ptr p(新整数[len]);
int x=0;
std::for_each(std::next(p.get(),0),std::next(p.get(),len),[&](int&a){a=++x;};//使用std::next(p.get(),0)代替p.get()。

std::for_each(std::next(p.get(),0),std::next(p.get(),len),[](int a){std::cout如果
len
const
那么为什么需要动态分配数组?如果
len
不是
const
那么为什么不使用
std::vector
?就个人而言,我会坚持使用
std::array
std::vector
(取决于结构改变大小的需要)并且只有当外部强制函数(例如,外部API)强制进入原始数组时才使用原始数组。@user657267在我的裸数组上移除常量中断begin()/end()。为什么?T[n]和T[const n]之间有类型区别吗对不起,我不太清楚C++。如果数组是固定大小的,你不需要动态分配它,它是<代码>新的<代码>,只使用<代码> int p(10);< /Calp> <代码>:STD::数组p;<代码>。然后使用<代码> STD::向量< /代码>。@ SynLynO0:你对下面的答案不满意吗?但是为什么不开始呢?(p.get())返回迭代器?-检查函数重载。不存在返回指针迭代器的
std::begin
重载。只需进行编辑,即可获得起始和结束迭代器。这只是为了满足拥有迭代器的要求。正如其他人所说,可以使用
std::array
std::vector
而不是
std::unique\u ptr
。您不需要担心内存管理,也不需要在STL算法中使用它们,为什么
std::next(p.get(),0)
而不是
p.get())
?@solotor\u zero
std::begin/end
未指定用于裸指针,并且
unique\u ptr
也没有成员
begin/end
,因为它不存储托管数组的大小。不清楚您要查找的确切答案。您之前关于的注释只是进行了编辑,以便获得迭代start和end的rs似乎表明您对
std::next
的返回值类型有误解。它返回与第一个参数相同的迭代器类别,在本例中为
int*
。裸
T*
是一个非常好的RandomAccessIterator,在上面的代码中没有区别在写入
std::next(p.get(),0)
/
p.get()
std::next(p.get(),len)
/
p.get()+len
#include <iostream>
#include <algorithm>
#include <memory>

void question() {
 const int len = 10;

 std::unique_ptr<int[]> p(new int[len]);
 int x = 0;
 std::for_each(std::next(p.get(), 0), std::next(p.get(), len), [&](int& a) { a = ++x; }); // used std::next(p.get(), 0) instead of p.get().
 std::for_each(std::next(p.get(), 0), std::next(p.get(), len), [](int a) { std::cout << a << "\n" ;});

}

int main()
{
    question();
}