The road to learning C++ primer-(Chapter 16 STL Template Sequence Container)

STL multiple containers and structures

1. Array (sequential container)

Structure diagram:

The size of the length is specified, and the space cannot be dynamically expanded

definition:

#include <array>
array<double,10>ary = {1,2,3,4,5,6,7,8,9,10}; //必须有第二个参数,要指定有多大

function:

1.get

This is a global function and not held by the array template

get (element offset, element type, element book in the array, the array selected from it)

#include <array>
array<int,4>c0={1,2,3,4};
get<1>(c0);

 

2.begin()

Get the iterator of the first position of the array

2.end()

Get an iterator to the next position of the last position of the array

3.size()

Get the length of the array

4.back()

Get the value of the last element of the array

5.data()

Get the first location address

6.front()

Get the value of the first position of the array

2.Vector (sequential container)

Structure diagram:

A vector is a backward-expanding container. When its space is insufficient, it expands to twice the original size, and then continues to double the original size when it is insufficient........

Their addresses are also a continuous

vector summary portal: https://blog.csdn.net/z1455841095/article/details/82668794

3. Deque (sequential container)

A container with two-way opening that can enter and exit.

Structure diagram:

This is just what you see on the surface is like this:

In fact, it is more specific like this:

 The first graph container stores an address that points to a section of buffer space. It depends on what the second graph looks like.

When both ends are filled, if you continue to push_back to add elements, the Deque container will automatically open up a buffer for storage and a seemingly continuous address points to it.

On the contrary, if you use push_front, it will also open up a buffer

4. List (sequential container)

Doubly linked list :

Structure chart

 

Example code:

#include<iostream>
#include <cstdlib>
#include <ctime>
#include <list>
#include <string>
#include <cstring>
#include <exception>
#include <array>
std::string get_a_target_string();
int main()
{
	using namespace std;
	srand(time(0));
	long value;
	cout << "how many elements: ";
	cin >> value;
	cout << "\ntest_list()........... \n";
	list<string>c;
	char buf[10];
	clock_t timeStart = clock();
	for (long i = 0; i < value; ++i)
	{
		try 
		{
			snprintf(buf, 10, "%d", rand());
			c.push_back(string(buf));
		}
		catch(exception & p)
		{
			cout << "i = " << i << " " << p.what() << endl;
			abort();//返回
		}
	}
	cout << "milli-seconds: " << (clock() - timeStart) << endl;

	cout << "list.size(): " << c.size() << endl;
	cout << "list.max_size(): " <<c.max_size() << endl;

	cout << "list.front(): " << c.front() << endl;
	cout << "list.back(): " << c.back() << endl;
	string target = get_a_target_string();
	timeStart = clock();
	auto pItem = find(c.begin(), c.end(), target);
	cout << "::find(), milli-seconds : " << (clock() - timeStart) << endl;
	if (pItem != c.end())
	{
		cout << "found, " << *pItem;
		cout << endl;
	}
	else {
		cout << "Not Found! " << endl;
	}

	timeStart = clock();
	c.sort();
	cout << "c.sort() milli-seconds : " << (clock() - timeStart) << endl;

	system("pause");
	return 0;
}
std::string get_a_target_string()
{
	using std::cin;
	using std::cout;
	int value;
	cout << "target (0~" << RAND_MAX << ": ";
	cin >> value;
	char buf[10];
	snprintf(buf, 10, "%d", value);
	return std::string(buf);
}

The list has a single sort member.

The order container with the highest space utilization is the most efficient but the sorting time is slow

5.Forward_list (sequential container)

Singly linked list

Structure diagram:

 Forward_list has a separate sort commonly used

6.Stack (sequential container)

Stack container (it is a first-in-last-out container)

Structure diagram:

 function:

1.push

Push elements into the stack container

2.pop

Remove the last element from the stack


7.Queue (heap container)

He is a first-in first-out container

 function:

1.push

Push elements onto the stack (added later)

2.pop

Always remove the first element from the stack

Guess you like

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