如何用std::distance对c风格数组类型使用std::copy输出

How to use std::copy output with std::distance for a C-style array type

本文关键字:std 类型 copy 数组 输出 何用 distance 风格      更新时间:2023-10-16

如何存储从std::copy返回的OutputIterator用作以后std::distance调用的参数?

我不能在c++ 11中使用auto,我需要使用C风格的数组。

这就是我要做的:

unsigned char data[MAX_DATA_LEN];
unsigned char x[MAX_X_LEN], y[MAX_Y_LEN];
// I cannot use auto here
auto out =
  std::copy ( x,
              x + runtime_x_len ,
              std::copy ( y,
                          y + runtime_y_len ,
                          data ) );
size_t data_size = std::distance ( data , out );

对于c风格的数组,返回的'迭代器'只是一个指向元素类型的指针。

那么在您的例子中,std::copy()的返回值将是unsigned char*