⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 36-1.cpp

📁 effective stl 源代码 code
💻 CPP
字号:
//
// Example from ESTL item 36
//

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#include "ESTLUtil.h"
#include "Widget.h"

bool isDefective(const Widget& w)
{
	return w.getVal() % 2 == 1;	// devective == odd
}


/*
template<typename InputIterator,		// a not-quite-right
		typename OutputIterator,		// implementation of
		typename Predicate>				// copy_if
OutputIterator copy_if(InputIterator begin,
			InputIterator end,
			OutputIterator destBegin,
			Predicate p)
{
	return remove_copy_if(begin, end, not1(p));
}
*/


template<typename InputIterator,	// a correct 
	typename OutputIterator,		// implementation of
	typename Predicate>				// copy_if
OutputIterator copy_if(InputIterator begin,
		InputIterator end,
		OutputIterator destBegin,
		Predicate p)
{
	while (begin != end) {
		if (p(*begin)) *destBegin++ = *begin;
		++begin;
	}
	return destBegin;
}


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


	vector<Widget> widgets;

	for (int j = 0; j < 10; ++j)	// insert some Widgets
		widgets.push_back(Widget(j));

	printContainer("widgets", widgets);

	copy_if(widgets.begin(), widgets.end(),		// this won't compile; 
		ostream_iterator<Widget>(cerr, " "),	// there is no copy_if
		isDefective);							// in the STL


	return 0;
}

⌨️ 快捷键说明

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