基础知识
基础知识
常用 OJ
常用语言 - C++
终于用回 C++ 了,上程序设计基础转 C 好累
万能头文件 #include<bits/stdc++.h>
等于以下头文件:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
(Visual Studio 不自带万能头文件)
时间(空间)复杂度
dddd懂的都懂
C++
STL
string · 字符串
头文件 : #include<string>
定义、赋值
string s1 = "1234";
string s2;
s2 = "1234";
读写
cin/cout
string s; cin >> s; //到空格为止 cout << s << endl;
getline
string s; getline(cin, s); //到回车为止
常用函数
清除
s1.clear();
求长度
l = s1.length();
在尾部插入字符
s1.push_back('x');
在尾部插入字符串
s1.append(s2);
从s1中找到字符 'x' 的位置
pos = s1.find('x');
删除
s1.erase()
vector
构造
vector<int>v1;
vector<string>v2("1234");
vecotr<int>v3(5,2);
vector<int>v4(v3);
常用函数
在数组的最后添加一个数据
v1.push_back();
去掉数组的最后一个数据
v1.pop_back();
得到数组头的引用
v1.front();
得到数组的最后一个单元的引用
v1.back();
获取 vector 大小
v1.size();
时间复杂度
- 随机访问 O(1)
- 删除 O(n)
queue · FIFO
构造
queue<int> q;
常用函数
入队
q.push();
出队
q.pop();
首位
q.front();
末位
q.back();
大小
q.size();
判断是否为空
q.empty();
stack · FILO
构造
stack<int> s;
常用函数
入队
s.push();
出队
s.pop();
首位
s.top();
大小
s.size();
判断是否为空
s.empty();
set · 集合
构造
set<int>s;
常用函数
插入
s.insert();
清除
s.erase();
查找
s.find([int]);
大小
[int] = s.size();
清空
s.clear();
值元素个数
s.count([int]);
首元
s.begin();
末元
s.end();
是否为空
s.empty();
时间复杂度
- 查询 O(logn)
- 插入 O(logn)
- 删除 O(logn)
map · key - value
构造
1.map<int,int>mp;
2.map<string,int>mp;
常用函数
访问
1.
mp[[int]] = [int];
2.
mp[[string]] = [int];
时间复杂度
- 插入 O(logn)
- 删除 O(logn)
list · 双向链表
构造
list<int>li;
常用函数
加到末位
li.push_back();
加到首位
li.push_front();
去掉末位
li.pop_back();
去掉首位
li.pop_front();
插入
li.insert();
删除
li.erase();
时间复杂度
- 随机访问 ×
- 插入 O(1)
- 删除 O(1)
sort · 快速排序
#include<algorithm>
用习惯 sort 后受够了 qsort 的折磨,现在终于回来了
qsort 到现在都还没学会,还好期末开卷照着书打
struct
重载运算符
#define _CRTSECURE_NOWARNINGS
#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<list>
using namespace std;
int n;
struct NODE
{
int a, b, c;
bool operator == (const NODE x) const
{
return this->a + b + c == x.a + x.b + x.c;
}
bool operator != (const NODE x) const
{
return this->a + b + c != x.a + x.b + x.c;
}
bool operator <= (const NODE x) const
{
return this->a + b + c <= x.a + x.b + x.c;
}
bool operator >= (const NODE x) const
{
return this->a + b + c >= x.a + x.b + x.c;
}
bool operator < (const NODE x) const
{
return this->a + b + c < x.a + x.b + x.c;
}
bool operator > (const NODE x) const
{
return this->a + b + c > x.a + x.b + x.c;
}
} s[1001];
bool cmp(NODE x, NODE y)
{
return x < y;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d%d%d", &s[i].a, &s[i].b, &s[i].c);
sort(s + 1, s + n + 1, cmp);
for (int i = 1; i <= n; i++) printf("%2d %d %d %d\n", s[i].a + s[i].b + s[i].c, s[i].a, s[i].b, s[i].c);
}
封装函数
#define _CRTSECURE_NOWARNINGS
#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<list>
using namespace std;
struct NODE
{
int a[1001];
void scan()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
}
int sum(int x)
{
int SUM = 0;
for (int i = 1; i <= x; i++)
{
SUM += a[i];
}
return SUM;
}
} s;
int main()
{
s.scan();
printf("%d", s.sum(5));
return 0;
}