- STL
- vector
- deque/list
- stack/queue
- priority_queue
- pair
- map/multimap
- set/multiset
- unordered_map/unordered_multimap/unordered_set/unordered_multiset
- DIY sort
- Iterator
- Tips
using namespace std;vector
vectordeque/listv; //create vector v(nSize); vector v(nSize, t); //create and assign the size and initial value vector v{1,2,3}; v.push_back(elem); v.popback(); //ATTENTION! return void! v.insert(pos,elem); v.emplace_back(elem); //construct quicker v.size(); v.erase(beg,end); //delete elements from iterator beg to end. The "end" argument can be omit.
deque is implemented by array, and lisi by double linked list. They can be used in the same way.
dequestack/queuedq(nSize, t); list lst(nSize, t); dq.push_front(elem); //can be replace with "emplace" too dq.pop_front(); //can push and pop at back too dq.front(); //return element in the front dq.back(); dp.empty(); //return bool value
Here we list all member functions.
stackpriority_queues(nSize, t); //default construct by deque, no iterator queue > q(nSize, t); s.empty(); s.emplace(); s1.swap(s2); s.push(elem); s.pop(); //both s.top(); //only stack, return reference q.front(); q.back(); //only queue, return reference
ATTENTION: compare function less() corresponds to a max heap, and function greater() to a min heap.
priorty_queuepairq; //default construct by vector(diffrent from normal queue) priorty_queue , less > q; //default compare function is less
pairmap/multimappair1("key1","value1"); pair pair2(make_pair("key2","value2")); pair1.first="key3"; pair1.second="value3";
mapset/multisetmp; map > mp2; //default less map mmp; mp.emplace("one",1); //can use "insert()" too mp["two"]=2; //insert a new pair mp.find("one"); //return a interator to the position or the end, can check if a key exists int count = mmp.count("key"); int val = mp["two"]; //if there is now such a key, insert a pair with default value mp.empty(); //check if is empty int count = mmp.erase("two"); //delete the element, count is the number of elements deleted mp.erase(mp.begin(), mp.end()); //when one augument means delete one element
setunordered_map/unordered_multimap/unordered_set/unordered_multisetst; multiset > mst; set.emplace("one"); set.find("one"); set.empty(); int count = mst.count("one"); int count = mst.erase("one");
Almost same usage as ordered container.
DIY sort- For associative containers (like set) or sort(), we can define and use a function object.
class cmp { public: //can use "struct cmp {" instread of line 1-2 too //override operator () bool operator ()(const string &a,const string &b) { //sort ascending by length return (a.length() < b.length()); } }; int main(){ setmyset{"11", "1", "111"}; return 0; }
- If the type of elements is not structure pointer or class pointer, we can overloaded relational operators in the member function of elements:
class myString { public: myString(string a):str(a){} string str; bool operator < (const myString &m)const { //const is necessary return str.length() < m.str.length(); } }; int main(){ vectorv{myString("11"), myString("1"), myString("111")}; sort(v.begin(), v.end()); return 0; }
- For sort(), we can define and use a normal function, too.
bool mycomp(string a, string b) { return (a.length() < b.length()); } int main(){ vectorIteratorv{"11", "1", "111"}; sort(v.begin(), v.end(), mycomp); return 0; }
for (auto first = values.begin(); first != values.end(); ++first) { cout << *first << " "; } //use "rbegin()" and "rend()" to iterate in reverse order mp.lower_bound(key0)->second //value of first element whose key is equal or bigger than key0 mp.upper_bound(key0)->second //value of first element whose key is bigger than key0 mp.equal_range(key0) //pair of iterator, in the pair the first is mp.lower_bound(key0), the second is mp.upper_bound(key0) auto it=values.begin(); auto it2=values.end(); int len=distance(it, it2); advance(it,-3); //move it back 3 position auto it3=it.prev(2); //it3 is the iterator before 3 position to it auto it4=it.next(5);Tips
- 取最大值时,为了使用松弛 *** 作等不使其移除,可取
const int INF = 0x3f3f3f3f; const int inf = 1000000007;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)