1. 程式人生 > 其它 >【C++語法】STL

【C++語法】STL

cppreference

Container

Built-in Types Abstract Data Types
defined directly by C++ language defined by standard libraty
repersents computer hardware facilities usually don't implemented by computer hardware
e.g., int, array... e.g., vector, string...
graph LR; A(Containers)-->B(第一類容器) B-->D(Sequential Containers) D-->vector D-->list/forward_list D-->deque B-->E(Associative Containers 紅黑樹-自排序) E-->set/multiset E-->map/multimap B-->G(Unordered Associative Containers 雜湊表-讀取快) G-->unordered_set G-->unordered_map A(Containers)-->C(第二類容器) C-->F(Adaptors) F-->stack F-->queue/priority_queue

String

std::string::npos	//-1
/*初始化*/
string s(10, 'c');	//s: "cccccccccc"
/*遍歷*/
for(auto &c:s)   //&引用
/*normal*/
s.size(); s.empty();
/*find*/
s.find(substr, pos=0);		//return size_type
/*子串*/
s.substr(pos,count);		//return string
/*string, char [], char**/
...
/*string,int*/
to_string((int)i);    //<string>
/*Algorithms*/
reverse(s.begin(),s.end());

1. vector

/*Algorithms*/
vector<int>::iterator max = max_element(v.begin(),v.end());
reverse(v.begin(),v.end());
sort(v.begin(),v.end(),cmp()=NULL);
stable_sort(v.begin(),v.end(),cmp()=NULL);
from Chu