鉴于每次刷oj都会忘记如何输入输出以及各种STL的用法,记录此文
文章目录
头文件以及模板
普通难度用这个
#include<iostream>
using namespace std;
int main(){
// Code:
return 0;
}
忘记库函数咋引用这个万能头
#include<bits/stdc++.h>
using namespace std;
int main(){
// Code:
return 0;
}
结构体定义
首先普通的一个结构体定如下
struct myStruct
{
int x;
int y;
};
struct myStruct ms = {1, 2}; // 定义之前每次都需要加上struct
cout << ms.x << " " << ms.y; // 1 2
可以看到每次定义都需要写struct
此时引入typedef
,其用法如下
typedef struct {
int x;
int y;
} myStruct;
myStruct ms = {1, 2}; // 只需写上别名
cout << ms.x << " " << ms.y; // 1 2
输入输出
普通变量
int a;
cin >> a;
cout << a << endl;
#include <cstring>
字符串(不含空格)
string s;
cin >> s; // abcde
cout << s << endl; // abcde
getline(cin, str)
字符串(含空格)
string s;
getline(cin, s); // abcde abcde abcde abcde
cout << s << endl; // abcde abcde abcde abcde
to_string(value)
整形转字符串
int a = 5, b = 10;
string str = to_string(a + b);
cout << str << endl; // 15
字符串遍历
下标
for (size_t i = 0; i < s2.size(); i++)
{
cout << s2[i] << " ";
}
cout << endl;
迭代器
string::iterator it = s2.begin();
while (it != s2.end())
{
cout << *it++ << " ";
//it++;
}
cout << endl;
范围for
for (auto e : s2)
{
cout << e << " ";
}
cout << endl;
字符串相关方法
str.find(子串)
查找子串
string str = "abcdefg", str1 = "de";
// 返回值就是位置 没找到返回-1
cout << str.find(str1); // 3
str.find("xxx", position)
从position之后查找子串
string str = "abcdefg abcdefg", str1 = "de";
// 从第八位之后找第一个str1
cout << str.find(str1, 8); // 11
str.replace(从哪开始换, 换多长, 要换成哪个字符串)
替换子串
string str = "Hello My World";
string str1 = "Your";
cout << str.replace(6, 2, str1); // Hello Your World
memset(arr, 填充内容, sizeof(arr))
初始化数组arr的内容为任意东西
char picture[5][5] = {{0}};
memset(picture, '.', sizeof(picture)); // 初始化数组为点点
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << picture[i][j];
}
cout << endl;
}
/* 输出内容为:
.....
.....
.....
.....
..... */
+
字符串连接
string str = "Hello";
string str1 = "World";
cout << str + str1; // HelloWorld
库函数
#include <map>
clear() 删除所有元素
find() 查找一个元素
insert() 插入元素, 而实际中我们直接map['x'] = 1 就存入了键值对 <x, 1>
erase() 删除元素,括号内为key, 也就是左值
size() 返回map中元素的个数
count() 判断map中有某key: map.count(str) == 1
map<char, int>type;
type['a'] = 997;
type['b'] = 998;
type['c'] = 999;
cout << type['a'] << endl; // 997
cout << type.find('a') -> first << endl; // a
cout << type.find('a') -> second << endl; // 999
cout << type.size() << endl; // 3
type.erase('a'); //括号内为key值,也就是左值
cout << type.size() << endl; // 2
type.clear();
cout << type.size() << endl; // 0
// map的遍历:
for(map<string, int>::iterator it = myMap.begin(); it != myMap.end(); it++){
it -> first; // 迭代器元素的访问 相当于(*it).first
it -> second;
}
// vector写法相同,均用迭代器
#include<vector>
他是一个可以存自定义类型的 随机存取的队列,理解为对象数组
typedef struct {
string c; // command 指令
float p; // price 出价
long long n; // num 股数
}data;
// 定义vector
vector<data>notes;
data temp = {"buy", 1.00, 200};
// 入栈
notes.push_back(temp);
// vector的遍历
for(vector<data>::iterator it = notes.begin(); it != notes.end(); it++){
// it是指针
string command= (*it).c; // 或 it -> c
float price = (*it).p; // 或 it -> p
long long num = (*it).n; // 或 it -> n
cout << command << price << num; // buy1200
}
#include <unordered_set>
无序set
erase() 删除元素
clear() 清空元素
insert() 添加元素
count() 查找元素
size() 返回set存储元素的个数
// 定义set
unordered_set<string> uset;
string str = "123abc";
uset.insert(str.begin(), str.end());
cout << uset.size(); // 6
cout << uset.count('a'); // 1
#include <ctype.h>
大小写转化的方法
char a = 'a';
char b = toupper(a); // 转化为大写
cout << a << endl; // a
cout << b << endl; // A
char c = tolower(b); // 转化为小写
cout << c << endl; // a
#include<cmath>
n次方pow
与开根sqrt
int n = 2, k = 100;
cout << pow(k, n) << endl; // 10000 k的n次方 k^n
cout << sqrt(k) << endl; // 10 根号k
也可以把pow的n写为求1/n次幂,相当于开n次根
float n = 1.0 / 3.0, k = 8.0;
cout << pow(k, n) << endl; // 2.0 k的1/n次方 k^(1/n) 相当于开3次根
#include<stack>
stack<int> q; // 以int型为例
int x;
q.push(x); // 将x压入栈顶
q.top(); // 返回栈顶的元素
q.pop(); // 删除栈顶的元素,单独使用,无返回值,建议与top()一起用
q.size(); // 返回栈中元素的个数
q.empty(); // 检查栈是否为空,若为空返回true,否则返回false`
q.push(1);
q.push(2);
q.push(3);
q.push(4);
cout<< q.size() << endl; // 4
cout<< q.top() << endl; // 4 输出栈顶元素
q.pop(); //删除栈顶元素
cout<< q.top() << endl; // 4 输出栈顶元素
#include<queue>
queue<int> q;
q.push() // 在<队尾>插入一个元素
q.pop() // 删除<队头>第一个元素
q.front() // 返回队列中的第一个元素
q.back() // 返回队列中最后一个元素
q.size() // 返回队列中元素个数
q.empty() // 如果队列空则返回true
q.push(1);
q.push(2);
q.push(3);
q.push(4);
cout << q.front() << endl; // 1
cout << q.back() << endl; // 4
q.pop(); // 弹出队首的1
cout << q.front() << endl; // 2
csp前三题一般用到的就是这些,提到的方法是最常用的,后续有发现新的需求再补充。