map/set of data structure and algorithm

Posted by ucffool on Mon, 31 Jan 2022 07:41:58 +0100

map/set of data structure and algorithm

map and set are a kind of STL Association container. Generally, when used to quickly find out whether an element exists or repeats, they can be solved through the so-called hash set and hash mapping. In addition to being familiar with their underlying principles, they should also be able to use each container flexibly.

map

Several points to memorize

  1. The mapping function is realized through key value. The underlying implementation is a red black tree, while the key is ordered and non repeatable. The efficiency of query, addition and deletion is O (log n).

use
Follow the procedure

//Include header file
#include<map>
//Create + initialize
map<type(key),type(value)>mymap;
map<int,string>mymap{{1,"1"}}
map<int,string>newmymap(mymap);
map<int,string>newmymap(++mymap.begin(),mymap.end());
//iterator 
//Usage: + + P, P + +, - P, P --, * P, only = = and between iterators= Compare
for(auto iter=mymap.begin();iter!=mymap.end();iter++){
	cout<<iter->first<<iter->second<<endl;

Insert element
insert and []; []: if a key value pair with the specified data in the [] operator as the key is not stored in the map container, a new key value pair will be added to the current map container by using the [] operator. insert first look at the source code.
Where value_type is data of pair type.

pair<iterator,bool>insert(const value_type&x){return.. }
iterator insert (const_iterator position, const value_type& val)
..
int main() {

	map<int, string>mymap{ {1,"1"} };
	pair<int, string>value(2, "2");
	pair<int, string>value1(0, "5");
	mymap.insert(value);
	mymap.insert(++mymap.begin(), value1);
	mymap[3] = "3";
	for (auto iter = mymap.begin(); iter != mymap.end(); iter++) {
		cout << iter->first << ":" << iter->second << endl;
	}
	return 0;
	
}


Find an element
Commonly used find and count Find (key): find the key value pair with the key value in the map container, and successfully find and return the two-way iterator pointing to the key value pair. On the contrary, return the same iterator as end().
count: find the number of key value pairs whose key is key and return. Note that since the key value of each key value pair in the map container is unique, the maximum return value of this function is 1.
//There are two ways to find out whether a key exists:
if (hash.find(key) != hash.end()), indicating that the key exists
if (hash.count(key) != 0)

int main() {
	map<int, string>mymap{ {1,"1"} };
	pair<int, string>value(2, "2");
	pair<int, string>value1(0, "5");
	mymap.insert(value);
	mymap.insert(++mymap.begin(), value1);
	mymap[3] = "3";
	mymap[1] = "a";
	for (auto iter = mymap.begin(); iter != mymap.end(); iter++) {
		cout << iter->first << ":" << iter->second << endl;
	}
	int a=mymap.count(1);
	cout << a;
	auto it = mymap.find(1);
	if (it != mymap.end()) {
		cout << "existence"<<endl;
	}
	auto it1 = mymap.find(6);
	if (it1 != mymap.end()) {
		cout << "key Does not exist, this part will not be output";
	}
	if (mymap.count(1) != 0) {
		cout << "existence"<<endl;
	}
	return 0;
}

Modify value
Using mymap [key]: if the key does not exist, it will be added
Using mymap At (key): it will detect the key and throw an exception if it does not exist

Delete key value pair
Iterator failure problem

C++11
(1) 
iterator  erase (const_iterator position);
(2) 
size_type erase (const key_type& k);
(3) 
iterator  erase (const_iterator first, const_iterator last);
int main() {
	map<int, string>mymap{ {1,"1"} };
	pair<int, string>value(2, "2");
	pair<int, string>value1(0, "5");
	mymap.insert(value);
	mymap.insert(++mymap.begin(), value1);
	mymap[3] = "3";
	mymap[1] = "a";
	for (auto iter = mymap.begin(); iter != mymap.end(); iter++) {
		cout << iter->first << ":" << iter->second << endl;
	}
	mymap.erase(++mymap.begin());
	mymap.erase(2);
	for (auto iter = mymap.begin(); iter != mymap.end(); iter++) {
		cout << iter->first << ":" << iter->second << endl;
	}
	mymap.erase(mymap.begin(), mymap.end());
	for (auto iter = mymap.begin(); iter != mymap.end(); iter++) {
		cout << iter->first << ":" << iter->second << endl;
	}
	return 0;
}

other

//Count the frequency of key occurrence
static bool cmp(const pair<int, int>&a, const pair<int, int>&b) {
	return a.second > b.second;// Sort by frequency from large to small
}
int main() {
	map<int, int>myhash;
	//Count the frequency of occurrence of value
	vector<int>ans{ 1,2,3,1,2,3,1,2,3,1,2,2 ,5,6};
	for (int i = 0; i < ans.size(); i++) {
		myhash[ans[i]]++;
	}
	cout << myhash[1] << endl;
	//Find out the biggest value and be good at using pair
	int m = INT_MIN;
	for (auto iter = myhash.begin(); iter != myhash.end(); iter++) {
		if (iter->second > m) {
			m = iter->second;
		}
	}
	cout << m<<endl;
	//To customize the sorting method, first convert to vector, value
	vector<pair<int, int>>vec(myhash.begin(), myhash.end());
	sort(vec.begin(), vec.end(), cmp);
	for (auto iter = vec.begin(); iter != vec.end(); iter++) {
		cout << iter->first ;
	}
	cout << endl;
	return 0;
}

unordered_map

  1. The mapping function is also realized through key value, but the underlying implementation is hash table. By mapping the key code to a position in the hash table to access records and find o(1), it is often used in massive data processing. The sorting of elements is out of order, and the key cannot be repeated. (LRU hash linked list can realize order)

set

  1. Key and value are packaged together. Key is value. The bottom layer is also a red black tree with orderly keys. The query efficiency is also different from that of o(logn) and map. Because it is not in the form of key value pairs, there is no [] insertion method. In addition, map is a key that cannot be repeated and set is a value that cannot be repeated (key value unity).

unordered_set

  1. The underlying implementation is a hash table, the elements are unordered, and the efficiency of query, addition and deletion is O (1).

Classic questions:

Topics: C++ Algorithm data structure