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


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

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

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

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

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

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

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

    1. #include
    2. ...
    3. 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

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

    运行结果:

    1. Let the dice be cast!
    2. Let have a look at the dice now !
    3. 6
    4. 7
    5. 2
    6. 9
    7. ....
    8. 6 7 2 9 4 11 8 7 10 5
    9. Implicict use of reverse iterator.
    10. 5 10 7 8 11 4 9 2 7 6
    11. Explicict use of reverse iterator.
    12. 5 10 7 8 11 4 9 2 7 6
    13. 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类似,因此具有一定的研究意义。

  • 相关阅读:
    cf1695D1. Tree Queries (Easy Version)(div2)【树上问题】
    工资支付系统可行性研究报告
    压缩图片怎么弄?这些方法我逢人必推
    Postman 调用 Microsoft Graph API (InsCode AI 创作助手)
    DataX-web安装部署和使用
    Android | LiveData 源码分析
    用户体验设计师是什么,一篇文章读懂!
    数据结构(超详细讲解!!)第二十一节 特殊矩阵的压缩存储
    HTTPS优化——协议优化,证书优化,会话复用
    vue中的watch的实际开发笔记
  • 原文地址:https://blog.csdn.net/superfreak/article/details/126804373