1.常规用于从前往后遍历的迭代器iterator
std::vector<int> vec;
for (int i = 0; i < 4; i++) {
vec.push_back(i);
}
for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
std::cout << *it << std::endl;
}
2.用于倒序遍历的迭代器reverse_iterator
std::vector<int> vec;
for (int i = 0; i < 4; i++) {
vec.push_back(i);
}
for (std::vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rend(); it++) {
std::cout << *it << std::endl;
}
3.只读不能赋值的迭代器const_iterator
std::vector<int> vec;
for (int i = 0; i < 4; i++) {
vec.push_back(i);
}
printVec(vec)
void printVec(const std::vector<int> &vec) {
for (std::vector<int>::const_iterator it = vec.begin(); it != vec.end(); it++) {
std::cout << *it << std::endl;
}
}