C++中常用函数以及类型总结

18 篇文章 0 订阅
16 篇文章 2 订阅

记录在刷题过程中经常用到的,总结一下加深印象,方便以后查询。

vector(动态数组)

需要头文件<vector>
构造函数

vector<int> nums; //构造数据元素类型为int的空动态数组
vector<int> nums2(nums1); //构造元素与nums相同的动态数组(拷贝)
vector<int> nums(size); //数据元素类型为int,个数为size的动态数组
vector<int> nums(size,val); //数据元素类型为int,个数为size,初始值为val的动态数组
vector<int> nums[arraySize]; //定义长度为arraySize的vector动态数组数组。

访问 vector内元素有两种访问方式,通过下标或迭代器访问。
通过下标访问时和普通数组差不多,范围为(0到nums.size()-1)
通过迭代器访问如下:

vector<typename>::iterator it; //typename是类型名,通过*it 来访问

注意:
(nums[i]和*(it.begin()+i)等价)(it.end()是尾元素的下一个地址)

排序 需要头文件<algorithm>(具体细节见algorithm)

sort(nums.begin(),nums.end());

nums.push_back(val); //在数组末尾插入一个值为val的 元素

string

头文件为<string>
子串

string s;
s.substr(begin,len) //返回字符串s从begin位置开始长度为len的子串。

list(双向链表)

头文件为<list>,list无法通过下标访问。

双向链表的每一个元素中都有一个指针指向后一个元素,也有一个指针指向前一个元素。
在 list 容器中,在已经定位到要增删元素的位置的情况下,增删元素能在常数时间内完成。(比vector要快)。
注意:list无法用algorithm中的sort进行排序,可以用自己的成员函数进行排序。

构造函数:

list<int> lst; //创建空list
list<int> lst1(5,5); //创建含有5个值为5的元素的list
list<int> lst2(lst); //用lst初始化lst2

常用函数:

  • list.begin()、list.end()
  • list.front()、list.back()
  • list.push_back(val)、list.push_front(val)
  • list.pop_back()、list.pop_front()
  • list.erase()

algorithm头文件下的常用函数

sort

sort(首元素地址(必须填),尾元素地址的下一个地址(必填),比较函数(非必填))
如果不写比较函数,默认对区间递增排序,实例如下(参考《算法笔记》)

注意: 在写cmp函数时,针对两个比较的对象,如果相等一定要返回false!!!

数组的排序

#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
	int a[6] = {9,4,2,5,6,-1};
	//将a[0]~a[3]从小到大排序
	sort(a,a+4);
	for(int i = 0;i <6;i++)
		printf("%d ",a[i]); 
	return 0;
}

对于排序的指定规则,也可以自己定义,这时候就要用到sort的第三个可选参数cmp.使用sort对数组升序排列时,cmp函数的构造如下。

结构体数组的排序

#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int a,int b){
	return a > b;	//可以理解为当a>b时,把a放在前面
}

int main(){
	int a[6] = {9,4,2,5,6,-1};
	//将a[0]~a[3]从大到小排序
	sort(a,a+4,cmp);
	for(int i = 0;i <6;i++)
		printf("%d ",a[i]); 
	return 0;
}

使用sort也可以对结构体数组进行排序,使用方法见下例。

#include<cstdio>
#include<algorithm>
using namespace std;

struct node{
	int x,y;
}ssd[10];

bool cmp(node a,node b){
	return a.x >= b.x ; //按x的值降序
}

int main(){
	ssd[0].x = 2;ssd[0].y = 2; //{2,2}
	ssd[1].x = 1;ssd[1].y = 3; //{1,3}
	ssd[2].x = 3;ssd[2].y = 1; //{3,1}
	sort(ssd,ssd+3,cmp);
	for(int i = 0;i <3;i++)
		printf("%d %d\n",ssd[i].x,ssd[i].y); 
	return 0;
}

容器的排序

在STL标准容器中,只有vector、string、deque是可以使用sort的,这是因为像map、set这种容器是用红黑树实现的,元素本身有序,故不允许用sort排序.对vector排序的例子如下:

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


bool cmp(int a,int b){
	return a >= b ; //降序
}

int main(){
	vector<int> nums;
	nums.push_back(3);
	nums.push_back(1);
	nums.push_back(2);
	sort(nums.begin(),nums.end(),cmp); //对整个vector排序 
	for(int i = 0;i <3;i++)
		printf("%d ",nums[i]); 
	return 0;
}

如string等,其余类似。

next_permutation()

next_permutation()给出一个序列在全排列的下一个序列。
prev_permutation()给出一个序列在全排列的上一个序列。

例题:洛谷p1088 火星人

#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
	int nums[10000];
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%d",&nums[i]);
	while(m--)
		next_permutation(nums,nums+n);
	for(int i=0;i<n;i++)
		printf("%d ",nums[i]);
	return 0;
}

max(),min(),abs(),reverse(),swap()

这些函数比较常用也比较容易,略。(使用reverse()也注意左闭右开)

fill()

int a[5] = {1,2,3,4,5};
fill(a,a+5,233); //将a[0]~a[4]均赋值为233。

set

set(集合)是一个内部自动有序且不含重复元素的容器。只能通过迭代器访问。

set<typename> name;
set<typename>::iterator it; //只能it++不能it+1的方式访问

常用函数:insert(),find(),erase(),size(),clear().
注意:

  • find(value)返回set中对应值为value的迭代器。set中没有该值时为.end();

实际上,由于set和map底层是基于平衡树(红黑树)实现,因此应该有很多平衡树的接口,比如lower_bound,upper_bound在log(N)的时间查询出第一个大于或等于大于的元素,这个有时也会用到,感兴趣可以自行查询https://zh.cppreference.com/w/cpp/container/set/lower_bound

map

需要头文件<map>
map的原理为红黑数,查询复杂度为log(N)
map可以通过下标访问,或者通过迭代器访问。

map<typename1,typename2>::iterator it;

通过it->first来访问键,使用it->second来访问值。

构造函数

map<int,int> hashmap; //构造一个map对象hashmap,键:int型,值:int型

插入

map<string,int> hashmap;
hashmap[“asd”] = 1; //直接插入,如果已经有键为"asd"的结点,则直接覆盖。
hashmap.insert(pair<string, int>(“aaa”,1)); //使用insert函数插入,如果结点已经存在,则无法插入(不会改变原来的值)

删除

map<string,int> hashmap;
map<string,int>::iterator iter;
hashmap.erase(iter); //通过迭代器的方式删除,删除成功返回1,失败返回0
hashmap.erase(“aaa”); //通过关键字的方式删除,删除成功返回1,失败返回0

查找

map<string,int> hashmap;
map<string,int>::iterator iter;
iter = hashmap.find(“asd”); //迭代器返回当前元素的位置,如果map中无该元素,则返回hashmap.end()
int n = hashmap.count(“asd”); //因为map中不存在相同的元素,因此只能返回0或1.

queue

queue为队列(先进先出),需要头文件<queue>

queue<typename> name;

访问
只能通过front()来访问队首元素,通过back()来访问队尾元素。
push()入队,pop()出队。
通过empty(),queue为空返回true;

priority_queue

priority_queue(优先队列),头文件也是<queue>。
底层用堆来实现,队首元素是队列中优先级最高的一个。

priority_queue<typename> name;

访问
和queue队列不同,priority_queue优先队列没有front()和back()函数,只能通过top()函数来访问队首元素。

优先级设置

  • 当优先队列的类型为可比较的类型时候,下列两种写法等价,以int类型为例。

priority_queue<int> q;
priority_queue<int,vector<int>,less<int> > q;

如果想把最小的元素放在队列首

priority_queue<int, vector<int>, greater<int> > q;

  • 结构体的优先级设置。按price高为优先级高
  1. 写法一(重载结构体的"<"号)
struct fuit{
	string name;
	int price;
	friend bool operator < (fruit f1, fruit f2){
		return f1.price < f2.price;
	}
};

2.写法二 (写在结构体外部)

struct fuit{
	string name;
	int price;
};

struct cmp{
	bool operator () (fruit f1, fruit f2){
		return f1.price < f2.price;
	}
};

priority_queue<fruit, vector<fruit>, cmp> q;

deque

双端队列,两头都能插入删除查看数据。需要头文件<deque>

deque dq;

常用函数:

  • dq.push_front();
  • dq.push_back();
  • dq.pop_front();
  • dq.pop_back();
  • dq.front();
  • dq.back();

stack

stack栈,后进先出,需要头文件<stack>

stack<typename> name;

访问
只能通过top()访问栈顶元素。

常用函数
push(),pop(),top(),empty(),size().

  • 7
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值