容器迭代器
容器迭代器
iterator:普通正向迭代器
-
可以读取
-
可以修改
int main() {vector<int> vec;for (int i = 0; i < 20; i++){vec.push_back(rand() % 100 + 1);}auto it = vec.begin();for ( ;it != vec.end();it++ ){if (*it % 2 == 0){*it = 0;}}for (int val : vec)cout << val << " ";cout << endl;return 0; }
const_iterator:常量迭代器
const_iterator 继承自iterator
-
只读
-
不写
int main() {vector<int> vec;for (int i = 0; i < 20; i++){vec.push_back(rand() % 100 + 1);}vector<int>::const_iterator it = vec.begin();for ( ;it != vec.end();it++ ){if (*it % 2 == 0){cout << *it << " ";} }cout << endl;return 0; }
reverse_iterator 反向迭代器
反向遍历容器的元素
int main()
{vector<int> vec;for (int i = 0; i < 20; i++){vec.push_back(rand() % 100 + 1);}vector<int>::const_iterator it = vec.begin();for ( ;it != vec.end();it++ ){cout << *it << " "; }cout << endl;vector<int>::reverse_iterator rit = vec.rbegin();for (; rit != vec.rend(); rit++){cout << *rit << " ";}cout << endl;return 0;
}
const_reverse_iterator 常量反向迭代器
- 反向遍历
- 只支持读,不能写