#include <iostream>
using namespace std;
int main() {
int i = 1;
long long l = 2;
float f = 3.0;
double d = 4.0;
char c = 'c';
string s = "string";
}
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int a;
cin >> a;
cout << a;
double b;
while (cin >> b) {
printf("%.2f", b);
}
}
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
if (a > 10) {
cout << "a > 10" << '\n';
}
else if (a > 5) {
cout << "5 < a <= 10" << '\n';
}
else {
cout << "a <= 5" << '\n';
}
}
#include <iostream>
using namespace std;
int main() {
for (int i=0;i<10;i++) {
cout << i << '\n';
}
int T = 10;
while (T--) {
cout << T << '\n';
}
}
#include <iostream>
using namespace std;
int main() {
int a[10] = {1, 2, 3};
for (int i=0;i<10;i++) {
cout << a[i] << '\n';
}
int b[5][10] =
}
#include <iostream>
using namespace std;
struct P {
int x, y;
};
int main() {
P p[10];
p[0].x = 1, p[0].y = 2;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a(5);
a.push_back(1);
a.push_back(2);
a.push_back(3);
a[1] = 99;
a.pop_back();
cout << a[1] << ' ' << a.size();
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello";
string s2;
getline(cin, s2);
s[1] = 'a';
s = s.substr(2, 3); //位置+数量
s = s + "olo";
cout << s << ' ' << s.size() << '\n';
int index = s.find("lo");
while (index != s.npos) {
cout << index << '\n';
s.replace(index, 2, "?");
s.insert(index + 1, "!!");
index = s.find("lo", index + 1);
}
cout << s;
}
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> cnt;
string ss[5] = {"hello", "hello", "bye", "hello", "bye"};
for (int i=0;i<5;i++) {
cnt[ss[i]] ++;
}
cout << cnt["hello"] << '\n';
for (auto i = cnt.begin();i != cnt.end(); i++) {
cout << i->first << ' ' << i->second << endl;
}
}
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> sett;
sett.insert(2);
sett.insert(1);
sett.insert(3);
sett.insert(2);
sett.erase(1);
for (auto i = sett.begin(); i != sett.end(); i++) {
cout << *i << '\n';
}
for (auto i : sett) {
cout << i << '\n';
}
cout << sett.count(1) << ' ' << sett.size();
}
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
cout << q.front() << ' ' << q.size() << '\n';
q.pop();
cout << q.front() << ' ' << q.size() << '\n';
}
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> stk;
stk.push(1);
stk.push(2);
stk.push(3);
cout << stk.top() << ' ' << stk.size() << '\n';
stk.pop();
cout << stk.top() << ' ' << stk.size() << '\n';
}
#include <iostream>
#include <algorithm>
using namespace std;
struct P {
int x, y;
};
bool cmp(int x, int y) {
return x > y;
}
bool cmp2(P a, P b) {
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
int main() {
string s = "bcad";
sort(s.begin(), s.end());
int a[5] = {3, 2, 4, 5, 1};
sort(a, a + 5, cmp);
for (int i=0;i<5;i++) {
cout << a[i] << ' ';
}
P p[4] = {{1, 2}, {1, 1}, {3, 1}, {2, 1}};
sort(p, p + 4, cmp2);
for (int i=0;i<4;i++) {
cout << p[i].x << ',' << p[i].y << ' ';
}
}
#include <iostream>
#include <algorithm>
#include <utility>
#include <tuple>
using namespace std;
int main() {
pair<string, int> p[3] = {{"b", 2}, {"a", 2} ,{"b", 0}};
sort(p, p + 3);
for (int i=0;i<3;i++) {
cout << p[i].first << ',' << p[i].second << '\n';
}
p[2] = make_pair("d", 1);
for (int i=0;i<3;i++) {
cout << p[i].first << ',' << p[i].second << '\n';
}
}
#include <iostream>
#include <algorithm>
#include <tuple>
using namespace std;
int main() {
tuple<string, string, int> t1("hh", "nnb", 1);
auto t2 = make_tuple(1, 2.3, "hello");
cout << get<1>(t2);
}
#include <queue>
#include <iostream>
using namespace std;
bool cmp(int a, int b) {
return a < b;
}
struct tmp {
bool operator()(int a, int b) {
return a > b;
}
};
int main() {
priority_queue<int, vector<int>, greater<int>> q;
priority_queue<int, vector<int>, tmp> q2;
q.push(2);q2.push(2);
q.push(1);q2.push(1);
q.push(3);q2.push(3);
while (!q.empty()) {
cout << q.top() << endl;
q.pop();
}
while (!q2.empty()) {
cout << q2.top() << endl;
q2.pop();
}
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string s = "ni hao a !!!";
stringstream ss(s);
string word;
while (ss >> word) {
cout << word << '\n';
}
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream ss("nihao-hello-nb");
string s;
while (getline(ss, s, '-')) {
cout << s << endl;
}
}
#include <bits/stdc++.h> //不包含<map>
#include <iostream>
using namespace std;
int main() {
freopen("./input.txt", "r", stdin);
freopen("./output.txt", "w", stdout);
int a;
while (cin >> a) {
cout << a + 1 << '\n';
}
}
test < in.txt > out.txt
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int cnt = 100;
while (cnt--) {
printf("%f ", 1.0 * rand() / RAND_MAX);
}
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << isalpha('B') << '\n';
cout << isdigit('9') << '\n';
}
| 头文件名 | 主要功能说明 |
|---|
<iostream> | 输入输出流,用于 cin、cout |
<cstdio> | C语言风格输入输出,用于 scanf, printf |
<vector> | 动态数组容器 vector |
<string> | string 类型,字符串操作 |
<algorithm> | 常用算法函数,如 sort, reverse, min, max 等 |
<map> / <unordered_map> | 关联容器,键值对映射 |
<set> / <unordered_set> | 集合容器,自动去重 |
<queue> | 队列 queue、优先队列 priority_queue |
<stack> | 栈容器 |
<deque> | 双端队列 |
<list> | 双向链表结构 |
<utility> | 提供 pair 和 make_pair |
<tuple> | 元组类型 |
<functional> | 函数对象,如 greater<int>() |
<cmath> | 数学函数,如 sqrt, abs, pow |
<cstdlib> | C标准库函数,如 rand(), srand() |
<ctime> | 时间函数,常用于随机种子 srand(time(0)) |
<sstream> | 字符串流处理,如 stringstream |
<bitset> | 位集合操作 |
<limits> | 获取数据类型的极限值(如 INT_MAX, INT_MIN) |
<fstream> | 文件读写(机试较少用) |
| 函数/类 | 所属头文件 |
|---|
cin, cout | <iostream> |
vector | <vector> |
string | <string> |
sort, reverse | <algorithm> |
map, set | <map>, <set> |
queue, stack | <queue>, <stack> |
sqrt, abs | <cmath> |
stringstream | <sstream> |
rand, srand | <cstdlib> |
time | <ctime> |
pair | <utility> |
priority_queue | <queue> |
unordered_map | <unordered_map> |