题解 | #迭代器遍历容器#
迭代器遍历容器
https://www.nowcoder.com/practice/0f7ab22e60ee4574a9d9c81412b26595
#include <iostream> // write your code here...... #include <vector> using namespace std; int main() { // write your code here...... vector<int> arr{}; int data{},n{5}; while (n--) { std::cin>>data; arr.push_back(data); } //只遍历不修改数据,保证数据不变,使用带有const的遍历 //decltype()判定类型只与表达式相关,auto则与赋值相关 decltype(auto) iter_front{arr.cbegin()}; decltype(auto) iter_back{arr.crbegin()}; for(;iter_front!=arr.cend();iter_front++){ cout<<*iter_front<<" "; } cout<<endl; for(;iter_back!=arr.crend();iter_back++){ cout<<*iter_back<<" "; } return 0; }