STL简介


容器类

algorithm中的函数

#include
using namespace std;

一些示例

queue

#include
#include
using namespace std;
/*push(); pop(); empty(); size()*/
/*queue: front(), back();*/
/*priority_queue && stack: top()*/
struct fruite
{
    string name;
    double price;

    /*overloading '>' is prohibited*/
    friend bool operator < (fruite a, fruite b)
    {
        return a.price < b.price;
    }
};

int main()
{
    /*queue*/
    queue q;
    if(q.empty() == true) cout<<"Queue is Empty!\n";
    else cout<<"Queue is not Empty!\n";
    for(int i = 0; i < 5; i++)
        q.push(i);
    cout< p; 
        // priority_queue, less) p;
        // priority_queue, greater> p;
    p.push(1);
    p.push(10);
    p.push(7);
    p.push(6);
    cout<<"size = "< f;
    struct fruite f1, f2, f3;
    if(f.empty() == true) cout<<"f is Empty!"<

string

#include
#include
#include
using namespace std;
int main()
{
    string s = "abc";
    cin>>s;
    cout<str2) cout<<"str1>str2"<

reverse

#include
#include
using namespace std;
int main()
{
    int a[10] = {10,11,12,13,14,15};
    reverse(a, a+4);
    for(int i = 0; i < 6; i++)
        printf("%d ", a[i]);
}

sort

#include
#include
using namespace std;
#define n 7
bool cmp(int a, int b)
{
    return a > b;
}
int main()
{
    int i,j,k;
    int a[7] = {3,23,4,42,43,12,34};
    char s[7] = {'a','c','q','e','w','h','e'};
    sort(a,a+n,cmp);
    sort(s,s+n);
    for(i = 0; i < 7; i++)
    printf("%d ", a[i]);
    printf("\n");
    for(i = 0; i < 7; i++)
    printf("%c ", s[i]);
}

cmp函数

//从大到小排序
bool cmp(int a, int b) return a > b;
bool cmp(char a, char b) return a > b;
//结构体数组排序
/*struct node包含x,y两个成员*/
bool cmp(node a, node b) return a.x > b.x;
/*若a.x == b.x则比较y*/
bool cmp(node a, node b)
{
    if(a.x != a.x) return a.x > b.x;
    else    return a.y > b.y;
}

相关