Articles

  • STL Iterator

    Table of Contents

    Iteartor Invalidation Rules

    Container/Operation Insertion Erasure Resizing
    Sequence containers      
    vector all iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated) every iterator and reference at or after the point of erase is invalidated as per insert/erase
    deque all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all iterators are invalidated, but references to elements are unaffected) erasing the last element invalidates only iterators and references to the erased elements and the past-the-end iterator; erasing the first element invalidates only iterators and references to the erased elements; erasing any other elements invalidates all iterators and references (including the past-the-end iterator) as per insert/erase
    list all iterators and references unaffected only the iterators and references to the erased element is invalidated as per insert/erase
    forward_list all iterators and references unaffected (applies to insert_after) only the iterators and references to the erased element is invalidated (applies to erase_after) as per insert/erase
    array (n/a) (n/a) (n/a)
    Associative containers      
    [multi]{set,map} all iterators and references unaffected only iterators and references to the erased elements are invalidated  
    Unsorted associative containers      
    unordered_[multi]{set,map} all iterators invalidated when rehashing occurs, but references unaffected [23.2.5/8]. Rehashing does not occur if the insertion does not cause the container’s size to exceed z * B where z is the maximum load factor and B the current number of buckets. only iterators and references to the erased elements are invalidated  
    Container adaptors      
    stack inherited from underlying container inherited from underlying container  
    queue inherited from underlying container inherited from underlying container  
    priority_queue inherited from underlying container inherited from underlying container  

    Note

    1. Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container.
    2. no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped. (Note: The end() iterator does not refer to any element, so it may be invalidated. —end note)

    反向迭代器 (Reverse Iterator)

    定义

    反向迭代器 (Reverse Iterator) 是一种反向遍历容器的迭代器。也就是,从最后一个元素到第一个元素遍历容器。反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问前一个元素,而–运算则访问下一个元素。

    作用

    1. 反向迭代器需要使用自减操作符:标准容器上的迭代器 (reverse_iterator) 既支持自增运算,也支持自减运算。 但是,流迭代器由于不能反向遍历流,因此流迭代器不能创建反向迭代器。
    2. 可以通过 reverse_iterator::base() 将反向迭代器转换为普通迭代器使用,从逆序得到普通次序。 这是因为: 有些容器的成员函数只接受 iterator 类型的参数,所以如果你想要在 ri 所指的位置插入一个新元素,你不能直接这么做,因为 vector 的 insert 函数不接受 reverse_iterator。如果你想要删除 ri 所指位置上的元素也会有同样的问题。erase 成员函数会拒绝 reverse_iterator,坚持要求 iterator。 为了完成删除和一些形式的插入操作,你必须先通过 base 函数将 reverse_iterator 转换成 iterator,然后用 iterator 来完成工作。
  • STL map and set

    STL map和set的使用虽不复杂,但也有一些不易理解的地方,如:

    • 为何map和set的插入删除效率比用其他序列容器高
    • 为何每次insert之后,以前保存的iterator不会失效
    • 为何map和set不能像vector一样有个reserve函数来预分配数据
    • 当数据元素增多时(1w 到2w个比较),map和set的插入和搜索速度变化如何
  • udev persistent name

    Introduction

    This article describes the workflow about udev daemon.

  • LDAP

    Introduction

    LDAP

  • Grub 引导 ISO

    GRUB
    GRand Unified Boot loader

    因为光盘安装和U盘安装都比较慢,所以最快还是从硬盘上的ISO文件引导开始安装,前提是有个现有的GRUB,可以在 /etc/grub.d/40_custom 文件中加入如下代码来生成启动项

  • MySQL Primer - Up and Running

    Introduction

    This article described the installation and service control of MySQL.

    [High Availability]:

  • Openconnect Used as an Alternative for Cisco Anyconnect

    Introduction

    Open client for Cisco AnyConnect VPN
    OpenConnect is a client for Cisco’s AnyConnect SSL VPN, which is supported by the ASA5500 Series, by IOS 12.4(9)T or later on Cisco SR500, 870, 880, 1800, 2800, 3800, 7200 Series and Cisco 7301 Routers, and probably others.

    Homepage http://www.infradead.org/openconnect/

    Install

    Ubuntu

    [will@ubuntu.vmg ~]$ sudo apt-get install network-manager-openconnect-gnome

    OS X

    homebrew openconnect.rb

    Configure

    Gateway: server address

  • C++ memory management

    因为你所编写的循环语句根本不能正确运行,所以当编译成可执行代码后,也不可能正常运行。语言规范中说通过一个基类指针来删除一个含有派生类对象的数组,结果将是不确定的。这实际意味着执行这样的代码肯定不会有什么好结果。多态和指针算法不能混合在一起来用,所以数组与多态也不能用在一起。

    C++中虚析构函数的作用--这样做是为了当用一个基类的指针删除一个派生类的对象时,派生类的析构函数会被调用。

  • C++ Template

    Template specialization

    template<size_t n>
    struct fibonacci {
    	enum { value = fibonacci<n-1>::value + fibonacci<n-2>::value };
    };
    
    template<> // specialization
    struct fibonacci<0> {
    	enum { value = 1 };
    };
    
    template<>
    struct fibonacci<1> {
    	enum { value = 1 };
    };
    
    TEST(fibonacci, fibonacci) {
    	EXPECT_EQ(1, fibonacci<0>::value);
    	EXPECT_EQ(1, fibonacci<1>::value);
    	EXPECT_EQ(2, fibonacci<2>::value);
    	EXPECT_EQ(3, fibonacci<3>::value);
    	EXPECT_EQ(5, fibonacci<4>::value);
    }
    
    template <typename T>
    struct is_pointer_type : std::false_type {};
    
    template <typename T>
    struct is_pointer_type<T*> : std::true_type {};
    
    template <typename T>
    constexpr bool is_pointer_type_v = is_pointer_type<T>::value;
    
  • Linux内存管理

    本篇介绍 Linux 内存管理。 当然这其中我们也会涉及一些诸如段页等内存管理的基本理论,但我们目的不是为了强调理论,而是为了指导理解开发中的实践,所以仅仅点到为止,不做深究。

    先从外部(用户编程范畴)来观察进程如何使用内存,等到对大家内存使用有了较直观的认识后,再深入到内核中去学习内存如何被管理等理论知识。 最后再通过一个实例编程将所讲内容融会贯通。