Stl:list issues to be aware of when using

Source: Internet
Author: User

This article is only the problem I have encountered, for reference only.

#include <list>#include<iostream>using namespacestd;classfoo{ Public: Foo (inti) {m_idata =i;} voidSetData (inti) {m_idata =i;} intGetData () {returnM_idata;}Private:    intM_idata;};//to improve readability, define the names of containers and iteratorstypedef list<foo*>foolist;typedef foolist::iterator foolistitor;voidMain () {foolist C;//Create a linked list container//populate the list with three different elementsC.push_back (NewFoo (1)); C.push_back (NewFoo (2)); C.push_back (NewFoo (3));//Iterating through a linked list     for(Foolistitor itor = C.begin (); Itor! =c.end ();) {        if((*itor)->getdata () = =2)        {            Delete(*itor); Itor=c.erase (itor); }        Else++Itor; }//make sure that all objects are deleted and that the list does not automatically complete the task for(Foolistitor Itor2 = C.begin (); Itor2! = end (); + +Itor2)Delete(*itor2);} 

There are a few things to be aware of when using pointers to structures or objects that dynamically allocate memory.

1. You are responsible for releasing all allocated memory after you have finished using the object. Containers do not know what type they will use, so they cannot automatically free up memory for you.

2. Many operations may fail because they manipulate pointers to objects or structures directly, rather than to the object or structure itself. For example, the list's sort () function, which uses the < operator to compare values and sort them with this result. Even if the operator is legal for the class Foo, the sort of the list still follows the actual value of the pointer rather than the value of the data in the object. It is therefore necessary to design your own comparison operators to dereference the pointers before comparing them.

3. Remember that when you copy a container, only the pointer, not the object, is copied. If a duplicate pointer is produced, it is extremely difficult to determine which object needs to be deleted. The solution is to use smart pointers (smart pointer) or to avoid the STL routines or algorithms that copy elements between containers.

4. Note that when iterating through a linked list, it is very careful to remove elements from the list. Because deleting the currently pointed element will invalidate the iterator, you must make sure that the return value of the erase () function is used correctly, that it is the next legitimate location in the container that the function will retrieve, and that by assigning the return value to the old iterator, we skip the illegal position. But this has brought us new problems. The problem occurs when the For loop tries to increment the iterator at the end of the loop, because we have incremented the iterator to the next position with erase (). To solve this problem, we move the increment operation from the for loop body to the conditional selection statement in the loop and increment when the element is not deleted. (In general, it is better to use an algorithm to remove elements from a container instead of manually iterating, as the algorithm remove_if () can do this safely and efficiently).

Stl:list issues to be aware of when using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.