C++:偏移到 std::vector 迭代器的正确转换是什么?

标签 c++ c++11 vector offset static-cast

我有一个函数,它接受 double 的 std::vector,并将它们复制到另一个 vector ,但在特定的偏移量处(假设有足够的空间):

void copy_stuff(const std::vector<double> & data,
                std::vector<double> & dest,
                size_t dest_offset) {
    std::copy(data.begin(), data.end(), dest.begin() + dest_offset);
}

这导致 C++11 clang 编译器 -Weverything 警告集中在 + dest_offset 部分:

Implicit conversion changes signedness: 'size_t' (aka 'unsigned long') to 'difference_type' (aka 'long').

我不确定应该如何转换表达式 dest.begin() + dest_offset 以消除此警告。将结果转换为 double * 不会编译:

    std::copy(data, data + data_size, static_cast<double *>(dest.begin() + dest_offset));

Cannot cast from type 'std::__1::__wrap_iter' to pointer type 'double *'.

我考虑过使用 vector 索引然后取地址:

    std::copy(data, data + data_size, &dest[dest_offset]);

这似乎消除了这种情况下的警告,但如果我尝试对源 vector 使用相同的模式,即 std::copy 的第一个或第二个参数涉及的偏移量,则不会编译。例如:

static void copy_stuff_differently(const std::vector<double> & data,
                                   std::vector<double> & dest,
                                   size_t offset) {
    std::copy(data.begin() + offset, data.end(), dest.begin());
}

+ offset 上给出与隐式转换相同的原始警告。尝试使用索引地址可能会提示:

    std::copy(&data[offset], data.end(), dest.begin());

或者不同但相似的情况:

    std::copy(data.begin(), &data[offset], dest.begin());

但是两者都会导致类似的错误:

test.cpp:8:3: error: no matching function for call to 'copy'
  std::copy(&data[offset], data.end(), dest.begin());
  ^~~~~~~~~
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1286:48: note:
      candidate template ignored: deduced conflicting types for parameter '_Ip' ('const double *' vs.
      'std::__1::__wrap_iter<const double *>')
    template <class _Ip, class _Op> friend _Op copy(_Ip, _Ip, _Op);
                                               ^

我正在寻找一种一致且无警告的方式来处理此类偏移。处理 vector 偏移并避免此类错误和警告的正确方法是什么?

最佳答案

I'm not sure how I should cast the expression dest.begin() + dest_offset in order to eliminate this warning.

警告只是告诉你 dest_offset 应该是 std::vector::difference_type 类型,但它是 size_t .

您可以显式进行转换以消除警告(请注意,如果源值无法在 difference_type 中表示,则结果由实现定义)。例如

dest.begin() + static_cast<std::vector<double>::difference_type>(dest_offset)

或者从一开始就声明类型为difference_type的参数dest_offset

请注意 std::vector::difference_type是有符号整数类型(通常为 std::ptrdiff_t ),不同于 size_t ;这是一个无符号整数类型。

关于C++:偏移到 std::vector 迭代器的正确转换是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480794/

相关文章:

C++11 外部作用域变量声明为 auto

c++ - 无法访问 std::move(...) C++ 11

c++ - 如何提取/扩展可变模板参数

c++ - 多个数组操作

c++ - 将安慰专有的二进制消息获取到 sscanf 中

c++ - VC++ 错误 : array instantiation with size from templated static const

c++ - Box2d cpp 给出 : box2d expression area > 1. 1 异常

c++ - 在图像 OpenCV 上写非常小的字符?

python - 将矢量数据与 numpy 相关联

c++11 - 在部分副本中使用 for_each