???????????????????????????????????????????????????±????vector?????????
??????????????????????????????????????? vector??
????vector<int>::iterator iter;
???????????????????? iter ????????????????????? vector<int> ????? iterator ??????????????????????????????????? iterator ??????????? iterator ???????????????????????
????begin ?? end ????
??????????????????????????? begin ?? end ??????????????????????????????????????????? begin ?????????????????????
????vector<int>::iterator iter = ivec.begin();
???????????? iter ??????????? vector ??????????????? vector ????????????iter ????????? ivec[0]??
?????? end ????????????????? vector ??“????????????”?????????????????????????????? vector ????begin ???????????? end ????????????????? end ????????????????????? vector ?в???????????????????????????????????sentinel???????á??????????????? vector ?????????
??????????????ò???begin??end??????е?????????????e????
????vector ???????????????????????
?????????????????y????ò???????dereference operator????*??????????????????????
????*iter = 0;
?????????ò?????????????????????????????? iter ??? vector ???? ivec ??????????? *iter ?? ivec[0] ????????????????????????Ч???????????????? 0??
????????????????????????????????????????????????????????????????????????????????? int ????????????????????? int ??????????????????? int ???“?? 1”?????????????????????????е??????“?????????λ??”?????????? iter ????????????? ++iter ???????????
??????? end ???????????????????????????????????????н????????????????
????????????????????
???????????????????????????????????? == ?? != ??????????????????????????????????????????????????????????????????????
????????????????????????
????1?????????????±???vector??????
??????????easy????????
#include <iostream>
#include <string>
#include <vector>
int print_int_vector(std::vector<int> ivec)
{
for(std::vector<int>::size_type ix =0?? j = 0; ix != ivec.size(); ++ix?? ++j)
{
std::cout<<ivec[ix]<<" "; //????
}
std::cout<<std::endl;
return 0;
}
int main()
{
std::vector<int> ivec(10?? 68); // empty vector
print_int_vector(ivec);
// reset all the elements in ivec to 0
/*
// ????±?
for (std::vector<int>::size_type ix = 0; ix != ivec.size(); ++ix)
{
ivec[ix] = 0;
}
*/
// equivalent loop using iterators to reset all the elements in ivec to 0
for (std::vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
*iter = 0; // set element to which iter refers to 0
print_int_vector(ivec);
return 0;
}
????2??tuple?????????????????
????const_iterator??????????????????????????????????????
?????????????? iterator ????????????????????????? const????????????? const_iterator ?????????????????????????? const ??????????????????????????????????????????д??
???????????const_itreator??????д????????????
??????? const_iterator ??????????????????????????????????????????????????????????????????????
???????????????????????????y????ò????????????????????????????
?????????????? const_iterator ?????? const ?? iterator ???????????????????? const ????????????????????????????????????????????????????
????vector<int> nums(10); // nums is nonconst
????const vector<int>::iterator cit = nums.begin();
????*cit = 1; // ok: cit can change its underlying element
????++cit; // error: can't change the value of cit
?????????const_iterator ??????????? const vector ??? const vector??????????д??????
????const ???????????????????????????????????????????????????????д????????????????????????????????????
????tuple???????????????const??????const vector<int> nums(10?? 9);
???????
????1??const_iterator?????????vector???????????????????const_iterator????????????????
????2??const????????????????????????????????????++???????????????????????????????????????
????3?????????????tuple??????const vector<int> nums(10?? 9);?????壡
????????????????const_iterator ????????????????