School for so long data structures and algorithms, it is necessary to come to a summary, the way we look at learning outcomes during this time. Data structure C ++ language itself provides an example. If you can master these 13 data structure, we believe in learning other languages is not struggling.
Array Array
Array initialization time you need to know the size, the follow-up can not change its size, can be obtained elements stored in an index by index. In C ++ source code can be known by, it is actually packaged in arrays on the basis of C:
#include <array>
void testArray() {
// Create an array
std::array<int, 5> a = {2, 4, 6, 8, 10};
// traverse the array
for (const auto i : a) {
std::cout << i << std::endl;
}
for(int i = 0; i < a.size(); i++) {
std::cout << a[i] << std::endl;
}
// take the first value, by [index] Gets
std::cout << "a[0] = " << a[0] << std::endl;
// modify the first value in the array
a[0] = 5;
/**
check whether the index will be at cross-border, cross-border will crash, and a [index] will not;
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: array::at
*/
a.at(0);
// if the array is empty
a.empty();
// find the length of the array
std::cout << "a.size()=" << a.size() << std::endl;
// get the first four values
int value = std::get<4>(a);
std::cout << "a(4) = " << value << std::endl;
// Create an empty array, the array is equal to the value 0 or other type of value with
std::array<int, 5> a2;
std::cout << "end a2" << a2[0] << std::endl;
// whether to compare two elements in the array are equal
if (a == a2) {}
}
Variable arrays, vectors vector
In C ++ using Vector stored as an array variable, its capacity can be dynamically changed, initialization need not be determined when the size of the array. And basically the same method used by the array.
#include <vector>
// vector
void testVector() {
/**
vector <T> Array different from it, its size is variable
*/
std::vector<std::string> v;
// increase the capacity of the container, at least 20 receiving elements
v.reserve(20);
// Initialize a vector
std::vector<int> v2 = {2, 4, 5, 7};
// initialize the array to a vector
std::array<std :: string, 5> words {"one", "two","three", "four", "five"};
std::vector<std::string> words_copy {std::begin(words) , std::end(words)};
// Get or modify elements by v [index]
std::cout << "v[0] = " << v2[1] << std::endl;
// Get the first element
std::cout << "v.front() = " << v2.front() << std::endl;
// value of the insertion end 9
v2.push_back(9);
// value of the insertion end 2
v2.emplace_back(2);
// Removes the first element, the value passed is an iterator
v2.erase(v2.begin());
// length
v2.size();
// remove all elements
v2.clear();
// remove the last element
v2.pop_back();
// insert elements at the end of
v2.insert(v2.end(), 3);
}
Doubly linked list list
Doubly linked list having a pointer pointing to a front and a rear node of a node. C ++ in itself provides a two-way linked list.
#include <list>
void testList() {
list<string> words {"hello", "list"};
// header insertion elements
words.push_front("push_fron");
words.emplace_front("emplace_front");
// insert the tail
words.push_back("push_back");
words.emplace_back("emplace_back");
// insert the specified location
words.insert(words.begin()++, "insert");
// delete elements
words.remove("push_fron");
// to access the elements of the list through the iterator
list<string>::iterator beg_iter = words.begin();
list<string>::iterator end_iter = words.end();
cout << "beg_iter:" << *beg_iter << endl;
cout << "end_iter:" << *end_iter << endl;
for (auto a : words) {
cout << a << endl;
}
}
Singly linked list forward_list
Only a single list pointer points to the next node, on the front of a singly linked list we do a lot of arithmetic problems.
#include <forward_list>
void testForwardList() {
forward_list<string> flist {"name", "lefe", "hello"};
// calculate its size
auto count = distance(begin(flist), end(flist));
cout << "size:" << count << endl;
// insert head
flist.push_front("before3");
// insert in the head
flist.insert_after(flist.begin(), "before2");
// inserted in front of the head node
flist.insert_after(flist.before_begin(), "before1");
// singly linked list traversal
for (auto word : flist) {
cout << word << endl;
}
}
Queue queue
A queue is a FIFO data structure, C ++ underlying the use of "double-ended queue" to achieve. More details about the queue, you can see the contents of this graphic design of a circular queue.
#include <queue>
void testQueue() {
queue<int> s;
// into the team
s.push(1);
// the team
s.pop();
// First Team
s.front();
// the tail
s.back();
// team size
s.size();
}
Deque deque
Deque team may operate on the head and the tail element. In doing algorithm design we had a double-ended queue graphic design of a double-ended queue.
void testDeque() {
// initialize an empty deque
and <int> my_deque;
// initialize a deque containing two elements
deque<string> name_queue {"lefe", "wsy"};
cout << "[0] = " << name_queue[0] << endl;
// Get the head elements
cout << "front = " << name_queue.front() << endl;
// get the tail element
cout << "back = " << name_queue.back() << endl;
// into the team from the tail
name_queue.push_back("bx");
name_queue.pop_front();
}
Priority queue priority_queue
Priority queues and queues as ordinary elements can only be inserted in the tail, team head deletion of elements, but it has a feature team is always the first priority of the largest element, regardless of the order of a team into the team order. Bottom in C ++ using the "stack" to achieve, so the time complexity can be controlled in O (logn).
void testPriorityQueue() {
// Create a priority queue
priority_queue<int> q;
// add an element to the queue
q.push(4);
q.push(1);
for(int i = 0 ; i < q.size() ; i++) {
cout << q.top() << endl;
// removes the first element
q.pop();
}
// if the queue is empty
q.empty();
}
Heap heap
Heap is a complete binary tree, the value of a node is always greater than the value of the parent node (large root heap), you can use an array to represent a heap, C ++ provides a default is large root heap. In the heap sort, we have introduced the heap in detail. Illustrates the sort 6/10 - heap sort (Subject write). In this article, we implemented a heap hands to write a "heap."
In C ++ through the stack algorithm requires introduction #include <algorithm>
#include <algorithm>
void testHeap() {
vector<int> numbers {6, 20, 7, 3, 5};
/ * ** The determination method /
// make_heap(numbers.begin(), numbers.end(), [](int a,int b){return a < b;});
// After creating a heap, numbers element of the value: 20,6,7,3,5, large root heap
make_heap(numbers.begin(), numbers.end(), [](int a,int b){return a < b;});
// add an element to the heap
numbers.push_back(40);
// restructuring heap: 40,6,20,3,5,7 large root heap, make sure to call push_heap previous vertor a heap
push_heap(numbers.begin(), numbers.end());
// remove the top of the heap elements need to be removed in the tail element numbers are not automatically removed
pop_heap(numbers.begin(), numbers.end());
numbers.pop_back();
}
Stack stack
After the stack is a data structure of an advanced, C ++ to achieve the underlying use deque. Before the minimum stack algorithm, we present details on this data structure. Illustrates the minimum stack (LeetCode155. Min Stack) .
#include <stack>
void testStack() {
stack<int> s;
s.push(1);
s.pop();
cout << "top=" << s.top() << endl;
s.size();
}
Mapping map, unordered_map
map is a data structure stored the key and vaule, key is unique. C ++ provides an orderly map and disorder map "unordered_map."
#include <unordered_map>
#include <map>
void testMap() {
// Initialize
map<string, string> m;
pair<map<string, string>::iterator, bool> ret_iter =
m.insert(pair<string, string>("name", "lefe"));
if (ret_iter.second == false) {
cout << "name have existed" << endl;
}
// Initialize
map<int, string> m2 = {
{2014, "iOS"},
{2015, "Android"},
};
// insert a single value
m["name"] = "wsy";
// insert multi-valued
m.insert({{"age", "20"}, {"from", "nmg"}});
cout << "size = " << m.size() << endl;
// delete iterators
m.erase(m.begin());
// Find
map<string, string>::iterator find_iter = m.find("from");
if (find_iter != m.end()) {
cout << "find" << endl;
}
// traverse, key is ordered
for (pair<string, string> v : m) {
cout << v.first << " = " << v.second << endl;
}
// remove all elements
m.clear();
}
pair
two pair of the stored values, the values of these two types can be any type, it may be different. To obtain the corresponding values from the first and second.
void testPair() {
pair<string, string> p = {"name", "lefex"};
// to obtain first and second values through the first and second
cout << p.first;
cout << p.second;
}
Tuple tuple
It is a pair of expanded version can store multiple different types of elements.
void testTuple () {
pair<string, string> p = {"name", "lefe"};
// create a tuple, type <strinng, int, double, pair>
auto my_tuple = make_tuple("name", 20, 10.3, p);
// Get the first element
cout << "0 = " << get<0>(my_tuple) << endl;
// Get the second element
cout << "1 = " << get<1>(my_tuple) << endl;
}
Collection set
The collection can not be the same storage elements, based on its underlying achieve balanced binary tree, in the previous article, we realized set by binary search tree. The use of BST realize Set. In C ++ set is ordered, while also providing unordered set "UnorderSet."
#include <set>
#include <unordered_set>
void testSet() {
set<int> s {7, 3, 4};
s.insert(5);
// 3,4,5,7
for (auto v : s) {
cout << v << endl;
}
unordered_set<int> us {7, 3, 4};
us.insert(5);
// 7,3,4,5
for (auto v : us) {
cout << v << endl;
}
}
to sum up
We introduced the data structure of the C ++ language itself provides, linear and non-linear structure structure. These data structures also provide nearly in other languages, but the underlying implementation are basically the same, only to master all of these data structures principle, learning an additional language becomes very easy, when you call API Gengshuang.