每個容器都有empty()方法,表示容器是不是空的;如果試圖存取空容器的值可能會RE或取到亂數
只有序列容器有索引值,當然是左閉右開,小心別讓i<0或i>=size()
避免從空容器中讀取或pop東西
對於序列容器來說,他們的第一項和最後一項分別是front()和back()
queue是front(), stack是top()
#include<algorithm>
using namespace std;
int main(){
int a[5];
sort(a, a + 5);
min(3, 5);
max('a', 47);
}
#include<algorithm>
using namespace std;
unsigned ran(){
static unsigned x = 0xdefaced;
return ++(x * 131);
}
int main(){
int a[5];
min({3, 4, 5});
fill(a, a + n, 7122);
int e = unique(a, a + n) - a;
pair<int, int> mnmx = minmax({1, 2, 3, 4, 5});
generate(a, a + n, ran;
int mx = max_element(a, a + n);
}
#include<algorithm>
using namespace std;
int main(){
int a[5] = {7, 9, 1, 3, 5};
reverse(a, a + n);
rotate(a, a + 2, a + n);
int l = lower_bound(a, a + n, 4);
int r = upper_bound(a, a + n, 4);
while(prev_permutation(a, a + 5));
next_permutation(a, a + 5);
shuffle(a, a + 5);
nth_element(a, a + 2, a + 5);
}
#include<iostream>
#include<bitset>
using namespace std;
int main(){
bitset<10> b(56789);
cout << b.to_string() << endl;
b.set();
cout << b.to_ulong() << endl;
b.reset();
b.flip(2);
cout << b[2] << endl;
b <<= 2;
// ^ | & << >> ~ 都可以用
}
某些題目甚至一定要 bitset 才過得了⋯⋯
rand()
內建 mt19937() 等
自己寫的 ran()
#include<cstdlib>
int main(){
srand(7122);
int n = 5;
int a[n];
for(int i = 0; i < n; ++i)
a[i] = rand();
}
#include<chrono>
#include<random>
int main(){
auto seed = std::chrono::\
high_resolution_clock::\
now().time_since_epoch().count();
std::mt19937 mt(seed);
int n = 5;
int a[n];
for(int i = 0; i < n; ++i)
a[i] = mt();
}
unsigned ran(){
static unsigned x = 0xdefaced;
return ++(x * 131);
}
#include<cstring>
int main(){
int a[1000];
memset(a, 0, sizeof(a));
}
isalpha Check if character is alphabetic (function )
isblank Check if character is blank (function )
iscntrl Check if character is a control character
isdigit Check if character is decimal digit (function )
isgraph Check if character has graphical representation (function )
islower Check if character is lowercase letter
isprint Check if character is printable (function )
ispunct Check if character is a punctuation character
isspace Check if character is a white-space (function )
isupper Check if character is uppercase letter
isxdigit Check if character is hexadecimal digit
#include<iostream>
#include<complex>
using namespace std;
int main(){
complex<double> a(1.0, 2.0);
cout << a << endl;
complex<double> b(3.0, 7.0);
complex<double> c = a * b;
cout << c.real() << ' ' << c.imag() << endl;
}
abs()
ceil()
floor()
round()
trunc()
log()
log10()
pow()
sqrt()
cbrt()
hypot() 這個超慢
tgamma(x):原本的 \(\Gamma(x)\)
lgamma(x):\(\log(\Gamma(x))\)