基础框架
cpp
#include<bits/stdc++.h>//万能头文件
using namespace std;
int main(){
return 0;
}
c的库函数去掉后缀.h,再在最前面加c可用在c++里,不过有万能头文件的话也不太需要去记了
输入输出
cin相当于scanf
cin>>n;
cout相当于printf
cout<<n;
endl相当于换行
cout<<n<<end1;
多个数据
cout<<"want to sleep"<<" "<<n<<" "<<s<<endl;
字符串
定义字符串 用string ,并且可以直接相加拼接
string a = "hello";
string b = " world!";
string c = a + b;//c="hello world"
getline 相当于fgts但无需指定最大读取长度,也不用处理换行符
getline(cin,a);
获取字符串长度:s.length()或s.size()
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Hello";
cout << s.length() << endl; // 输出 5
cout << s.size() << endl; // 同样输出 5
string empty = "";
cout << empty.length() << endl; // 输出 0
return 0;
}
截取子串:s.substr(pos, len)
cpp
string sub = s.substr(pos); // 从 pos 开始,截取到字符串末尾
string sub = s.substr(pos, len); // 从 pos 开始,截取最多 len 个字符
// 用 substr 复制整个字符串(等价于 strcpy 的效果)
string dest = s.substr(0); // 从位置 0 截取到末尾 → 完整复制
cout << dest << endl; // 输出: Hello, world!
STL
vector(动态数组)
头文件:#include <vector>
创建数组:
cpp
vector<int> v; // 空 vector,存储 int
vector<int> v(10); // 定义长度为10的数组,初始值为默认值0
vector<int> v(10, 2); // 定义长度为10的数组并全部初始化为2
vector<string> words; // 定义字符串数组,记得用resize分配大小
字符串数组赋值:
cpp
vector<string> words = {"apple", "banana", "cherry"};
或者
cpp
words[0] = "apple";
words[1] = "banana";
words[2] = "cherry";
查看数组大小:
cpp
cout << v.size();
分配数组大小:
cpp
v.resize(length);
cpp
vector<int> v;
v.reserve(20); // 预分配 20 个元素的内存(物理空间)
访问数组元素: v[i],与C语言一致
删除数组末尾数据:
cpp
v.pop_back();
末尾添加新的数据:
cpp
v.push_back(data); // 这也是动态的体现之处
迭代器:
cpp
for(auto p=v.begin();p!=v.end();p++){
cout<<*p<<" ";
}
//遍历整个数组,这样的话如果数组扩容,也不用更改边界条件
//v.begin在数组第一个元素位置,v.end在最后一个逻辑元素的下一个位置
set(集合)
map(键值对)
stack
头文件:include<stack>
创建栈:stack<int> s;
压栈:s.push(i);
出栈:s.pop()
访问栈顶:s.top();
获取长度:s.size();
queue
头文件:include<queue>
创建队列:queue<int>s;
入队:s.push(i);
出队:s.pop();
访问队首:s.front();
访问队尾:s.back();
获取长度:s.size();
sort排序函数
数组升序
cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[]={5,2,8,1,9};
int n=5;
//对于c风格数组从小到大排序(默认)
sort(arr,arr+5);
//但如果是vector容器,则是sort(arr.begin(),arr.end())
for(int i=0;i<n;i++){
printf("%d",arr[i]);
}
return 0;
}
数组降序
cpp
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
return a>b; //(a排在前面,并且a>b,即降序)
}
int main(){
int arr[]={5,2,8,1,9};
int n=5;
//从大到小排序(使用自定义cmp)
sort(arr,arr+5,cmp);
for(int i=0;i<n;i++){
printf("%d",arr[i]);
}
return 0;
}
结构体排序
cpp
#include <bits/stdc++.h>
using namespace std;
typedef struct{
char name[20];
int score;
}Student;
//按分数从高到低排序,分数相同按名字字典序排序 'A'<'B'
bool cmp(Student a,Student b){
if(a.score!=b.score){
return a.score>b.score;
}
return strcmp(a.name,b.name)<0;
//其实C++ 的强大特性:std::string(或 char[])可以直接用 <, >, == 等运算符进行字典序比较,而不需要手动调用 strcmp!
//所以可以写成a.name<b.name,前提是字符是string定义,而不是c中的char,c风格必用strcmp,c++可以直接比较,不能混了
}
int main(){
Student stu[3];
sort(stu, stu + n, cmp);
for(int i=0;i<3;i++){
scanf("%s %d",stu[i].name,stu[i].score);
}
return 0;
}
字符串排序
1.多个字符串
- 按字典序排序(默认升序,且大写字母永远比小写字母小)
cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<string>s={"apple", "banana", "cherry"};
sort(s.begin(),s.end());
return 0;
}
- 按字符串长度排序
cpp
bool cmp(const string& a, const string& b) {
return a.length() < b.length();
}
sort(words.begin(),words.end(),cmp);
- 按字典序升序排序,不区分大小写
cpp
bool cmp(string a, string b) {
for(int i=0;i<a.length();i++){
a[i]=tolower(a[i]);
}
for(int i=0;i<b.length();i++){
b[i]=tolower(b[i]);
}
return a<b;
}
sort(words.begin(),words.end(),cmp);
- 按字典序降序排序,不区分大小写
cpp
bool cmp(string a,string b) {
for(int i=0;i<a.length();i++){
a[i]=tolower(a[i]);
}
for(int i=0;i<b.length();i++){
b[i]=tolower(b[i]);
}
return a>b;
}
sort(words.begin(),words.end(),cmp);
2.单个字符串内按字典序排序
cpp
string str = "dcba";
// 对字符串内部的字符进行排序
sort(str.begin(), str.end());
根据具体问题修改排序方式