STL 進階&函式介紹
一堆
奇怪的
函式
來自蛋餅的提醒
-
每個容器都有empty()方法,表示容器是不是空的;如果試圖存取空容器的值可能會RE或取到亂數
-
只有序列容器有索引值,當然是左閉右開,小心別讓i<0或i>=size()
-
避免從空容器中讀取或pop東西
-
對於序列容器來說,他們的第一項和最後一項分別是front()和back()
-
queue是front(), stack是top()
<algorithm>
超好用。有點萬能。
這次要來講一些其他的東東
#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);
}
但是⋯⋯
記得去看 Ref 喔!
<bitset>
一點點 int
加一點點 bool
#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 才過得了⋯⋯
<random>
唬爛好幫手
以及最小覆蓋圓・模擬退火
大概有三種
-
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);
}
<cstring>
重設・比較
使用時機跟 string
一點關係也沒有
#include<cstring> int main(){ int a[1000]; memset(a, 0, sizeof(a)); }
注意條件!
他的運作方式是以 byte 做事
<cctype>
懶得手刻 if ?
都幫你做好了
- isalnum Check if character is alphanumeric
-
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
這麼多?!
其實好用的只有
- tolower
- toupper
<complex>
想要
FFT用複數嗎?
也都幫你寫好了~~
#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; }
開根號・三角函數都有
不要自己寫啦
<cmath>
這些是什麼?
-
abs()
-
ceil()
-
floor()
-
round()
-
trunc()
這些是什麼?
-
log()
-
log10()
-
pow()
-
sqrt()
-
cbrt()
-
hypot() 這個超慢
\(\Gamma()\) function
這啥?
\(\Gamma(x) = (x-1)!, \ x \in \mathbb{N}\)
- 那可以幹嘛?
用科學記號輸出排組!
tgamma(x):原本的 \(\Gamma(x)\)
lgamma(x):\(\log(\Gamma(x))\)
Copy of STL 進階
By yennnn
Copy of STL 進階
- 299