• 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类似,因此具有一定的研究意义。

  • 相关阅读:
    找实习之从0开始的后端学习日记【9.17】
    【目标检测】旋转目标检测DOTA格式转YOLO格式标注
    Python二进制序列类型(二)array、struct和memoryview
    【实战案例】技术转项目经理容易踩的坑,我都踩了
    导入JankStats检测卡帧库遇到问题记录
    java 字符串替换
    IDEA DeBug 调试工具详解
    TypeScript逆变 :条件、推断和泛型的应用
    状态(State)模式
    【洛谷算法题】B2029-大象喝水【入门1顺序结构】
  • 原文地址:https://blog.csdn.net/superfreak/article/details/126804373