28-1.cpp

来自「effective stl 源代码 code」· C++ 代码 · 共 45 行

CPP
45
字号
//
// Example from Item 28
//

#include <iostream>
#include <vector>

#include "ESTLUtil.h"


int main()
{
	using namespace std;
	using namespace ESTLUtils;

	vector<int> v;
	v.reserve(5); // see Item 14
	for (int j = 1; j <= 5; ++j) {			// put 1-5 in the vector
		v.push_back(j );
	}

	printContainer("v", v);

	vector<int>:: reverse_iterator ri =		// make ri point to the 3
			find(v.rbegin(), v.rend(), 3);
	vector<int>::iterator i(ri.base());		// make i the same as ri抯 base

	cout << "After finding the 3, *i = " << *i << ", *ri = " << *ri << endl;


/*
	v.erase(--ri.base());					// attempt to erase at the position
											// preceding ri.base(); for a vector,
											// this will typically not compile
											// (With VC++, compiles w/Dinkumware
											// library, but not the stock library)
*/

	v.erase((++ri).base());					// erase the element pointed to
											// ri; this should always compile
	printContainer("v", v);

	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?