我想使用C++數組的迭代器,但也使用原始指針。 我可以用一個靜態的載體做:stl ::與原始指針的迭代器
#define SIZE 10
int vect[SIZE] = {0};
vect[3] = 5;
int* p = std::find(std::begin(vect), std::end(vect), 5);
bool success = p != std::end(vect);
怎麼可能與原始指針(也許堆上分配向量)辦呢? 當然,編譯器不知道數據的大小,因此,這段代碼
int* pStart = vect;
std::find(std::begin(pStart), std::end(pStart), 5);
給
error C2784: '_Ty *std::begin(_Ty (&)[_Size])' :
could not deduce template argument for '_Ty (&)[_Size]' from 'int *'
是否有可能使begin()
和end()
意識到這一點?
怎麼可能做_what_?你想達到什麼目的?你的目標是什麼? –
改爲使用'std :: array',它具有C數組的所有功能,但實際上具有STL接口。它的迭代器被實現爲指針。 – rlbond
@rlbond除了在這種情況下,普通數組是完全正確的。 – juanchopanza