官术网_书友最值得收藏!

  • Mastering C++ Programming
  • Jeganathan Swaminathan
  • 136字
  • 2021-07-02 18:28:50

Code walkthrough

Basically, the copy algorithm accepts a range of iterators, where the first two arguments represent the source and the third argument represents the destination, which happens to be the vector:

istream_iterator<int> start_input(cin);
istream_iterator<int> end_input;

copy ( start_input, end_input, back_inserter( v ) );

The start_input iterator instance defines an istream_iterator iterator that receives input from istream and cin, and the end_input iterator instance defines an end-of-file delimiter, which is an empty string by default ("").  Hence, the input can be terminated by typing "" in the command-line input terminal.

Similarly, let's understand the following code snippet:

cout << "\nPrint the vector ..." << endl;
copy ( v.begin(), v.end(), ostream_iterator<int>(cout, "\t") );
cout << endl;

The copy algorithm is used to copy the values from a vector, one element at a time, to ostream, separating the output with a tab character (\t). 

主站蜘蛛池模板: 汉川市| 自贡市| 阿克苏市| 巩义市| 广平县| 贵德县| 嵩明县| 定西市| 阳新县| 手游| 泾川县| 玉山县| 元氏县| 丰台区| 政和县| 武清区| 南平市| 扎鲁特旗| 威信县| 太原市| 商水县| 新源县| 辉县市| 肃南| 太康县| 罗甸县| 乌鲁木齐市| 桂林市| 囊谦县| 翼城县| 凤山县| 黄龙县| 仁化县| 仁寿县| 天峨县| 嫩江县| 依兰县| 苍南县| 盱眙县| 靖边县| 贵阳市|