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

Commonly used vector APIs

The following table shows some commonly used vector APIs:

 

It would be really fun and convenient to read and print to/from the vector using istream_iterator and ostream_iterator. The following code demonstrates the use of a vector:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main () {
vector<int> v;

cout << "\nType empty string to end the input once you are done feeding the vector" << endl;
cout << "\nEnter some numbers to feed the vector ..." << endl;


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

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


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


return 0;
}

Note that the output of the program is skipped, as the output depends on the input entered by you. Feel free to try the instructions on the command line.
主站蜘蛛池模板: 呼伦贝尔市| 申扎县| 开阳县| 西和县| 广南县| 曲阜市| 瓦房店市| 无极县| 马山县| 文安县| 封开县| 长子县| 汶川县| 沙雅县| 洪泽县| 虹口区| 涟源市| 丁青县| 彰化县| 舟山市| 沁阳市| 正阳县| 渭南市| 肇州县| 亳州市| 许昌县| 大竹县| 伊吾县| 鸡泽县| 洪江市| 修水县| 石城县| 伽师县| 茂名市| 资兴市| 夏邑县| 农安县| 江津市| 城口县| 彰武县| 锡林浩特市|