Loading

STL deque 代码

#include <iostream>
#include <deque>

using namespace std;

int main(int argc, char* argv[])
{
    deque<int> deq;
    for (int i = 0; i<6; i++)
    {
        deq.push_back(i);
    }

    cout << deq.size() << endl; // 输出:6
    cout << deq.max_size() << endl; // 输出:1073741823
    deq.resize(0); // 更改元素大小
    cout << deq.size() << endl; // 输出:0
    if (deq.empty())
        cout << "元素为空" << endl; // 输出:元素为空

    return 0;
}
#include <iostream>
#include <deque>

using namespace std;

int main(int argc, char* argv[])
{
    deque<int> deq;
    for (int i = 0; i < 8; i++)
        deq.push_back(i);

    // 头部删除元素
    deq.pop_front();
    // 末尾删除元素
    deq.pop_back();
    // 任意位置删除一个元素
    deque<int>::iterator it = deq.begin();
    deq.erase(it);
    // 删除[first,last]之间的元素
    deq.erase(deq.begin(), deq.begin()+1);

    // 遍历显示
    for (it = deq.begin(); it != deq.end(); it++)
        cout << *it << " ";
    cout << endl;

    // 清空所有元素
    deq.clear();

    // 遍历显示
    for (it = deq.begin(); it != deq.end(); it++)
        cout << *it << " "; // 输出:3 4 5 6
    cout << endl;

    return 0;
}
#include <iostream>
#include <deque>

using namespace std;

int main(int argc, char* argv[])
{
    deque<int> deq;
    for (int i = 0; i < 6; i++)
        deq.push_back(i);

    // 下标访问
    cout << deq[0] << endl; // 输出:0
    // at方法访问
    cout << deq.at(0) << endl; // 输出:0
    // 访问第一个元素
    cout << deq.front() << endl; // 输出:0
    // 访问最后一个元素
    cout << deq.back() << endl; // 输出:5

    return 0;
}
#include <iostream>
#include <deque>

using namespace std;

int main(int argc, char* argv[])
{
    // 多个元素赋值
    deque<int> deq;
    deq.assign(3, 1);
    deque<int> deq2;
    deq2.assign(3, 2);

    // 交换两个容器的元素
    deq.swap(deq2);

    // 遍历显示
    cout << "deq: ";
    for (int i = 0; i < deq.size(); i++)
        cout << deq[i] << " "; // 输出:2 2 2
    cout << endl;

    // 遍历显示
    cout << "deq2: ";
    for (int i = 0; i < deq2.size(); i++)
        cout << deq2[i] << " "; // 输出:1 1 1
    cout << endl;

    return 0;
}
#include <algorithm>
#include <deque>
#include<iostream>
using namespace std;
int main()
{
	deque<int> a;
	deque<int>::iterator it;
	for(it=a.begin();it<=a.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
	a.clear();
	for(int i=1;i<=10;i++)
	{
		a.push_back(i);
	}
	for(it=a.begin();it<a.end();it++)
	{
		cout<<*it<<" ";
	}
	cout<<endl;
	reverse(a.begin(), a.end());
	for(it=a.begin();it<a.end();it++)
	{
		cout<<*it<<" ";
	}
 } 
#include <iostream>
#include <deque>

using namespace std;

int main(int argc, char* argv[])
{
    deque<int> deq;
    deq.push_back(1);
    deq.push_back(2);
    deq.push_back(3);

    cout << *(deq.begin()) << endl; // 输出:1
    cout << *(--deq.end()) << endl; // 输出:3
    cout << *(deq.cbegin()) << endl; // 输出:1
    cout << *(--deq.cend()) << endl; // 输出:3
    cout << *(deq.rbegin()) << endl; // 输出:3
    cout << *(--deq.rend()) << endl; // 输出:1
    cout << endl;

    return 0;
}
#include <algorithm>
sort(deq.begin(), deq.end()); // 采用的是从小到大的排序

// 如果想从大到小排序,可以采用先排序后反转的方式,也可以采用下面方法:
// 自定义从大到小的比较器,用来改变排序方式
bool Comp(const int& a, const int& b) {
    return a > b;
}

sort(deq.begin(), deq.end(), Comp);
deque<int>::iterator it;
for (it = deq.begin(); it != deq.end(); it++)
    cout << *it << endl;
// 或者
for (int i = 0; i < deq.size(); i++) {
    cout << deq.at(i) << endl;
}
posted @ 2020-01-22 21:26  hyl天梦  阅读(156)  评论(0编辑  收藏  举报