C++stl 向量,链表,栈,队列(vector, list, stack, queue)

随机存取的向量-vector

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

bool comp(const int &a, const int &b)
{
    return a > b;
}

void VectorMain()
{
    // constructors used in the same order as described above:
    vector<int> first;                                // empty vector of ints
    vector<int> second(4, 100);                       // four ints with value 100
    vector<int> third(second.begin(), second.end());  // iterating through second
    vector<int> fourth(third);                       // a copy of third

    // the iterator constructor can also be used to construct from arrays:
    int myints[] = { 16,2,77,29 };
    vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));

    cout << "The contents of fifth are:";
    for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';

    // sort
    sort(fifth.begin(), fifth.end());
    cout << "The ascend contents of fifth are:";
    for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';

    // descend
    sort(fifth.begin(), fifth.end(), comp);
    cout << "The descend contents of fifth are:";
    for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
        cout << ' ' << *it;
    cout << '\n';

    // push_back, pop_back, empty, clear
}

链表

#include <iostream>
#include <cmath>
#include <vector>
#include <list>
using namespace std;

// a binary predicate implemented as a function:
bool same_integral_part(double first, double second)
{
    return (int(first) == int(second));
}

// a binary predicate implemented as a class:
struct is_near {
    bool operator() (double first, double second)
    {
        return (fabs(first - second)<5.0);
    }
};

void ListMain()
{
    // constructors used in the same order as described above:
    std::list<int> first;                                // empty list of ints
    std::list<int> second(4, 100);                       // four ints with value 100
    std::list<int> third(second.begin(), second.end());  // iterating through second
    std::list<int> fourth(third);                       // a copy of third

    double mydoubles[] = { 12.15,  2.72, 73.0,  12.77,  3.14,
        12.77, 73.35, 72.25, 15.3,  72.25 };
    std::list<double> mylist(mydoubles, mydoubles + 10);

    mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,
                               // 15.3,  72.25, 72.25, 73.0,  73.35

    mylist.unique();           //  2.72,  3.14, 12.15, 12.77
                               // 15.3,  72.25, 73.0,  73.35

    mylist.unique(same_integral_part);  //  2.72,  3.14, 12.15
                                        // 15.3,  72.25, 73.0

    mylist.unique(is_near());           //  2.72, 12.15, 72.25

    std::cout << "mylist contains:";
    for (std::list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    ///insert/
    std::list<int> mylist1;
    std::list<int>::iterator it;

    // set some initial values:
    for (int i = 1; i <= 5; ++i) mylist1.push_back(i); // 1 2 3 4 5

    it = mylist1.begin();
    ++it;       // it points now to number 2           ^

    mylist1.insert(it, 10);                        // 1 10 2 3 4 5

                                                  // "it" still points to number 2                      ^
    mylist1.insert(it, 2, 20);                      // 1 10 20 20 2 3 4 5

    --it;       // it points now to the second 20            ^

    std::vector<int> myvector(2, 30);
    mylist1.insert(it, myvector.begin(), myvector.end());
    // 1 10 20 30 30 20 2 3 4 5
    //               ^
    std::cout << "mylist contains:";
    for (it = mylist1.begin(); it != mylist1.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    // front(), back(), push_back(value), pop_back(), push_front(value), pop_front(), empty(), clear(), remove(value)
}

stack:

push(value) //向容器顶部插入元素
pop() //删除容器顶部的元素
top() //返回容器顶部的元素
size() //返回容器的元素个数
empty() //检查是否为空

queue:

back() //返回队列最后一个元素的引用
empty() //检查是否为空
front() //返回队列第一个元素的引用
push(value) //队列尾添加一个元素
pop() //删除队列第一个元素
size() //返回队列的元素个数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值