iterator初学者提问
iterator菜鸟提问
求大家帮个忙 小弟不解的是 为什么return NULL 这个编译错误啊 错误是
------解决方案--------------------
不能想当然地认为vector<int>::iterator是int*类型.虽然这么做也很合情合理.为了加强debug时期的查错能力,现在很多STL的实现都是把vector的iterator实现为自定义类类型.至少调试版本如此
------解决方案--------------------
iterator因为有重载操作符可以当指针一样操作,但不能直接和指针划等号。
vector<int>::iterator searchValue(vector<int>::iterator first,vector<int>::iterator last, int value){
while(first!=last){
if(*first==value) return first;
first++;
}
return NULL;
}
求大家帮个忙 小弟不解的是 为什么return NULL 这个编译错误啊 错误是
13:52:51 **** Incremental Build of configuration Debug for project Exercise ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "scr\\test.o" "..\\scr\\test.cpp"
..\scr\test.cpp: In function 'std::vector<int>::iterator searchValue(std::vector<int>::iterator, std::vector<int>::iterator, int)':
..\scr\test.cpp:12:29: error: conversion from 'int' to non-scalar type 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' requested
..\scr\test.cpp: In function 'int main()':
..\scr\test.cpp:27:55: error: cannot convert 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' to 'int*' in initialization
------解决方案--------------------
不能想当然地认为vector<int>::iterator是int*类型.虽然这么做也很合情合理.为了加强debug时期的查错能力,现在很多STL的实现都是把vector的iterator实现为自定义类类型.至少调试版本如此
------解决方案--------------------
iterator因为有重载操作符可以当指针一样操作,但不能直接和指针划等号。