三、反向迭代器【續:習題】
//P355 習題11.19 int main() { vectoriVec; for (vector ::size_type index = 0; index != 10; ++index) { iVec.push_back(index); } for (vector ::reverse_iterator r_iter = iVec.rbegin(); r_iter != iVec.rend(); ++r_iter) { cout << *r_iter << endl; } }
//習題 11.20 int main() { vectoriVec; for (vector ::size_type index = 0; index != 10; ++index) { iVec.push_back(index); } for (vector ::iterator iter = iVec.end()-1;iter >= iVec.begin(); --iter) { cout << *iter << endl; } }
//習題 11.21 int main() { int ia[] = {0,1,2,3,4,5,6,0,7,8,9,0,10}; vectoriVec(ia,ia + sizeof(ia)/sizeof(*ia)); vector ::reverse_iterator r_iter = find(iVec.rbegin(),iVec.rend(),0); if (r_iter != iVec.rend()) { cout << "element after the last 0 : " << *(-- r_iter) << endl; } else { cout << "Not found!" << endl; } }
//習題11.22 int main() { int ia[] = {0,1,2,3,4,5,6,0,7,8}; vectoriVec(ia,ia + sizeof(ia)/sizeof(*ia)); list iList; vector ::reverse_iterator rbeg = iVec.rbegin(),rend; for (int i = 0; i != 3; ++i) ++ rbeg; rend = rbeg; for (int i = 0; i != 5; ++i) ++rend; copy(rbeg,rend,back_inserter(iList)); for (list ::iterator iter = iList.begin(); iter != iList.end(); ++iter) { cout << *iter << endl; } }
四、const迭代器