鉴于每次刷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 !=