struct node {
int x, y;
};
ostream& operator<<(ostream& out, node p) {
return out << p.x << p.y;
}
模板类、模板函数
template<typename T>
T sum(T* begin, T* end) {
T ans = 0;
for (T *p = begin; p != end; ++p)
ans += *p;
return ans;
}
template<typename T>
struct Point {
T x, y;
};
对于upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针,lower_bound则是返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针。
unique函数删除有序数组中重复元素
优先队列:
struct cmp {//重载()符号
bool operator()(const long long a, const long long b) {//a优先级小于b时返回true
return a > b;
}
};
priority_queue<long long, vector<long long>, cmp > all;
生成随机数
#include<ctime>
#include<cstdlib>
void fill_random_int(vector<int>& v, int cnt) {
v.clear();srand(time(NULL));//初始化随机数种子
while (cnt--)
v.push_back(rand());//rand()生成[0,32767]的随机数
}
assert(表达式) 当表达式真时无变化,假时强行终止程序,并给出错误提示。
大整数:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#pragma warning(disable:4996)
struct BigInteger {
static const int BASE = 100000000;
static const int WIDTH = 8;
vector<int> s;
BigInteger(long long num = 0) { *this = num; }
BigInteger operator = (long long num) {
s.clear();
do {
s.push_back(num % BASE);
num /= BASE;
} while (num > 0);
return *this;
}
BigInteger operator = (const string& str) {
s.clear();
int x, len = (str.length() - 1) / WIDTH + 1;
for (int i = 0; i < len-1; ++i) s.push_back(stoi(str.substr(str.length() - (i + 1)*WIDTH, WIDTH)));
s.push_back(stoi(str.substr(0, (str.length() - 1) % 8 + 1)));
return *this;
}
};
ostream& operator << (ostream &out, const BigInteger& x) {
for (int i = x.s.size() - 1; i >= 0; --i)
out << x.s[i];
return out;
}
istream& operator << (istream &in, BigInteger& x) {
string s;
if(!(in >> s)) return in;
x = s;
return in;
}