摘要:在本文中,我们可以快速掌握关于C++基本语法以及STL库的常见函数,我们还会谈论ACM的一些比赛细节。
作者:来自ArimaMisaki创作
全文目录:
我们以最简单的一段代码为例开始讲解。
#include
using namespace std;
int main(){
cout << "hello world" < #include
using namespace std;
int main(){
int a = 1;
string b = "hello";
cout << a << endl;
cout << b << endl;
cin.get();
return 0;
}
C++中输出使用cin,相当于C中的scanf,所有标准数据类型都可以用cin输入。
>> 和<<代表输入流的方向。
cin.getline(读取字符串,读取字符数)用于读取一行,但要加入读取的字符数上限。
int* number = new int;
int* arr = new int[100];
int* carr = (int*)malloc(100*sizeof(int));
void swapInt(int&a, int&b){
int c = a;
a = b;
b = c;
}
int main(){
int a = 1,b =2;
swapInt(a,b);
}
int add(int a, int b){
return a+b;
}
int add(int a){
return a;
}
int minus(int a,int b = 0){
return a-b;
}
int main(){
add(0);
add(0,0);
minus(0);
minus(0,0);
}
struct node{
int number;
node* next;
};
//struct node* head;
node* head;
struct node{
int number;
node* next;
node(int _number = 0,node* _next = nullptr){
number = _number;
next = _next;
}
};
int main(){
node a = node(0);
node* b = new node(1,&a);
}
| 函数 | 说明 |
|---|---|
| strlen() | 字符串长度 |
| strcmp() | 字符串比较 |
| strcpy() | 字符串拷贝 |
| memset() | 暴力清空 |
| memcpy() | 暴力拷贝 |
三角函数、指数函数、浮点取整函数
| 函数 | 说明 |
|---|---|
| qsort() | C语言快排,调用复杂一般不用而使用C++的排序 |
| rand() | 随机数 |
| malloc() | 申请内存 |
| free() | 释放内存 |
| 函数 | 说明 |
|---|---|
| time(0) | 从1970年到现在的描述,常配合随机数用于初始化 |
| clock() | 程序启动到目前为止的毫秒数 |
| 函数 | 说明 |
|---|---|
| isdigit() | 判断字符是否为数字 |
| isalpha() | 判断大小写字母 |
vector简介
vector arr1(100);
vector arr2;
vector arr3 = {1,2,3,4,5};
vector arr4{1,2,3,4,5};
int arr2[100];
vector list;
list.push_back(1);
vector的遍历
迭代器
vector arr1(100);
int arr2[100];
vector::iterator p1;
int* p2;
for(p1 = arr1.begin();p1 != arr1.end();p1++){
cout << *p1< 与普通的数组类似,vector也可以使用指针来访问遍历每一个元素。STL的指针被称为迭代器。
C++的迭代器需要指明类型,如需要访问整型向量的迭代器写作vector
begin()是首元素迭代器,begin默认在容器的首元素的地址。end同理,默认在容器的尾元素的下一个地址。
vector基本操作
声明:vector
| 函数 | 说明 |
|---|---|
| list.size() | 数组元素个数,复杂度O(1) |
| list.clear() | 一键清空数组,而非删除数组,复杂度O(n) |
| list.empty() | 判空,复杂度O(1) |
| list.begin() | 首元素迭代器,复杂度O(1) |
| list.end() | 尾元素的下一位元素的迭代器,复杂度O(1) |
| list.erase() | 删除数组某个迭代器所在位置的数字,复杂度O(n) |
| list.push_back() | 往数组后添加元素,复杂度O(1) |
| list.pop_back() | 删除数组最后一个元素,复杂度O(1) |
string简介
string str1 = "hello";
char str2[] = "world";
string str3;
str3.push_back('!');
cout << str1 << " " << str2 << str3 << endl;
string基本操作
声明:string str = “hello”
| 函数 | 说明 |
|---|---|
| str.length() | 等同于str.size(),复杂度O(n) |
| str.insert(1,“a”) | 在下标为1处插入字符或字符串,复杂度O(1) |
| str.insert(str.begin,‘a’) | 在迭代器处插入一个字符,复杂度O(n) |
| str.c_str() | 返回C语言风格的字符串,复杂度O(n) |
| str.append(str2) | 把str2拼接到str后面,复杂度O(n) |
| str.compare(str2) | 比较字符串 |
| str += str2 | 相当于拼接 |
| str += ‘a’ | 相当于push_back |
stack sta;
sta.push(1);
int topElement = sta.top();
sta.pop();
sta.empty();
sta.size();
queue que;
que.push(1);
int frontElement = que.front();
que.pop();
que.empty();
que.size();
priority_queue que2;
que2.push(1);
int minElement = que2.top();
que2.pop();
que2.empty();
que.size();
set st;
st.insert(1);
st.find(1);
st.erase(1);
multiset mst;
mst.insert(1);
mst.insert(1);
mst.count(1); //2
pair origin;
origin = make_mair(0,0);
origin.first == origin.second;
origin.swap;
pairid;
id = make_pair("somebody",110);
pair id;
id = make_pair("somebody",110);
map studentHeight;
studentHeight["小明"] = 170;
studentHeight["小红"] = 150;
studentHeight.insert(id);
studentHeight.erase("小明");
有些毒瘤题目会卡map的时间,用unordered_map
algorithm和之前两个头文件不同,它没有定义什么新的类型,而是定义了很多算法,极大简化了代码量。
sort的基本使用
语法:sort(数组首元素地址,数位尾元素的下一位地址)
int arr[] {2,3,1,5,4};
int n = 5;
sort(arr,arr+n);
for(int i = 0;i arr;
arr.push_back(2);
arr.push_back(3);
arr.push_back(1);
sort(arr.begin(),arr.end());
for(int var : arr){
cout << var << endl;
}
注:C++sort排序的复杂度是O(nlogn)
降序排列
bool cmpInt(int a, int b){
return a>b;
}
vector arr;
arr.push_back(2);
arr.push_back(3);
arr.push_back(1);
sort(arr.begin(),arr.end(),cmpInt);
结构体的排序
struct Point{
int x,y;
};
Point points[1111];
bool cmp(Point a,Point b){
if(a.x != b.x){
return a.x| 函数 | 说明 |
|---|---|
| max(1,2) | 返回最大值,复杂度为O(1) |
| min(1,2) | 返回最小值,复杂度为O(1) |
| min_element(arr.begin(),arr.end()) | 数组最大指针,复杂度为O(n) |
| max_element(arr.begin(),arr.end()) | 数组最小指针,复杂度为O(n) |
| nth_element(arr.begin(),arr.begin()+n,arr.end()) | 查看排好序后的第n个位置里面的数据,复杂度为O(n) |
| swap(arr[0],arr[1]) | 用于交换两个数据,复杂度为O(1) |
| reverse(arr.begin(),arr.end()) | 用于反转数组,复杂度为O(n) |
| unique(arr.begin(),arr.end()) | 大多数用于离散化的情况,能使得数组的前面部分变得独一无二,重复的数据放于数组的后面部分。复杂度为O(n) |
| binary_search(arr.begin(),arr.end(),1) | 在排好序的数组中进行二分查找,搜索对应元素是否存在,返回布尔值。时间复杂度为O(logn) |
| lower_bound(arr.begin(),arr.end(),2) | 在排好序的数组中返回第一个大于等于你所给定的值的位置。时间复杂度为O(logn) |
| upper_bound(arr.begin(),arr.end(),2) | 在排好序的数组中返回第一个大于你所给定的值的位置。时间复杂度为O(logn) |
如果不想记忆C++的所有库函数,我们可以使用以下语句:
#include
它可以帮我们包含所有的头文件,Visual Studio用不了。
使用Reference - C++ Reference (cplusplus.com)查看STL的正确用法。
使用编译器自动补全自己摸索细节。
AC-Accepted 答案正确/通过
WA-Wrong Answer 答案错误
RE-Runtime Error 运行时错误
CE-Complie Error 编译错误
TLE-Time Limit Exceed 超出时间限制/超时
MLE-Memory Limit Exceed 超出内存限制
PE-Presentation Error 格式错误
OLE-Output Limit Exceed 输出超出限制/输出超限)