PBDS
一個比STL更強的東東
807
Policy-Based Data Structures
需要的東東
#include<bits/extc++.h>
using namespace __gnu_pbds;
萬用標頭
五種東東
-
trie
-
tree
-
hash
-
priority_queue
-
list
可以用的編譯器
C++11
Windows
編譯器: Msys2
直接使用沒問題
PS C:\Users\soleus> g++ --version
g++.exe (Rev10, Built by MSYS2 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
WSL
編譯器: 👇(內建的)
直接使用沒問題
root@MyComputerName:/mnt/c/Users/UserName/Desktop/C++# g++ --version
g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Mac
我沒有😭
DevC++
需要修正
Path:
/* Copyright (C) 1999-2019 Free Software Foundation, Inc.
This file is part of the GNU LIBICONV Library.
The GNU LIBICONV Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
The GNU LIBICONV Library is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU LIBICONV Library; see the file COPYING.LIB.
If not, see <https://www.gnu.org/licenses/>. */
/* When installed, this file is called "iconv.h". */
#ifndef _LIBICONV_H
#define _LIBICONV_H
#define _LIBICONV_VERSION 0x0110 /* version number: (major<<8) + minor */
extern __declspec (dllimport) int _libiconv_version; /* Likewise */
/* We would like to #include any system header file which could define
iconv_t, 1. in order to eliminate the risk that the user gets compilation
errors because some other system header file includes /usr/include/iconv.h
which defines iconv_t or declares iconv after this file, 2. when compiling
for LIBICONV_PLUG, we need the proper iconv_t type in order to produce
binary compatible code.
But gcc's #include_next is not portable. Thus, once libiconv's iconv.h
has been installed in /usr/local/include, there is no way any more to
include the original /usr/include/iconv.h. We simply have to get away
without it.
Ad 1. The risk that a system header file does
#include "iconv.h" or #include_next "iconv.h"
is small. They all do #include <iconv.h>.
Ad 2. The iconv_t type is a pointer type in all cases I have seen. (It
has to be a scalar type because (iconv_t)(-1) is a possible return value
from iconv_open().) */
/* Define iconv_t ourselves. */
#undef iconv_t
#define iconv_t libiconv_t
typedef void* iconv_t;
/* Get size_t declaration.
Get wchar_t declaration if it exists. */
#include <stddef.h>
/* Get errno declaration and values. */
#include <errno.h>
/* Some systems, like SunOS 4, don't have EILSEQ. Some systems, like BSD/OS,
have EILSEQ in a different header. On these systems, define EILSEQ
ourselves. */
#ifndef EILSEQ
#define EILSEQ
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* Allocates descriptor for code conversion from encoding ‘fromcode’ to
encoding ‘tocode’. */
#ifndef LIBICONV_PLUG
#define iconv_open libiconv_open
#endif
extern iconv_t iconv_open (const char* tocode, const char* fromcode);
/* Converts, using conversion descriptor ‘cd’, at most ‘*inbytesleft’ bytes
starting at ‘*inbuf’, writing at most ‘*outbytesleft’ bytes starting at
‘*outbuf’.
Decrements ‘*inbytesleft’ and increments ‘*inbuf’ by the same amount.
Decrements ‘*outbytesleft’ and increments ‘*outbuf’ by the same amount. */
#ifndef LIBICONV_PLUG
#define iconv libiconv
#endif
extern size_t iconv (iconv_t cd, char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft);
/* Frees resources allocated for conversion descriptor ‘cd’. */
#ifndef LIBICONV_PLUG
#define iconv_close libiconv_close
#endif
extern int iconv_close (iconv_t cd);
#ifdef __cplusplus
}
#endif
#ifndef LIBICONV_PLUG
/* Nonstandard extensions. */
#if 1
#if 0
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
<wchar.h>.
BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
included before <wchar.h>. */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#endif
#include <wchar.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* A type that holds all memory needed by a conversion descriptor.
A pointer to such an object can be used as an iconv_t. */
typedef struct {
void* dummy1[28];
#if 1
mbstate_t dummy2;
#endif
} iconv_allocation_t;
/* Allocates descriptor for code conversion from encoding ‘fromcode’ to
encoding ‘tocode’ into preallocated memory. Returns an error indicator
(0 or -1 with errno set). */
#define iconv_open_into libiconv_open_into
extern int iconv_open_into (const char* tocode, const char* fromcode,
iconv_allocation_t* resultp);
/* Control of attributes. */
#define iconvctl libiconvctl
extern int iconvctl (iconv_t cd, int request, void* argument);
/* Hook performed after every successful conversion of a Unicode character. */
typedef void (*iconv_unicode_char_hook) (unsigned int uc, void* data);
/* Hook performed after every successful conversion of a wide character. */
typedef void (*iconv_wide_char_hook) (wchar_t wc, void* data);
/* Set of hooks. */
struct iconv_hooks {
iconv_unicode_char_hook uc_hook;
iconv_wide_char_hook wc_hook;
void* data;
};
/* Fallback function. Invoked when a small number of bytes could not be
converted to a Unicode character. This function should process all
bytes from inbuf and may produce replacement Unicode characters by calling
the write_replacement callback repeatedly. */
typedef void (*iconv_unicode_mb_to_uc_fallback)
(const char* inbuf, size_t inbufsize,
void (*write_replacement) (const unsigned int *buf, size_t buflen,
void* callback_arg),
void* callback_arg,
void* data);
/* Fallback function. Invoked when a Unicode character could not be converted
to the target encoding. This function should process the character and
may produce replacement bytes (in the target encoding) by calling the
write_replacement callback repeatedly. */
typedef void (*iconv_unicode_uc_to_mb_fallback)
(unsigned int code,
void (*write_replacement) (const char *buf, size_t buflen,
void* callback_arg),
void* callback_arg,
void* data);
#if 1
/* Fallback function. Invoked when a number of bytes could not be converted to
a wide character. This function should process all bytes from inbuf and may
produce replacement wide characters by calling the write_replacement
callback repeatedly. */
typedef void (*iconv_wchar_mb_to_wc_fallback)
(const char* inbuf, size_t inbufsize,
void (*write_replacement) (const wchar_t *buf, size_t buflen,
void* callback_arg),
void* callback_arg,
void* data);
/* Fallback function. Invoked when a wide character could not be converted to
the target encoding. This function should process the character and may
produce replacement bytes (in the target encoding) by calling the
write_replacement callback repeatedly. */
typedef void (*iconv_wchar_wc_to_mb_fallback)
(wchar_t code,
void (*write_replacement) (const char *buf, size_t buflen,
void* callback_arg),
void* callback_arg,
void* data);
#else
/* If the wchar_t type does not exist, these two fallback functions are never
invoked. Their argument list therefore does not matter. */
typedef void (*iconv_wchar_mb_to_wc_fallback) ();
typedef void (*iconv_wchar_wc_to_mb_fallback) ();
#endif
/* Set of fallbacks. */
struct iconv_fallbacks {
iconv_unicode_mb_to_uc_fallback mb_to_uc_fallback;
iconv_unicode_uc_to_mb_fallback uc_to_mb_fallback;
iconv_wchar_mb_to_wc_fallback mb_to_wc_fallback;
iconv_wchar_wc_to_mb_fallback wc_to_mb_fallback;
void* data;
};
/* Requests for iconvctl. */
#define ICONV_TRIVIALP 0 /* int *argument */
#define ICONV_GET_TRANSLITERATE 1 /* int *argument */
#define ICONV_SET_TRANSLITERATE 2 /* const int *argument */
#define ICONV_GET_DISCARD_ILSEQ 3 /* int *argument */
#define ICONV_SET_DISCARD_ILSEQ 4 /* const int *argument */
#define ICONV_SET_HOOKS 5 /* const struct iconv_hooks *argument */
#define ICONV_SET_FALLBACKS 6 /* const struct iconv_fallbacks *argument */
/* Listing of locale independent encodings. */
#define iconvlist libiconvlist
extern void iconvlist (int (*do_one) (unsigned int namescount,
const char * const * names,
void* data),
void* data);
/* Canonicalize an encoding name.
The result is either a canonical encoding name, or name itself. */
extern const char * iconv_canonicalize (const char * name);
/* Support for relocatable packages. */
/* Sets the original and the current installation prefix of the package.
Relocation simply replaces a pathname starting with the original prefix
by the corresponding pathname with the current prefix instead. Both
prefixes should be directory names without trailing slash (i.e. use ""
instead of "/"). */
extern void libiconv_set_relocation_prefix (const char *orig_prefix,
const char *curr_prefix);
#ifdef __cplusplus
}
#endif
#endif
#endif /* _LIBICONV_H */
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++
檔名:
iconv.h
內容:
Trie 字典樹
一個STL沒有的東東
字典樹
H
P
T
U
A
U
U
W
HAI
HUN
PLW
TUR
TWN
US
PL
UK
UKR
L
48
L
I
509
N
36
W
680
R
90
N
886
R
380
K
44
S
011
模板
template<
typename Key,
typename Mapped,//null_type or 其他資料型態
typename Cmp_Fn = std::less<Key>,//比較涵式
//trie_string_access_traits<>
typename Tag = pat_trie_tag,
template<
typename Const_Node_Iterator,
typename Node_Iterator,
typename E_Access_Traits_,
typename Allocator_>
class Node_Update = null_trie_node_update,
typename Allocator = std::allocator<char>
//trie_prefix_search_node_update>
class trie;
一堆寒士
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
// 引索 值 比較涵式
typedef trie<string,int,trie_string_access_traits<>, pat_trie_tag,trie_prefix_search_node_update> TRIE;
//數據結構 節點更新方式
TRIE dict1,dict2,dict3;
int main(){
dict1.insert({"Monday",1});
dict1.insert({"Tuesday",2});
dict1.insert({"Wednesday",3});
dict2.insert({"January",1});
dict1.join(dict2);
dict1.split("Monday",dict3);
dict3.erase("Wednesday");
dict1.swap(dict2);
auto found=*dict2.find("January");
cout<<found.first<<" "<<found.second<<endl;
cout<<"dict2 Monday value: "<<dict2["Monday"]<<endl;
cout<<"size of dict3: "<<dict3.size()<<endl;
cout<<"is dict2 empty? "<<dict2.empty()<<endl;
}
dict2:
dict1:
dict3:
Monday: 1
Tuesday: 2
January: 1
Wednesday: 3
Monday: 1
Tuesday: 2
January: 1
Wednesday: 3
Monday: 1
Tuesday: 2
January: 1
Wednesday: 3
Monday: 1
Tuesday: 2
January: 1
Monday: 1
Tuesday: 2
January: 1
January: 1
Monday: 1
Monday: 1
Tuesday: 2
January: 1
Tree 樹
類似std::set, set::map的東東
樹
🌲
樹
rb_tree | 紅黑樹 | map, set 默認 |
---|---|---|
splay_tree | 伸展樹 | 低記憶體, 最糟O(n) |
ov_tree | 有序向量樹 |
🌳
模板
template<
typename Key,
typename Mapped,//null_type or 其他資料型態
typename Cmp_Fn = std::less<Key>,
typename Tag = rb_tree_tag,
//splay_tree_tag or ov_tree_tag
template<
typename Const_Node_Iterator,
typename Node_Iterator,
typename Cmp_Fn_,
typename Allocator_>
class Node_Update = null_tree_node_update,
typename Allocator = std::allocator<char> >
class tree;
一堆寒士
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
// 引索 值 比較涵式 數據結構
typedef tree<int,int,less<int>,rb_tree_tag, tree_order_statistics_node_update> TREE_INT;
//節點更新方式
TREE_INT tree1,tree2,tree3;
int main(){
tree1.insert({5,700});
tree1.insert({3,900});
tree1.insert({7,120});
tree2.insert({9,700});
tree1.join(tree2);
tree1.split(5,tree3);
tree2.copy_from_range(tree1.lower_bound(3), tree1.upper_bound(5));
//其他和map一樣
}
tree2:
tree1:
tree3:
3: 900
5:700
9:700
7:120
3: 900
5: 700
9: 700
7: 120
3: 900
7: 120
5: 700
9: 700
3: 900
7: 120
5: 700
9: 700
3: 900
5: 700
Hash 哈希
比STL快多了
table
- gp_hash_table
- 快
- cc_hash_table
- 相對慢
- std::unordered_map
- 更慢
模板
template<
typename Key,//引索
typename Mapped,//值
typename Hash_Fn = std::hash<Key>,//哈希函式
typename Eq_Fn = std::equal_to<Key>,//比較函式
typename Comb_Hash_Fn = direct_mask_range_hashing<>
//值轉換成位置函式
typename Resize_Policy = default explained below.
bool Store_Hash = false,//是否一對一儲存
typename Allocator = std::allocator<char> >
class cc_hash_table;
一堆寒士
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef gp_hash_table<int,int> GPHash;
GPHash table1,table2,table3;
int main(){
table1.insert({5,700});
table1.insert({3,900});
table1.insert({7,120});
table2.insert({9,700});
table1.erase(7);
table1.swap(table3);
cout<<table2[9]<<endl;
cout<<table1.empty()<<endl;
cout<<table2.size()<<endl;
}
tree2:
tree1:
tree3:
3: 900
5: 700
9: 700
7: 120
3: 900
5: 700
9: 700
3: 900
5: 700
9: 700
9: 700
3: 900
5: 700
9: 700
priority queue
優先ㄓㄨˋ列
STL也有
PBDS↔️STL
- 新增
- join
- clear
- modify
- operator[ ]
模板
template<typename Value_Type,//資料型態
typename Cmp_Fn = std::less<Value_Type>,//比較函式
typename Tag = pairing_heap_tag,
//thin_heap_tag
//binomial_heap_tag
//rc_binomial_heap_tag
//binary_heap_tag
typename Allocator = std::allocator<char > >
class priority_queue;
一堆寒士
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef __gnu_pbds::priority_queue<int> pq;
pq q1,q2,q3;
bool is_odd(int n){return n&1;}
int main(){
q1.push(7);
q1.push(9);
q1.push(3);
q2.push(6);
q1.modify(q1.begin(),2);
q2.join(q1);
q2.split(is_odd,q3);
cout<<q3.size()<<endl;
}
tree2:
tree1:
tree3:
3
7
6
9
2
7
6
9
2
7
6
9
2
7
6
9
2
7
6
9
list
STL也有
PBDS↔️STL
- 新增
- clear
- operator[ ]
- earse_if
- 減少
- rbegin, rend
- cbegin, cend
- reverse
- 和很多
初始化
template<typename Key,//引索
typename Mapped,//值
typename Eq_Fn = std::equal_to<Key>,//比較函式
typename Update_Policy = move_to_front_lu_policy<>,
//更新方式
typename Allocator = std::allocator<char> >
class list_update;
一堆寒士
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef list_update<int,int,std::equal_to<int>,lu_move_to_front_policy<>,std::allocator<char>> List;
List mylist;
int main(){
mylist.insert({3,7});
mylist.insert({4,9});
mylist.insert({0,100});
auto a=*mylist.find(4);
cout<<a.second<<endl;
cout<<mylist[3]<<endl;
mylist.clear();
}
mylist
0: 100
4: 9
3: 7
3: 7
4: 9
結論
PBDS
By 建中店自計算機研習社學術長807⁸⁰⁷
PBDS
- 111