算法常用C++总结

栈(stack)

stack实现了一种先进后出的数据结构,使用时需要包含stack头文件

C++定义一个stack:

stack<int> s;  //int为栈的数据类型,也可以为string,double等

C++中stack的基本操作有:

1、出栈:如 s.pop() 注意并不返回出栈的元素 
2、进栈:如 s.push(x) 
3、访问栈顶元素:如s.top(); 
4、判断栈空:如 s.empty() 栈为空时返回true 
5、返回栈中元素个数,如:s.size()

例子:

#include <iostream>
#include <stack>

using namespace std;

int main(int argc, char const *argv[])
{
    stack<int> s;

    for (int i = 0; i < 10; i++) {
        s.push(i);
    }
    cout << s.empty() << endl;

    for (int i = 0; i < 10; i++) {
        cout << s.top() << endl;
        s.pop();
    }
    cout << s.empty() << endl;

    return 0;
}

动态数组(vector)

C++中的vector是一个可以改变大小的数组,当解题时无法知道自己需要的数组规模有多大时可以用vector来达到最大节约空间的目的。使用时需要包含vector头文件。

定义一个一维动态数组的语法为:

vector<int> a;  //int为该动态数组的元素数据类型,也可以为string、double等

定义一个二维动态数组的语法为

vector<int*> a; //三维数据类型为int**,以此类推。

C++中vector的基本操作有:

1、push_back(x) 在数组的最后添加元素x。 
2、pop_back() 删除最后一个元素,无返回值。 
3、at(i) 返回位置i的元素。 
4、begin() 返回一个迭代器,指向第一个元素。 
5、end() 返回一个迭代器,指向最后一个元素的下一个位置。 
6、front() 返回数组头的引用。 
7、capacity(x) 为vector分配空间 
8、size() 返回数组大小 
9、resize(x) 改变数组大小,如果x比之前分配的空间大,则自动填充默认值。 
10、insert 插入元素
①a.insert(a.begin(),10); 将10插入到a的第一个元素前。 
②a.insert(a.begin(),3,10) 在a的第一个元素前插入三个10。 
11、erase 删除元素
①a.erase(a.begin()); 将起始位置的元素删除。 
②a.erase(a.begin(),begin()+2); 将0~2之间的元素删除。 
12、rbegin() 返回一个逆序迭代器,它指向最后一个元素。 
13、rend() 返回一个逆序迭代器,它指向的第一个元素前面的位置。 
14、clear()清空所有元素。

例子:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> a;
    vector<int> b;

    for (int i = 0; i < 10; ++i) {
        a.push_back(i);
    }

    a.swap(b);  // 将数组a元素与数组b元素交换
    cout << a.size() << " " << b.size() << endl;  // 输出:0 10

    for (vector<int>::iterator it = b.begin(); it != b.end(); ++it) {
        cout << *it << " ";  // 输出: 0 1 2 3 4 5 6 7 8 9
    }
    cout << endl;

    b.erase(b.begin() + 1);  // 删除位置1的元素,即元素1.
    cout << b.size() << endl;  //输出:9

    for (vector<int>::reverse_iterator rit = b.rbegin(); rit != b.rend(); ++rit) {
        cout << *rit << " ";  // 逆向输出数组元素:9 8 7 6 5 4 3 2 0
    }
    cout << endl;

    b.resize(11);  // 将数组空间设定为11
    b.push_back(20);

    for (vector<int>::iterator it = b.begin(); it != b.end(); ++it) {
        cout << *it << " ";  // 输出:0 2 3 4 5 6 7 8 9 0 0 20 
    }
    cout << endl;

    b.insert(b.begin(), 100);  // 在开始第一个元素前插入100

    for (vector<int>::iterator it = b.begin(); it != b.end(); ++it) {
        cout << *it << " ";  // 输出: 100 0 2 3 4 5 6 7 8 9 0 0 20 
    }
    cout << endl;

    b.insert(b.begin() + 5, 3, 100);  // 在第六个元素前插入三个100

    for (vector<int>::iterator it = b.begin(); it != b.end(); ++it) {
        cout << *it << " ";  // 输出: 100 0 2 3 4 5 6 7 8 9 0 0 20 
    }
    cout << endl;

    return 0;
}

集合(set)

C++中集合(set)类似于数学上的集合,即每个元素只能出现一次,使用该容器需要包含set头文件。

定义一个set的语法为:

set<int> s; //int为集合的数据类型,可以为string,double等

C++中set的基本操作有:

1、begin() 返回一个迭代器,指向第一个元素。 
2、end() 返回一个迭代器,指向最后一个元素的下一个位置。 
3、clear()清空set的所有元素。 
4、empty() 判断是否为空。 
5、size() 返回当前元素个数 
6、erase(it) 删除迭代器指针it指向的元素。 
7、insert(a) 插入元素a 
8、count() 查找某个元素出现的次数,只有可能为0或1。 
9、find() 查找某个元素出现的位置,如果找到则返回这个元素的迭代器,如果不存在,则返回s.end()
例子:

#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

int main(int argc, char const *argv[]) 
{
	set<int> s;

    s.insert(20);
    s.insert(10);
    s.insert(30);
    s.insert(10);

    cout << s.size() << endl;  // 输出3,因为集合中元素不能重复
    for (set<int>::iterator it = s.begin(); it != s.end(); ++it) {
        cout << *it << " ";  // 输出: 10 20 30, 集合会自动排序
    }
    cout << endl;

    //输出: 1 0
    cout << count(s.begin(), s.end(), 20) << " " << count(s.begin(), s.end(), 40) << endl;

    s.erase(s.find(10));
    for (set<int>::iterator it = s.begin(); it != s.end(); ++it) {
        cout << *it << " ";  //输出: 20 30
    }
    cout << endl;

    return 0;
}

队列(queue)

queue实现了一种先进先出的数据结构,使用时需要包含queue头文件。

定义一个queue的语法为:

queue<int> q;   //int为队列的数据类型,可以为string,double等

C++中queue的基本操作有:

1、入队,如:q.push(x) 将元素x置于队列的末端 
2、出队,如: q.pop() 同样不会返回弹出元素的值 
3、返回队首元素,如:q.front(); 
4、返回队尾元素,如:q.back(); 
5、判断是否为空,如:q.empty(); 
6、返回队列元素个数,如:q.size();

#include <iostream>
#include <queue>

using namespace std;

int main(int argc, char const *argv[])
{
    queue<int> q;

    for (int i = 0; i < 10; ++i) {
        q.push(i);  // 将0~9依次入队
    }

    cout << q.front() << " " << q.back() << endl;  // 输出: 0 9

    for (int i = 0; i < 10; ++i) {
        cout << q.front() << " ";  // 依次输出0、1、...、9
        q.pop();
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值