STL - STL Summary

· This is a lazy stuff

 

I. Introduction:

· STL: Standard Template Library .Standard Template Library

· STL in God horse? ? ? :

pair
Vector, Stack, Queue, the deque
The priority_queue
Map, SET
algorithm
CMP and operator overloading


 二、Pair

# The include · <Utility>
· There are two elements first and second.
Can compare the size of the first than the first further than second.

#include <cstdio>
#include <utility>

using namespace std;

int main() {
	pair <int, int> _pair1, _pair2;
	_pair1.first = 1, _pair1.second = 2;
	_pair2.first = 2, _pair2.second = 2;
	printf("%d\n", _pair1 < _pair2);
	return 0;
}

Three, vector

· # The include <vector>
· dynamic array
. * Vector implementation in the STL
vector is STL basis of other data structures.
· Common Interface:
push_back ();
[];
empty (), size (), Clear () .

#include <cstdio>
#include <vector>

using namespace std;

int main() {
	vector <int> vec;
	
	vec.push_back(1);
	vec.push_back(2);
	vec.push_back(3);
	printf("%d %d %d\n", vec[0], vec[1], vec[2]);
	printf("size = %d\n", vec.size());
	
	vec.clear();
	printf("size = %d\n", vec.size());
	return 0;
}

Begin · STL data structures () and end ()
internal data structures of most STL implementations have vector.
Because variable-length arrays, the address on behalf of STL has begin () and end () stored.
Begin () denote STL start address of a data structure.
end () indicate the address of an end of an STL.
general STL data structures have find () function, if STL is not found, returns end ().


 Four, stack

The include # * <Stack>
· stack.
* Common Interface:
Top ();
Push ();
POP ();
empty (), size ();
No Clear ().

#include <cstdio>
#include <stack>

using namespace std;

int main() {
	stack <int> s;
	
	while (!s.empty()) s.pop(); //clear

	s.push(1);
	s.push(2);
	s.push(3);
	printf("%d\n", s.top());
	s.pop();
	printf("%d\n", s.top());
	return 0;
}

 Five, queue

The include # * <Queue>
* queue.
* Common Interface:
Front (), Back ();
Push ();
POP ();
empty (), size ();
No Clear ().

#include <cstdio>
#include <stack>

using namespace std;

int main() {
	stack <int> s;
	
	while (!s.empty()) s.pop(); //clear

	s.push(1);
	s.push(2);
	s.push(3);
	printf("%d\n", s.top());
	s.pop();
	printf("%d\n", s.top());
	return 0;
}

 六, and

· # The include <the deque>
· deque.
* Common Interface:
Front (), Back ();
push_back (), push_front ();
pop_back (), pop_front ();
empty (), size (), Clear () .

#include <cstdio>
#include <deque>

using namespace std;

int main() {
	deque <int> d;
	
	d.clear();
	
	d.push_back(1);
	d.push_back(2);
	d.push_back(3);
	printf("%d\n", d.front());
	printf("%d\n", d.back());
	
	d.pop_back();
	printf("%d\n", d.front());
	printf("%d\n", d.back());
	
	d.push_front(100);
	d.push_front(200);
	d.push_front(300);
	printf("%d\n", d.front());
	printf("%d\n", d.back());
	
	d.pop_front();
	printf("%d\n", d.front());
	printf("%d\n", d.back());
	
	return 0;
}

七, bitset

The include # * <the bitset>
· binary array, each location assigned only 0 or 1.
* Common Interface:
[].
The bitset with better output cout.

#include <cstdio>
#include <iostream>
#include <bitset>

using namespace std;

int main() {
	bitset <5> b;
	
	b[1] = 1;
	b[2] = 1;
	
	cout << b << endl;
	
	return 0;
}

八、priority_queue

The include # * <Queue>
· priority queue (stack)  this useful.
* Common Interface:
Top ();
Push ();
POP ();
empty (), size ();
No Clear ().

#include <cstdio>
#include <queue>

using namespace std;

int main() {
	priority_queue <int> q; //澶ф牴鍫?
	//priority_queue <int, vector<int>, greater<int> > q; //灏忔牴鍫?
	
	while (!q.empty()) q.pop(); //clear
	
	q.push(1);
	printf("%d\n", q.top());
	q.push(2);
	printf("%d\n", q.top());
	q.push(3);
	printf("%d\n", q.top());
	q.push(4);
	printf("%d\n", q.top());
	
	q.pop();
	printf("%d\n", q.top());
	
	return 0;
}

Nine, set

The include # · <SET>
· set.
· Common Interface:
INSERT (), ERASE ();
Find ();
empty (), size (), Clear ().

#include <cstdio>
#include <set>

using namespace std;

int main() {
	set <int> s;
	
	s.clear();
	
	s.insert(1);
	s.insert(100);
	printf("%d\n", s.find(10) == s.end());

	s.insert(10);
	printf("%d\n", s.find(10) == s.end());
	
	s.erase(10);
	printf("%d\n", s.find(10) == s.end());
	
	return 0;
}

Ten, multiset  

· # The include <SET>
-repeatable set.
· Common Interface:
INSERT (), erase ();
Find (), COUNT ();
. Empty (), size (), Clear ()
Note that, erase here ( ) will all the same stuff all deleted.

#include <cstdio>
#include <set>

using namespace std;

int main() {
	multiset <int> m;
	
	m.clear();
	
	m.insert(1);
	m.insert(100);
	printf("%d\n", m.find(10) == m.end());

	m.insert(10);
	printf("%d\n", m.find(10) == m.end());
	
	m.insert(10);
	printf("%d\n", m.count(10));
	
	m.erase(10);
	printf("%d\n", m.find(10) == m.end());
	printf("%d\n", m.count(10));
	
	
	return 0;
} 

 Eleven, map

The include # * <Map>
. * Mapping
. · Map elements are inside the pair of type
-common interfaces:
INSERT (), ERASE ();
Find ();
[];
empty (), size (), Clear () .

#include <cstdio>
#include <map>

using namespace std;

int main() {
	map <int, int> m;
	
	m.clear();
	
	m.insert(make_pair(1, 1));
	m[2] = 2;
	m[3] = 3;
	
	printf("%d %d\n", m.find(1) == m.end(), m.find(4) == m.end());
	printf("%d %d\n", m[1], m[2]);
	
	m.erase(1);
	printf("%d\n", m.find(1) == m.end());
	return 0;
}

Twelve, hash_map

· Do not want to write your own hash, not recommended hash_map. C ++ is
· hash_map not a standard STL, so there is no in cplusplus.com above.
· On linux hash_map is __gnu_cxx inside, and unordered_map is c ++ 11 inside the data structure these two no law debugging in the exam.
100% is not recommended, really do not want / would handwritten hash, please use the map instead.


Thirteen, iterator

• Each STL data structure has a corresponding iterator, Chinese called iterators.
· Example set <int> iterator is set <int> :: iterator, iterator use is to have access to internal data structures of the elements.
For example set , multiset and internal map is sorted, we can from small to large accessed by the iterator.
· iterator itself is a pointer to access the internal elements to use * or ->.

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <set>

using namespace std;

int main() {
	srand(time(0));
	set<int> s;
	for (int i = 1; i <= 10; ++i) {
		int x = rand() % 100000;
		if (s.find(x) == s.end()) s.insert(x);
	}
	
	for (set<int>::iterator iter = s.begin(); iter != s.end(); iter++) {
		printf("%d ", *iter);
	}
	puts("");
	
	set<int>::iterator lower_iter = s.lower_bound(10000);
	set<int>::iterator upper_iter = s.upper_bound(10000);
	
	printf("%d %d\n", *lower_iter, *upper_iter);
	
	return 0;
}

十四、lower_bound(),upper_bound()

· For a set, and can be used lower_bound upper_bound to binary query:
· s.lower_bound (x) returns an Iterator, less than the minimum point of the element x, if there is no such element, a random value is returned.
· S.upper_bound (x) and s.lower_bound (x) is a mean, the only difference is not equal.
· upper_bound not return less than!


Fifteen, Caution

* Note: STL data structure is a premium, so we should always consider whether or not to empty, it will easily RE.
· RE typical example:
the Vector bounds subscript access.
Is empty stack access Top ().
· Address cross-border will not necessarily RE, but will still affect the normal operation of the program.


Sixteen, algorithm

There is also a · STL algorithm library, there are several relatively easy algorithm.
Sort () ordered sequence;
Reverse () inverted sequence;
random_shuffle () scrambled;
() to generate a successive arrangement next_permutation;
min (), max (), the swap () takes maximum and minimum, two switching elements.

#include <cstdio>
#include <algorithm>

using namespace std;

int main() {
	int x = 1, y = 2;
	
	printf("%d\n", min(x, y));
	printf("%d\n", max(x, y));
	
	swap(x, y);
	printf("%d %d\n", x, y);
	
	int a[10];
	a[1] = 5;
	a[2] = 6;
	a[3] = 3;
	a[4] = 4;
	
	sort(a + 1, a + 4 + 1); //end should be a+4 + 1!!!
	printf("%d %d %d %d\n", a[1], a[2], a[3], a[4]);
	
	reverse(a + 1, a + 4 + 1);
	printf("%d %d %d %d\n", a[1], a[2], a[3], a[4]);
	
	random_shuffle(a + 1, a + 4 + 1);
	printf("%d %d %d %d\n", a[1], a[2], a[3], a[4]);
	
	puts("");
	a[1] = 1, a[2] = 2, a[3] = 3;
	for (int i = 1; i <= 6; ++i) {
		printf("%d %d %d\n", a[1], a[2], a[3]);
		next_permutation(a + 1, a + 3 + 1);
	}
	return 0;
}

Seventeen, sort

· Algorithm of the more important function is the Sort ().
· Usefulness is a sequence from small to large sort.
The Sort () is not a simple quick sort, but the time complexity is O (nlogn) a.
The Sort () can sorting array or vector, but not to other sorting STL data structures.
Sort () of the first parameter is the beginning of the array, a second parameter is the position of the end of the array.
Sort () there is a third parameter, cmp.


Eighteen, reverse

· This is relatively simple, is to flip an array.
Arguments and sort as the first parameter is the beginning of the array, the second parameter is a position after the end of the array.


Nineteen, random_shuffle

· This is relatively simple, it is the contents of an array of upset.
· Parameters and the same sort, the first parameter is the beginning of the array, the second parameter is a position after the end of the array.


Twenty, next_permutation

· This is also relatively simple, is arranged to generate a current arrangement (lexicographical sense).
* Parameters and the same sort, the first parameter is the beginning of the array, the second parameter is a position after the end of the array.
* Of course, there a function prev_permutation (), arranged for generating a current on the array (meaning lexicographic order)  


·Note:

· STL in the interface forgot how to do?
cplusplus.com can query.

· STL in simple data (stacks, queues) structure recommended handwriting, because of bad checks.
· The Map, the SET more difficult to achieve, suggested STL.

· STL in the algorithm because the package constant is relatively large, the card may be constant, such as vector.

 

Guess you like

Origin www.cnblogs.com/konglingyi/p/11391131.html