标准模板库(STL)

来源:互联网 发布:乐乎城市青年公寓黄村 编辑:程序博客网 时间:2024/05/22 17:55

标准模板库(STL)

以矢量vector为例,如下程序是begin()、end()、push_back、pop_back()、erase()、insert()、sort()的用法:
#include <iostream>#include <algorithm>#include <cstdlib>#include <vector>using namespace std;vector <int>::iterator p;void show(vector <int> &a){    for(p=a.begin();p!=a.end();p++)        cout<<*p<<' ';    cout<<endl<<endl;}bool cmp(int a,int b){    return a>b;}int main(){    vector <int> sb;    p=sb.begin();    int i;    cout<<"Create the list , 0 to finish:"<<endl;    while(cin>>i&&i)    {        sb.push_back(i);        p++;    }    show(sb);    cout<<"Push_back a price :"<<endl;    cin>>i;    sb.push_back(i);    show(sb);    cout<<"Delete the last one :"<<endl;    sb.pop_back();    show(sb);    cout<<"The reserve order sort:"<<endl;    sort(sb.begin(),sb.end(),cmp);    show(sb);    cout<<"Erase:"<<endl;//delete a interval of 3th to 5th    sb.erase(sb.begin()+2,sb.begin()+5);    show(sb);    cout<<"Insert:"<<endl;//insert a interval of first to 3rd itself    sb.insert(sb.begin(),sb.begin()+3,sb.begin()+6);    show(sb);    cout<<"The size of sb: "<<sb.size()<<endl;    return 0;}