zoukankan      html  css  js  c++  java
  • STL之List

    List的基本操作:http://www.cnblogs.com/scandy-yuan/archive/2013/01/08/2851324.html(转载)

    其本质为双端链表。

    1. List中earse函数的使用问题:

       正确使用如下:

            list<int> l{1,2,3,4,5,6};
        list<int>::iterator it;
        for (it = l.begin(); it != l.end(); ) {
            if (((*it) >= 3) && ((*it) <= 4)) {
                it = l.erase(it);
            }
            else
                it++;
        }

      错误使用如下:

    for (it = l.begin(); it != l.end(); it++) {
    		if (((*it) >= 3) && ((*it) <= 4)) {
    			it = l.erase(it);
    		}
    	}
    

      错误原因分析:

          当list中执行一次erase后,原来用来遍历的iterator就失效了。其行为是不可预知的。而earse操作会返回一个新的iterator指向list的下一个元素,所以如果想继续遍历,就得用返回的iterator继续操作。

       

    此鸟不飞则已,一飞冲天,不鸣则已,一鸣惊人~~~ 我要做这只鸟儿~~ O(∩_∩)O哈!
  • 相关阅读:
    112. Path Sum
    66. Plus One
    258. Add Digits
    268. Missing Number
    275. H-Index II
    274. H-Index
    264. Ugly Number II
    263. Ugly Number
    199. Binary Tree Right Side View
    222. Count Complete Tree Nodes
  • 原文地址:https://www.cnblogs.com/minesweeper/p/5906736.html
Copyright © 2011-2022 走看看