文章目录
1.vector基本操作
https://blog.csdn.net/fanyun_01/article/details/56842637
https://www.cnblogs.com/littleswan/p/12143646.html
v.push_back()在最后插入一个元素
v.pop_back()删除最后一个元素
v.empty()若空为true
v.size()元素个数
v.clear()清空元素
- 定长容器
vector<int>nums(20);//直接当数组用
- 不定长容器
vector<int>nums;//需要push_back()
3.定长容器版插入数据
nums[0]=2;nums[1]=0;
4.不定长容器版插入数据
nums.push_back(2);nums.push_back(0);
5.devc++不能对vector直接赋值(ans={1,2,3}不行),只能用push_back插入或者定长的时候赋值
2.栈的常用操作
stack< char >c;
push()插入
empty()若空为ture
top()取顶
pop()出顶
size()大小
bool isValid(string s) {
stack<char>c;
for (int i = 0; i<s.size(); i++)
{
if (s[i] == '(')
c.push(')');
else if (s[i] == '{')
c.push('}');
else if (s[i] == '[')
c.push(']');
else if (c.empty()||c.top() != s[i])
return false;
else
c.pop();
}
return c.empty();
}
3.队列的常用操作
queue< char >q;
push()插入
empty()若空为ture
front()取第一个元素
back()取最后一个元素
pop()出顶
size()大小
4.set容器常用操作
multiset和set差不多都是容器(它们都是一插进去就自动排序),set里自动除重,multiset里可以有重复的元素
multiset用法传送门:https://blog.csdn.net/sodacoco/article/details/84798621
set特点:自动排序并除重
注意:begin() 和 end()函数是不检查set是否为空的,使用前最好使用empty()检验一下set是否为空
s.begin() 返回set容器的第一个元素
s.end() 返回set容器的最后一个元素
s.clear() 删除set容器中的所有的元素
s.empty() 判断set容器是否为空
s.insert() 插入一个元素
s.erase() 删除一个元素
s.size() 返回当前set容器中的元素个数
s.count() 返回当前元素是否出现过,只有0和1
遍历(一定要用迭代器,set不能用数组的方式来遍历):
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main() {
set<int>s;
s.insert(3);
s.insert(1);
s.insert(2);
s.insert(1);
set<int>::iterator it;
for(it=s.begin();it!=s.end();it++)
{
cout<<*it<<endl;
}
}
ps:string的begin的地址,比如string s=“abcd”;
那么string::iterator it=s.begin();是字符串的首地址,结束是s.end()
5.vector容器排序,并除重
6.读取输入字符串含有空格
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main() {
string s;
getline(cin,s);//可以读取空格,以换行为cin结束标记
int pos=s.find_last_of(" ");
cout<<pos<<endl;
}
ps:s,find()函数//如果多个符合,输出的是第一个,即s.find_first_of()
s.find_last_of()函数输出的是最后一个
7.int最大值INT_MAX,最小值INT_MIN
写成 INT_MAX
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main() {
int a=INT_MAX;
if(a>10000000)
cout<<"t";
else
cout<<"f";
return 0;
}
写成INT_MIN
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main() {
int a=INT_MIN;
if(a<-10000000)
cout<<"t";
else
cout<<"f";
return 0;
}
例题:整数反转
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main() {
long x;
while(cin>>x)
{
if(x>INT_MAX||x<INT_MIN)
return 0;
else
{
long n=0;
while(x)
{
n=n*10+x%10;
x/=10;
}
if(n>INT_MAX||n<INT_MIN)
return 0;
else
cout<<n<<endl;
}
}
}
8.翻转函数reverse(使字符串自身反转)
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main() {
string s;
while(cin>>s)
{
reverse(s.begin(),s.end());
cout<<s<<endl;
}
}
9.二分法
二分法 (这题二分法非常典型)
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int sq(int x)
{
int left=0,right=x;
if(x==0)
return 0;
if(x<=3)
return 1;
while(left<right)
{
int mid=left+(right-left)/2;
if(mid>x/mid)//目标值在左
right=mid;
else if(mid<x/mid)//目标值在右
left=mid;
else
return mid;
//left2,right4,如果3.2,目标在右,left不能+1,并且此时return left即mid
//left2, right4, 如果2.2, 目标在左,return left
if(right-left<=1)
return left;
}
return 0;
}
int main()
{
int x;
while(cin>>x)
{
cout<<sq(x)<<endl;
}
return 0;
}
10.链表结构体构造
例题:将链表元素去重
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
struct LN{
int val;
LN* next;
LN(int x):val(x),next(NULL){}//这里的分号写不写都不报错,但leetcode原句没有分号
LN(int x,LN* L1):val(x),next(L1){}
};
int main(){
LN* L1=new LN(1,new LN(1,new LN(2)));
//取头
LN* cur=L1;
//去重
while(cur&&cur->next)
{
if(cur->val==cur->next->val)
cur->next=cur->next->next;
else
cur=cur->next;
}
//输出去重后
while(L1)
{
cout<<L1->val;
L1=L1->next;
}
return 0;
}
9.二叉树
跳转链接:主要看里面的第九题和第十题,有深搜栈,广搜队列
判断对称二叉树例题,判断二叉树深度例题:
https://blog.csdn.net/weixin_44575911/article/details/107873836
10.数组元素个数用sizeof(array)除以sizeof(类型)
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
int main()
{
int array[]={7,1,5,3,6,4};
cout<<sizeof(array)/sizeof(int);//整个数组大小除以单个元素大小才是数组个数,用size()不行,会报错
return 0;
}
11.动态规划:
链接里的第九题和第十题
https://blog.csdn.net/weixin_44575911/article/details/107903770
12.不要随便用erase()它很慢,尽量用数组(空间)换时间,建个新数组
例,链接里的第一题
https://blog.csdn.net/weixin_44575911/article/details/107997704
13.数组批量赋初值
整数数组只能赋初值为0,字符char数组可以赋初值为任何字符,比如’a’、‘b’、‘2’…
整数:
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main()
{
int array[5];
memset(array,0,5);//这个5是数组长度,可以用sizeof(array)/sizeof(int)来代替
cout<<array[4];
return 0;
}
字符:
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main()
{
char str[5];
memset(str,'a',sizeof(str)/sizeof(char));
cout<<str[4];
return 0;
}
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
int main()
{
char c[20]="abc";
cout<<sizeof(c)<<endl;//结果为20
cout<<strlen(c);//结果为3
return 0;
}
14.list使用
传送门1:https://blog.csdn.net/galesaur_wcy/article/details/81586274
传送门2:https://blog.csdn.net/lskyne/article/details/10418823
assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main() {
list<int>l1;//链表
list<int>l2;
list<int>::iterator iter;//指针
l1.push_back(2);//插入
l1.push_back(1);
l2.push_back(3);
l2.push_back(2);
l2.push_back(4);
l2.push_back(1);
l2.push_back(1);
l2.push_back(2);
iter=++l2.begin();//iter只支持++前缀,不支持+1
l2.erase(iter);//删除l2的第二位
l2.remove(1);//删除值为1的元素
list<int>l2temp;//临时中转的列表
l2temp=l2;
l1.merge(l2temp);//合并并清空l2temp
for(iter=l1.begin();iter!=l1.end();iter++)
{
cout<<*iter<<" ";
}
cout<<endl<<endl;
for(iter=l2.begin();iter!=l2.end();iter++)
{
cout<<*iter<<" ";
}
cout<<endl<<endl;
if(l2temp.empty())
{
cout<<"l2temp变为空";
}
return 0;
}
输出结果:
2 1 3 4 2
3 4 2
l2temp变为空