C++标准模板库介绍

vector的常见用法详解

include

using namespace std;
vector a;
vector<vector > b;

.[index]
.begin()/.end()
::iterator
push_back/pop_back
size/clear/insert/erase删除一个或一个区间

set的常见用法

include

using namespace std;
set a;
::iterator
.insert/.begin/.end/.find/.erase删除一个用位置,用键值,删除区间/.size/.clear

set内元素自动排序且去除重复元素

string常见用法

include

using namespace std;

string str = "abc";
.length/.[index]/

要读入和输出整个字符串,只能用cin和cout
#include
#include
using namespace std;
string str;
cin >> str;
cout << str;

.c_str()/::iterator/operator+=/==/!=/</<=/>/>=/length()/size()/insert/erase/clear/substr/string::npos[作为find函数失败时返回值]/find/replace

科学计数法
0.a1a2... x 10^e

map常用用法详解

include

using namespace std;

map的定义

map<typename1, typename2> mp;

map容器内元素的访问

通过下标访问,通过迭代器访问
- 通过下标访问
- 通过迭代器访问
map<typename1, typename2>::iterator it
it->first来访问键
it->second来访问值

map常用函数实例解析

find/erase/size/clear

map的常见用途

一个键多个值下用multima/

queue

queue的定义

include

using namespace std;
queue name;

queue内元素访问

front/back/push/pop/empty/size

priority_queue

在有限队列中,队首元素是队列中优先级最高的那一个.

priority_queue的定义

include

using namespace std;
priority_queue name;

priority_queue容器内元素的访问

top/push/pop/empty/size

priority_queue内元素优先级的设置

以下介绍基本数据类型与结构体类型的优先级设置方法
- 基本数据类型的优先级设置
数字大的优先级大
构造时,可选参数2表示承载底层数据结构堆的容器
可选参数3,是对参数1的比较类
如希望数字小的优先级大
priority_queue<int, vector, greaterr > q;
- 结构体的优先级设置
例子
struct fruit
{
string name;
int price;
};

// 现希望水果价格低的为优先级高
struct fruit
{
string name;
int price;

friend bool operator<(fruit f1, fruit f2)
{
return f1.price > f2.price;
}
};

priority_queue q;

// 另一种等价表示方法
struct cmp
{
bool operator()(fruit f1, fruit f2)
{
return f1.price > f2.price;
}
};

priority_queue<fruit, vector, cmp> q;

待比较对象较大时,可用const &修饰比较形参

stack常见用法详解

stack的定义

include

using namespace std;
stack name;

stack容器内元素的访问

top/push/pop/empty/size

pair的常见用法详解

pair的定义

include

using namespace std;
pair<typename1, typename2> name;

- 如果想在代码中临时构建一个pair,有如下两种方法
方法1
pair<string, int>("haha", 5)
方法2
make_pair("haha", 5)

pair中元素的访问

类似于
struct pair
{
typename1 first;
typename2 second;
}

pair常用函数解析

两个pair类型数据可直接用==/!=/</<=/>/>=比较
比较规则:
先以first大小作为标准
相等时,继续比较second

pair常见用途

map的insert需要pair

algorithm头文件下的常用函数

using namespace std;

max/min/abs/fabs[位于math头文件]

swap(x,y)交换x和y的值

reverse(its, ite)

将[its, ite)之间元素反转

next_permutation()

给出一个序列在全排列中的下一个序列
next_permutation在已经达到全排列的最后一个时会返回false
#include <stdio.h>
#include
using namespace std;

int main()
{
int a[10] = {1,2,3};
do
{
printf("%d%d%d\n", a[0], a[1], a[2]);
}
while(next_permutation(a, a+3));
return 0;
}

fill

可把数组或容器中的某一段区间赋为某个相同的值

sort

如何使用sort

include

using namespace std;
sort(首元素地址,尾后元素地址,可选的比较函数);
默认对区间进行递增排序

如何实现比较函数cmp

介绍对基本数据类型,结构体类型,STL容器进行自定义排序规则时cmp的写法

include <stdio.h>

include

using namespace std;
// 按递减排序
bool cmp(int a, int b)
{
return a > b;
}

int main()
{
int a[] = {3, 1, 4, 2};
sort(a, a+4, cmp);
}

结构体数组的排序

include <stdio.h>

include

using namespace std;

struct node
{
int x, y;
}ssd[10];
bool cmp(node a, node b)
{
return a.x > b.x;
}

int main()
{
ssd[0].x = 2;
ssd[0].y = 2;
ssd[1].x = 1;
ssd[1].y = 3;
ssd[2].x = 3;
ssd[2].y = 1;
sort(ssd, ssd+3, cmp);

}

容器的排序

在STL标准容器中,只有vector/string/deque可使用sort
#include
#incude
#include
using namespace std;
bool cmp(string str1, string str2)
{
return str1.length() < str2.length();
}

int main()
{
string str[] = {"bbbb", "cc", "aaa"};
sort(str, str+3, cmp);
}

lower_bound/upper_bound

lower_bound和upper_bound需用在一个有序数组或容器中
lower_bound(first, last, val)来寻找在数组或容器的[first, last)范围内第一个值大于等于val的元素的位置,如是数组,返回该位置的指针.如是容器,返回该位置的迭代器.
upper_bound(first, last, val)来寻找在数组或容器的[first, last)范围内第一个值大于val的元素的位置.如是数组,返回该位置的指针,如是容器,返回该位置的迭代器.

posted @ 2020-09-03 00:29  raindayinrain  阅读(162)  评论(0)    收藏  举报