C++语法

基础

//ASCII
'A':65
'Z':90
'a':97
'z':122
'0':48

常见函数

abs()
reverse(a, a + n)	//#include<algorithm>
memset(a, 0, sizeof a)	//#include<cstring>
memcpy(b, a, sizeof a);	//#include<cstring>

字符串

//char
char s[100];
fgets(s, 100, stdin);
cin.getline(s, 100);

//string
string s;
getline(cin, s);

//输出
printf("%s", s.c_str());

//遍历
for(int i = 0; i < s.size(); i++)

for(char c : s)
    cout << c << endl;

for(auto c : s)
    cout << c << endl;

for(char &c : s)    //如果要改变

//删除最后一个字符
s.pop_back();

//截取
s.substr(起始位置, 长度);	//长度不写则表示到结尾

//插入
s.insert(起始位置, string);

//stringstream-<sstream>
string s = "123 abc 23 1.4";
stringstream ssin(s);
int a, b; string str; double c;
// a = 123, b = 23, str = "abc", c = 1.4;
ssin >> a >> str >> b >> c;
cout << a << ' ' << str << ' ' << b << ' ' << c;

//str.back()
str.back() //返回字符串最后一个字符

//字符串数组
string s[100];
while (cin >> str[n])
    n++;

//大小写转换
for(auto &c : a)
    c = tolower(c);
for(auto &c : a)
    c = toupper(c);

//find()函数
//npos是一个常数,用来表示不存在的位置
if(a.find(b) != string::npos)

函数

//二维数组的第二维下标不可以省略
void output(int m, int n, int a[][3])

技巧

//1
for(int i = 0, len = strlen(str); i < len; i++)

//2、第一类双指针
int j = i;
while(j < s.size() && s[j] == s[i])
    j++;
i = j - 1;	//for循环i++

指针

// . 和 -> 的区别,主要看p是哪种类型的变量
Node p = new Node();
p.next
p.val

Node* p = new Node();
p->next
p->val

STL容器

vector

#include<vector>

//初始化
vector<int> a = {1, 2, 3, 4, 5};
vector<int> a({1, 2, 3, 4, 5});
vector<vector<int>> num(n, vector<int>(n));    //行n,列n

//遍历
//方式1:通过a[i]读取元素值
for (int i = 0; i < a.size(); i++) 
    cout << a[i] << endl;

//方式2:迭代器,通过*i读取元素值
for (vector<int>::iterator i = a.begin(); i < a.end(); i++) 
    cout << *i << endl;

//方式3:迭代器简化版
for (auto i = a.begin(); i < a.end(); i++) 
    cout << *i << endl;

//方式4:auto
for (auto x : a)
    cout << x << << endl; 

//操作
a[k];           // 取值
a.size();       // 长度
a.empty();      // 判空
a.clear();      // 清空

a.push_back();  //尾部插入
a.pop_back();   //尾部删除
a.front();      //获取头部元素
a.back();       //获取尾部元素

//在指定范围内大于等于x的元素下标
int index = lower_bound(a.begin(), a.end(), 2) - a.begin();    //index值为1
//在指定范围内大于x的元素下标
int index = lower_bound(a.begin(), a.end(), 2) - a.begin();    //index值为2

//反向迭代器
for(auto i = a.rbegin(); i < a.rend(); i++)
        cout << *i << endl;

return vector<int>(res.rbegin(), res.rend());

queue

#include<queue>

/**********普通队列**********/
//定义
queue<int> q;

//操作
q.push(1);    //队尾入队
q.pop();      //对头出队
q.front();    //取队头
q.back();     //取队尾

/**********优先队列**********/
//定义
priority_queue<int> a;    //大根堆
priority_queue<int, vector<int>, greater<int>> b;    //小根堆

//操作
a.push(1);    //插入
a.top();      //取最大值
a.pop();      //删除最大值

//自定义类型
struct Rec {
    int a, b;

    //大根堆需要自定义类重载<号
    bool operator< (const Rec& t) const {
        return a < t.a;
    }

    //小根堆需要自定义类重载>号
    bool operator> (const Rec& t) const {
        return a > t.a;
    }
};

priority_queue<Rec> c;    //大根堆
priority_queue<Rec, vector<Rec>, greater<Rec>> d;    //小根堆
d.push({1, 2});

deque

#include<deque>

// 定义
deque<int> q;

// 操作
q[i]                // 随机访问
q.begin();          // 队头元素地址,用*q.begin()读取元素
q.end();            // 队尾元素地址,用*q.end()读取元素
q.front();          // 队头元素值
q.back();           // 队尾元素值

push_back();        // 队尾插入元素
push_front();       // 队头插入元素
pop_back();         // 队尾删除元素
pop_front();        // 队头删除元素

stack

#include <stack>

// 定义
stack<int> stk;

// 操作
s.push(x);       // 入栈
s.top();         // 查看栈顶
s.pop();         // 出栈

set

#include<set>

// 定义
set<int> s;             // 集合
multiset<int> ms;       // 允许元素重复

//自定义类要求重载<

// 操作
s.size();
s.empty();
s.claer();

s.insert(x);
s.find(x);              // 返回迭代器,可用if(s.find(x) == s.end())判断是否存在元素x(不存在)
s.lower_bound(x);       // 返回大于等于x的最小元素
s.upper_bound(x);       // 返回大于x的最小元素

s.erase(x);             // 删除x
s.count(x);             // 统计x出现的次数(普通集合只会返回0或1,multiset可能返回大于1的数)

map

#include<map>

//定义
map<string, int> a;
a["ww"] = 2;

map<string, vector<int>> a;
a["ww"] = vector<int>({1, 2, 3, 4});
cout << a["ww"][2] << endl;

unordered_map

#include <unordered_map>

//定义
unordered_map<int, int> hash;

//赋值
hash[0] = 1;
hash[1] = 2;

//查询
hash[0];

//遍历key是否存在,存在返回1,否则返回0
hash.count(1) != 0

//遍历
for (unordered_map<int, int>::iterator it = hash.begin(); it != hash.end(); it ++ )
	cout << it->first << ' ' << it->second << endl;

bitset

#include <bitset>

//定义二进制串
bitset<100> s;

//操作
s[0] = 1;
s.count();      // 1的个数
s.set(p);       // 第p位设为1
s.reset(p);     // 第p位设为0

pair

pair<int, string> a;
a = {88, "ww"};
cout << a.first << " " << a.second << endl;

位运算

  • 右移:>>\(\frac{a}{{{2^k}}}\)
  • 左移:<<\(a*{2^k}\)
  • x的第k位数字:x >> k & 1
  • lowbit(x) = x & -x ,返回x的最后一位1

常用库函数

#include<algorithm>

//reverse()--反转
vector<int> a({1, 2, 3, 4, 5});    //vector
reverse(a.begin(), a.end());

int a[] = {1, 2, 3, 4, 5};    //数组
reverse(a, a + 5);

//unique()--必须保证相同元素挨到一起,unique()并没有真的删除重复元素,它仅将非重复的元素移到了前面
int a[] = {1, 2, 3, 4, 5};
unique(a, a + a.size());    //去重后最后一个元素的下一个元素的地址
int m = unique(a, a + 5) - a;    //数组中不同元素的个数

int m = unique(a.begin(), a.end()) - a.begin();    //vector中不同元素的个数

a.erase(unique(a.begin(), a.end()), a.end());    //去除重复元素

//random_shuffle--随机打乱
#include<ctime>
srand(time(0));
random_shuffle(a.begin(), a.end());

//sort()--排序
sort(a.begin(), a.end());    //从小到大
sort(a.begin(), a.end(), greater<int>());    //从大到小

//自定义排序规则
bool cmp(int a, int b)  //a是否应该排在b的前面
{
    return a > b;    //如果a小于b,那么a应该排到b的前面
}
sort(a.begin(), a.end(), cmp);

//结构体自定义排序规则
struct rec 
{
    int x, y;   
};
bool cmp(rec a, rec b)  //a是否应该排在b的前面
{
    return a.x < b.y;    //如果a小于b,那么a应该排到b的前面
}
struct rec r[5];
sort(r, r + 5, cmp);

//重载 < 号
struct rec 
{
    int x, y;   
    bool operator< (const rec &t) const    //此处的 < 不可修改
    {
        return x > t.x;    //排序方式只能修改此处的 < 或 >
    }
};
struct rec r[5];
sort(r, r + 5);

//lower_bound--返回大于等于x的最小元素的迭代器
int t = lower_bound(a, a + 5, 3) - a;

//upper_bound--返回大于等于x的最小元素的迭代器
posted @ 2022-03-20 11:20  当惜  阅读(133)  评论(0)    收藏  举报