STL Quick Start tutorial is simple to use list of learning

STL Quick Start tutorial is simple to use list of learning

SUMMARY list
       list using a double link list (bidirectional linked list) is stored in the form of an element.
       list and vector sequences are the type of container, now let's talk about the differences between them, the only difference between them is; Vector is a contiguous memory space for storage, the list is not contiguous memory space storage, we can quickly associate sequential storage structure and chain storage structure linear form, if you have a basic knowledge of data structures, I believe you will soon be able to understand them. list discontinuous storage structure for storing data, is compared Vector, data list when inserting and deleting data, only a memory allocation or release element can disadvantages: slow random access.

1.Vector more than basic data types to be stored, and to store complex multi-List number type, I introduced the usage List stored in the form structures.
2. This is just a personal point of view, the specific use of the environment have to consider the specific requirements and needs.

node list
       defines the data to be stored in the form of their structure as a storage node, a simple definition of a Mylist_node structure.

struct Mylist_node
{
	int id;
	char *ch;
};

Constructor

  1. list <type> listname; // create an empty list
  2. list <type> listname (5); // Create a list containing the default value is 0
  3. list <type> listname (5,2); // Create comprising five elements and the value chain are both 2
  4. list <type> listname (listname1); // copy establish a list of listname1
  5. list <type> listname (listname1.begin (), listname1.end ()); // listname element is a region of the listname1.

Member function

listname.begin () // returns a pointer to the first element of the iterator
listname.end () // returns a pointer to the tail of the elements of the iterator

void print_node(Mylist_node &d)
{
	cout << d.id << " " << d.ch <<endl;
}
void list_node_init()
{
	Mylist_node no = { 1001,"李明" };
	list<Mylist_node> stu(5,no);

	for_each(stu.begin(), stu.end(), print_node);
}

Here Insert Picture Description

listname.rbegin () // return the first element of the list reverse, i.e. the last element in the original list.
listname.rend () // return the next element in the list reverse the last element, i.e., a position before the first element in the original list.

Add and remove elements

push_back () // add an element to the end of the List of
push_front () // add an element in the head List
pop_back () // remove the last element
pop_front () // removes the first element

The complete code

#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

struct Mylist_node
{
	int id;
	const char *ch;
};

ostream &operator << (ostream &os, Mylist_node & node_li)
{
	os << node_li.id << " " << node_li.ch << endl;
	return os;
}
void print_node(Mylist_node &d)
{
	cout << d.id << " " << d.ch << endl;
}
void list_node_init()
{
	Mylist_node no0 = { 1000,"王翔" };
	Mylist_node no1 = { 1001,"李明" };
	Mylist_node no2 = { 1002,"李四" };
	Mylist_node no3 = { 1003,"王强" };
	Mylist_node no4 = { 1004,"刘刚" };
	Mylist_node no5 = { 1005,"韦晓" };

	list<Mylist_node> stu(1, no1);   //使用构造函数初始化list
									 //使用push_back(),将数据插入list的尾部
									 //下面为插入四个Mylist_node结构的数据
	stu.push_back(no2);
	stu.push_back(no3);
	stu.push_back(no4);
	stu.push_back(no5);
	stu.push_front(no0);  //在list头部添加元素no0
    stu.resize(3);          //设置list大小为3

    cout<<stu.size()<<endl; //显示当前list的大小

    //stu.pop_back();         //删除list最后一个元素
    //stu.pop_front();          //删除list头部元素
	//将List中的元素输出
	for_each(stu.begin(), stu.end(), print_node);

    //判断list是否为空
    //返回0表示不为空,返回1,表示为空
    list<Mylist_node> stu1;

    cout<<stu.empty()<<endl;
    cout<<stu1.empty()<<endl;

	//使用迭代器输出list元素
	list<Mylist_node>::iterator li;
	for (li = stu.begin();li != stu.end();li++)
	{
		cout << *li << endl;
	}
}

int main()
{
	//list<int> li;
	//li.push_back(1);
	//li.push_back(2);
	//li.push_back(3);
	//li.push_back(3);
	//li.push_back(5);

	list_node_init();

	//system("pause");
	return 0;
}

Remove the line: (// stu.pop_back (); // remove the last element of list) in the comments, the results:
Here Insert Picture Descriptionremove the line: (// stu.pop_front (); // delete the list header element) in the comments , the results:
Here Insert Picture Descriptionchange the size of the list

listname.resize (3) // Set list size 3

The current size of the container 3 (container 3 contains only elements):
Here Insert Picture Description
determining whether the list is empty

listname.empty () // determines whether the list is empty, it returns 0 is not empty, a return, represents list is empty.

Here Insert Picture DescriptionOutput list using an iterator
Here Insert Picture Description on list there are a lot of content, will be supplemented later.

Copyright Notice

By the author learning content, prepared and provided, shall not be reproduced without permission from the author
About the Author For more go to: website

Guess you like

Origin blog.csdn.net/qq_38937025/article/details/91505199