The road to C++ learning STL associative container mulit (repeatable container)

1.multiset

The header file include <set> in the std namespace

Structure diagram:

In this structure, the key is the value, and the value is the key, regardless of whether the key and the value are both together.

There  can be repeated data . (The structure is based on the red-black tree (symmetric binary b-tree) to automatically adjust the balance of the structure)

function:

1.insert (element)

Insert an element or range of elements into a multiple set. There are multiple overloads of this function, please refer to the official documentation

2.find (find target)

Pass in a parameter, the parameter is key/value to search, this structure search is very fast. Almost 0 milliseconds

3.size

Returns the number of all elements in the container;

4.begin()

Returns an iterator of the first element of the container

5.end()

Returns an iterator of the next element of the last element of the container

6.erase()

Remove the corresponding element

parameter:

Where
is the location of the element to be removed.

First
The position of the first element to be removed.

The last one
to be removed is just beyond the position of the last element.

Key
The key value of the element to be removed.

There are many ways to wait. Consult the related documentation  multiset class

example:

#include <iostream>
#include <ctime>
#include <set>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
string get_a_string_target();

int main()
{
	srand(time(0));
	clock_t startTime = clock();
	cout << "how many elements: ";
	long int value;
	cin >> value;
	char buf[10];
	multiset<string>c;
	cout << "\ntest_multiset()..........\n";
	for (long i = 0; i < value; i++)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());		//rand()随机数转为字符串

			c.insert(string(buf));
		}
		catch (exception & p)
		{
			cout << "i = " << i << p.what() << endl;
			abort();
		}
	}
	cout << "milli-seconds : " << (clock() - startTime) << endl;
	cout << "multiset.size() = " << c.size() << endl;	//得到元素多少
	cout << "multiset.max_size() = " << c.max_size() << endl;	//得到具体multise容器空间的大小
	string target = get_a_string_target();	//得到一个字符串
	{
		startTime = clock();
		auto pItem = find(c.begin(), c.end(), target);	//用全局的find函数来查找数据
		cout << "::find(), milli-second: " << (clock() - startTime) << endl;
		if (pItem != c.end())
		{
			cout << "found, " << *pItem << endl;
		}
		else {
			cout << "not found " << endl;
		}
	}
	startTime = clock();
	auto pItem = c.find(target);
	cout << "c.find(), milli-second: " << (clock() - startTime) << endl;
	if (pItem != c.end())
	{
		cout << "found, " << *pItem << endl;
	}
	else {
		cout << "not found " << endl;
	}
	system("pause");
	return 0;
}
string get_a_string_target()
{
	int val;
	char buf[10];
	cout << "target (0~" << RAND_MAX << ") : ";
	cin >> val;
	snprintf(buf,  10, "%d", val);
	return string(buf);
}

result:

 

Analysis: 1. The above source code is mainly to prompt how much container space to enter, and then insert a random number with insert. This random number is processed as a string type, how long it takes to print and process this container, and the contents of the container, Why is 1000000? Because it is a repeatable, and the set utilization is better, and the global find algorithm function takes 197 milliseconds to find, and the container itself takes almost 0 milliseconds, so if the container itself This kind of function is built-in. 2. Why use try catch for loops? Because the input of 1 million container space may lead to insufficient system allocation and errors, so it tries to catch this error.

2.multimap(key-value容器)

Structure diagram:

From the structure diagram, a key value corresponds to a value key value but the key value cannot be repeated, but the value can be repeated.

This is also based on the red-black tree .

function

1.insert (element)

Insert an element or range of elements into a multiple set. There are multiple overloads of this function, please refer to the official documentation

2.find (find target)

Pass in a parameter, the parameter is the key to search, this structure search is very fast. Almost 0 milliseconds

3.size

Returns the number of all elements in the container;

4.begin()

Returns an iterator of the first element of the container

5.end()

Returns an iterator of the next element of the last element of the container

There are many other methods, please refer to the information multimap class

Example:

#include <iostream>
#include <ctime>
#include <map>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
long get_a_long_target();

int main()
{
	srand(time(0));
	clock_t startTime = clock();
	cout << "how many elements: ";
	long int value;
	cin >> value;
	char buf[10];
	multimap<long,string>c;	//key的类型为long 而value类型为string
	cout << "\ntest_multimap()..........\n";
	for (long i = 0; i < value; i++)
	{
		try
		{
			snprintf(buf, 10, "%d", rand());		//rand()随机数转为字符串

			c.insert(pair<long,string>(i,buf));	//使用pair来辅助map加入结构
		}
		catch (exception & p) 
		{
			cout << "i = " << i << p.what() << endl;
			abort();
		}
	}
	cout << "milli-seconds : " << (clock() - startTime) << endl;
	cout << "multiset.size() = " << c.size() << endl;	//得到元素多少
	cout << "multiset.max_size() = " << c.max_size() << endl;	//得到具体multimap容器空间的大小
	long target = get_a_long_target();	//得到一个long类型的数值
	startTime = clock();
	auto pItem = c.find(target);	//查找 key
	cout << "c.find(), milli-second: " << (clock() - startTime) << endl;
	if (pItem != c.end())
	{
		cout << "found, " << (*pItem).second << endl;	//second表示value第二个值 first表示第一个值
		cout << "found, key : " << (*pItem).first << " , value : " << (*pItem).second << endl;
	}
	else {
		cout << "not found " << endl;
	}
	system("pause");
	return 0;
}
long get_a_long_target()
{
	long val;
	cout << "target (0~" << RAND_MAX << ") : ";
	cin >> val;
	return val;
}

operation result:

Source code analysis:

Introduce the header file #include<map>

In the example, there is a sentence using pair<long,string>(i,buf). When there is a reference, this multimap is a combination of key and value, so it is necessary to combine key and value, so this is quoted, which is provided by the standard library , Another structure is called " pair " (a pair is two things (key and value)) or print the time it takes to open up one million containers as in the previous example. Use try catch to catch errors in the development process. , And finally use the find function that comes with c.find(key) to find the key and return an iterator (it can be said to be a pointer)... But this iterator has two attributes: one is first, the first is long in front What is defined is the key value, and second is the value of the string type defined by the previous pair. Finally, the result is printed...

Guess you like

Origin blog.csdn.net/z1455841095/article/details/82700270