C++PrimerPlus(第6版)中文版:Chapter16.4泛型编程_概念_改进和模型_copyit.cpp

本文介绍了C++ Standard Template Library (STL) 中的概念和模型,特别是迭代器的使用。通过示例展示了如何使用copy()函数复制数据,以及预定义的输出迭代器ostream_iterator和输入迭代器istream_iterator。还探讨了反向迭代器的隐式和显式使用,并强调了STL函数的通用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STL文献使用概念(concept)来描述一系列的要求。

概念的具体实现叫做模型(model)。

C++提供了一些预定义迭代器。如果想了解其中原因,需要学习一个背景知识。有一种算法(名为copy())可以将数据从一个容器复制到另外一个容器中。这种算法是以迭代器方式实现的。

例如下面的代码将一个数组复制到另外一个vector中:

int casts[10]={6,8,9,10,8,4,8,9,2,3};
vector <int> dice(10);
copy(casts,casts+10,dice.begin());

copy()的前两个迭代器参数表示要复制的范围,最后一个迭代器参数表示要将第一个元素复制到什么位置。前两个参数必须是(最好是)输入迭代器,最后一个参数必须是(或最好是)输出迭代器。copy()函数将覆盖目标容器中已有的数据,同时目标容器必须足够大,以便能够容纳被复制的元素。

现在,假设要将信息复制到显示器上。如果有一个表示输出流的迭代器,则可以使用copy()。STL为这种迭代器提供了ostream_iterator 模版。用STL的话说,该模版是输出迭代器的一个模型,它也是一个适配器(adapter)-----一个类或函数,可以将一些其他接口转换为STL使用的接口。可以用以下语句创建这种迭代器:

#include <iterator>
...
ostream_iterator<int ,char> out_iter(cout," ");

out_iter现在是一个接口,让您能够使用cout来显示信息。第一个模版参数(这里为int)指出了被发送给输出流的数据类型,第二个模版参数(char)指出流使用的字符类型(另一个可能的值为wchar_t)。构造函数的第一个参数(cout)指出了要使用的输出流,它还可以是用于文件输出的流。最后一个字符串参数是发送给输出流的每个数据项后显示的分隔符。

可以将copy用于迭代器:

copy(dice.begin(),dice.end(),out_iter) 

这句话的意思是将dice容器的整个区间复制到输出流中,即显示出容器的内容。

iterator头文件还定义了一个istream_iterator模版,是istream输入可以用作迭代器接口,它是一个输入迭代器概念的模型。

还有其他的预定义的迭代器类型它们是:reverse_iterator、back_insert__iterator、front_insert_iterator、front_insert_iterator和insert_iterator。

例子:copyit.cpp

#include <iostream>
#include <iterator>
#include <vector>
#include "iterator.cpp"

int main()
{
   // std::cout << "Hello World!\n";
	using namespace std;
	int casts[10] = { 6,7,2,9,4,11,8,7,10,5 };
	vector<int> dice(10);
	//copy from array to vector 
	copy(casts,casts+10,dice.begin());
	cout << "Let the dice be cast!\n";
	cout << "       Let have a look at the dice now !\n";
	cout << "      "<<"    "<<dice[0] << endl;
	cout << "      "<<"    "<<dice[1] << endl;
	cout << "      "<<"    " << dice[2] << endl;
	cout << "      "<<"    " << dice[3] << endl;
	cout << "      "<<"    " <<"...."<< endl;
	//create an ostream itertor 创建了一个ostream 迭代器
	ostream_iterator<int, char> out_iter(cout, " ");
	//第一个模版参数 (这里为int)指出了被发送给输出流的数据类型
	//第二个模版参数(这里为char)指出了输出流使用的字符类型(另一个可能的值是wahar_t)。
	// 构造函数的第一个参数(这里为cout)指出了要使用的输出流 ,它也可以是用于文件输出的流,
	// 最后一个字符串参数是分隔符,现在结果是:6 7 2 9 4 11 8 7 10 5   如果使用* ,则输出为:6*7*2*9*4*11*8*7*10*5*
	//copy from vector to output
	copy(dice.begin(),dice.end(),out_iter);//这句话会复制dice的内容到cout,输出:6 7 2 9 4 11 8 7 10 5
	cout << endl;
	cout << "Implicict use of reverse iterator.\n";//隐式使用反向迭代器,含蓄地使用反向迭代器
	copy(dice.rbegin(),dice.rend(),out_iter);////这句话会反向复制dice的内容到cout,输出:5 10 7 8 11 4 9 2 7 6
	cout << endl;
	cout << "Explicict use of reverse iterator.\n";//显式使用反向迭代器,明目张胆地使用反向迭代器
	vector<int>::reverse_iterator ri;
	for (ri = dice.rbegin(); ri != dice.rend(); ++ri)
		cout << *ri << ' ';
	cout << endl;
	cout << "Note:Explicict use of reverse iterator or use STL funtion to Implicict use of reverse iterator?" <<
		"You should better choose the STL funtion,for the less code work and the less error! ";
	return 0;
}

运行结果:

Let the dice be cast!
       Let have a look at the dice now !
          6
          7
          2
          9
          ....
6 7 2 9 4 11 8 7 10 5
Implicict use of reverse iterator.
5 10 7 8 11 4 9 2 7 6
Explicict use of reverse iterator.
5 10 7 8 11 4 9 2 7 6
Note:Explicict use of reverse iterator or use STL funtion to Implicict use of reverse iterator?You should better choose the STL funtion,for the less code work and the less error!

很多STL函数都和copy类似,因此具有一定的研究意义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值