STL containers in the traversal of use

2010-08-12  来源:本站原创  分类:CPP  人气:107 

STL containers in accordance with stored divided into two categories according to the array is stored in the container (such as: vector, deque); another node is not a continuous form of storage container (eg: list, set, map). Using erase method to remove elements, need to pay attention to some problems.
Using list, set or remove certain elements traverse map can be used:

Proper use of Method 1
std:: list <int> List;
std:: list <int>:: iterator itList;
for (itList = List.begin (); itList! = List.end ();)
(
if (WillDelete (* itList))
(
itList = List.erase (itList);
)
else
itList + +;
)

Or

Proper use of Method 2
std:: list <int> List;
std:: list <int>:: iterator itList;
for (itList = List.begin (); itList! = List.end ();)
(
if (WillDelete (* itList))
(
List.erase (itList + +);
)
else
itList + +;
)

Here are two errors in use:

Error to use a
std:: list <int> List;
std:: list <int>:: iterator itList;
for (itList = List.begin (); itList! = List.end (); itList + +)
(
if (WillDelete (* itList))
(
List.erase (itList);
)
)

Or wrong to use 2
std:: list <int> List;
std:: list <int>:: iterator itList;
for (itList = List.begin (); itList! = List.end ();)
(
if (WillDelete (* itList))
(
itList = List.erase (+ + itList);
)
else
itList + +;
)

The right to use 1: erase the return value to get the location of the next element in the proper use of Method 2: before the call to erase method using the "+ +" to get the next element of the position error using Method 1: In the call to erase method After using the "+ +" to get the location of the next element, because the call to erase method then, the element's position has been deleted, if the basis of this old place to get the next location will be abnormal.
Error using Method 2: Ibid.

Here "+ +" operator with our usual understanding of the opposite, erase (itList + +) is to first get the location of the next element removed; erase (+ + itList) is removed later to obtain the location of the next element.

The use of vector, deque traverse remove elements, they can erase the return value to obtain the location of the next element:
Proper use
std:: vector <int> Vec;
std:: vector <int>:: iterator itVec;
for (itVec = Vec.begin (); itVec! = Vec.end ();)
(
if (WillDelete (* itVec))
(
itVec = Vec.erase (itVec);
)
else
itList + +;
)

相关文章
  • STL containers in the traversal of use 2010-08-12

    STL containers in accordance with stored divided into two categories according to the array is stored in the container (such as: vector, deque); another node is not a continuous form of storage container (eg: list, set, map). Using erase method to re

  • [Change] C + + STL basic container string, vector, list, deque, map 2010-08-09

    In the basic STL containers are: string, vector, list, deque, set, map map set and the preservation of elements of disorder are only available through the interface to access the elements inside set: a collection of elements used to determine one is

  • Learning STL map, STL set of basic data structures 2010-12-23

    Learning STL map, STL set of basic data structures Abstract: This article lists a few basic STL map and the STL set of questions, answers these questions by explaining the STL containers associated with the internal data structures, and finally prese

  • Boost and STL Learning Sourcebook 2011-01-06

    from http://blog.csdn.net/k2eats/archive/2008/09/06/2892948.aspx Quote Boost Library Study Guide Boost Chinese site After Boost library is a tempered, portable, providing the source code of C + + library, the library as a standard backup, is the C +

  • (Turn) STL: History and component Glance 2011-03-06

    - STL advantage is not wordy, and a C + + developer is concerned, in any case are not familiar with the STL is justified, it does not need any excuses. Hou Jie, the teacher's masterpiece "STL source code analysis" is really a great STL-depth stu

  • STL中算法 2013-07-03

    ############# http://www.cplusplus.com/reference/algorithm/ 这边都有了- <algorithm> Standard Template Library: Algorithms The header<algorithm>defines a collection of functions especially designed to be used on ranges of elements. A range is any se

  • [Translation] generic picture library (GIL) tutorial (on) 2011-01-14

    Quote from the original http://stlab.adobe.com/gil/html/giltutorial.html Translation quoted from (Note: to modify and re-translation) http://www.cppprog.com/boost_doc/libs/gil/doc/html/giltutorial.html The original English version of this tutorial is

  • Chrome inter-process communication (2) 2010-03-15

    Chrome inter-process communication (2) 1. Chrome process, the basic pattern of communication Inter-process communication, called IPC (Inter-Process Communication), much of the documentation in Chrome, there is an introduction to this, in here . There

  • Javaer alike C + +, key point 2010-03-22

    Transfer: Study: white South http://www.blogjava.net/calvin/archive/2005/10/10/15132.html The key is to C + + as Java's sister, has nothing to do C do. So do not buy <C/C++.....> in the name of the book, Introduction to recommend <Essential C++&g

  • Linux / UNIX C + + Senior Developer 2010-03-26

    Linux / UNIX, C + +, Boost, ACE, Shell Linux C + + Advanced Training Course --- Distance * Training Objectives: Linux / UNIX C + + Senior Software Engineer focused on Linux / UNIX server-side software development (background development). Course cove

  • C + + Library description 2010-04-02

    Foundation Classes 1, Dinkumware C + + Library Reference Site: http://www.dinkumware.com PJ Plauger prepared high-quality standard library. Dr. PJ Plauger is Dr. Dobb's Programming Award of Excellence winner. The preparation of the library has long b

  • Linux C + +. Boost.ACE ... 2010-04-21

    Linux / UNIX, C + +, Boost, ACE, Shell, Oracle / MySQL ...... Linux / UNIX C + + Advanced Training Course --- Distance * Training Objectives: Linux / UNIX C + + Senior Software Engineer focused on Linux / UNIX server-side software development (backgr

  • C / C + + header file list 2010-04-26

    C / C + + header files list ( C, traditional C + + # Include <assert.h> / / set the insertion point # Include <ctype.h> / / character handling # Include <errno.h> / / custom error code # Include <float.h> / / floating-point process

  • c + + library and resource List 2010-09-13

    1 Introduction Numerous times to hear "I want to start learning C + +!" Cry, heard countless "C + + is too complicated, I really can not learn" helplessness. Mr. Stan Lippman in "C + + Primer" in his book "C + + is the m

  • C + + library of more famous and learning experience 2010-09-13

    1, C + + description of the major well-known library - C + + Standard Library 2, C + + description of the major well-known library - prospective standard library Boost 3, C + + description of the major well-known database - GUI 4, C + + description o

  • C + + smart pointer Introduction 2010-09-15

    Memory leak is the C + + programmers have headaches a big problem. C + + lacks, like JAVA, C #, like, have a useful weapon in the GC so it's part of the memory management authority to the programmer. Although the existence of GC saves development tim

  • C + + Advanced Tutorial 2010-09-24

    C + + Advanced Course Course Overview C + + Intensive Curriculum in learning C + + syntax: C + + language based on abstract system, templates, C + + standard library full description: STL (STL containers, STL algorithms, iterators, etc.), String, I /

  • Linux C + + Advanced Tutorial 2010-09-26

    Linux / UNIX C + + Advanced Tutorial * Training Objectives: Linux / UNIX C + + Senior Software Engineer focused on Linux / UNIX server-side software development (background development), cultivate the professional Linux / UNIX C + + Senior Software E

  • Introduction C + + class library 2010-10-06

    Favorites C + + class library introduces another manifestation of the core language of C + + to maintain the efficiency of application of the library at the same time to develop trend!! In C + +, the library's status is very high. Father of C + + Bja

  • 44 classic computer science books 2010-11-12

    Source: http://www.cppblog.com/milkyway/articles/61038.html 1.The Art of Computer Programming Author: Donald.E.Knuth Web site: http://www-cs-faculty.stanford.edu/ ~ knuth / taocp.html Book Info: The book known as the 20th century the most important o