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

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). 

主站蜘蛛池模板: 博湖县| 石河子市| 武乡县| 邹城市| 三台县| 汉川市| 顺昌县| 呼伦贝尔市| 吉水县| 博客| 云安县| 舒城县| 东城区| 会昌县| 岐山县| 伊金霍洛旗| 楚雄市| 北辰区| 九江县| 昆明市| 磐石市| 乐山市| 汝州市| 科尔| 鄂伦春自治旗| 库伦旗| 兴宁市| 共和县| 通州市| 翼城县| 乳山市| 梓潼县| 临桂县| 始兴县| 雅江县| 盈江县| 凯里市| 潞西市| 桦川县| 阳高县| 灵石县|