【发布时间】:2017-07-16 00:23:29
【问题描述】:
我很难让 std::begin() 与动态分配的数组(指针)一起工作,而它似乎与堆栈分配的数组一起工作。
这行得通:
int numbers[100];
// Fill array with numbers
std::sort(std::begin(numbers), std::end(numbers));
这不是
int* numbers = new int[10000000];
// Fill array with numbers
std::sort(std::begin(numbers), std::end(numbers));
这是产生的错误。
ptests.cpp:120:33: error: no matching function for call to ‘begin(int*&)’
std::sort(std::begin(numbers), std::end(numbers));
^
ptests.cpp:120:33: note: candidates are:
In file included from /usr/include/c++/4.8/utility:74:0,
from /usr/include/c++/4.8/algorithm:60,
from ptests.cpp:1:
/usr/include/c++/4.8/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
begin(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.8/initializer_list:89:5: note: template argument deduction/substitution failed:
ptests.cpp:120:33: note: mismatched types ‘std::initializer_list<_Tp>’ and ‘int*’
std::sort(std::begin(numbers), std::end(numbers));
^
In file included from /usr/include/c++/4.8/string:51:0,
from /usr/include/c++/4.8/random:41,
from /usr/include/c++/4.8/bits/stl_algo.h:65,
from /usr/include/c++/4.8/algorithm:62,
from ptests.cpp:1:
/usr/include/c++/4.8/bits/range_access.h:48:5: note: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)
begin(_Container& __cont) -> decltype(__cont.begin())
是否可以将动态指针转换为 begin() 期望的类型?任何建议将不胜感激!
【问题讨论】:
-
在您的问题中包含您的错误文本。我不知道
.webp文件是什么。见How to Ask a Question 和How to create a Minimal, Complete, and Verifiable example