Home | History | Annotate | Download | only in map.modifiers
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <map>
     11 
     12 // class map
     13 
     14 // iterator erase(const_iterator position);
     15 
     16 #include <map>
     17 #include <cassert>
     18 
     19 #include "min_allocator.h"
     20 
     21 int main()
     22 {
     23     {
     24         typedef std::map<int, double> M;
     25         typedef std::pair<int, double> P;
     26         typedef M::iterator I;
     27         P ar[] =
     28         {
     29             P(1, 1.5),
     30             P(2, 2.5),
     31             P(3, 3.5),
     32             P(4, 4.5),
     33             P(5, 5.5),
     34             P(6, 6.5),
     35             P(7, 7.5),
     36             P(8, 8.5),
     37         };
     38         M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
     39         assert(m.size() == 8);
     40         I i = m.erase(next(m.cbegin(), 3));
     41         assert(m.size() == 7);
     42         assert(i == next(m.begin(), 3));
     43         assert(m.begin()->first == 1);
     44         assert(m.begin()->second == 1.5);
     45         assert(next(m.begin())->first == 2);
     46         assert(next(m.begin())->second == 2.5);
     47         assert(next(m.begin(), 2)->first == 3);
     48         assert(next(m.begin(), 2)->second == 3.5);
     49         assert(next(m.begin(), 3)->first == 5);
     50         assert(next(m.begin(), 3)->second == 5.5);
     51         assert(next(m.begin(), 4)->first == 6);
     52         assert(next(m.begin(), 4)->second == 6.5);
     53         assert(next(m.begin(), 5)->first == 7);
     54         assert(next(m.begin(), 5)->second == 7.5);
     55         assert(next(m.begin(), 6)->first == 8);
     56         assert(next(m.begin(), 6)->second == 8.5);
     57 
     58         i = m.erase(next(m.cbegin(), 0));
     59         assert(m.size() == 6);
     60         assert(i == m.begin());
     61         assert(m.begin()->first == 2);
     62         assert(m.begin()->second == 2.5);
     63         assert(next(m.begin())->first == 3);
     64         assert(next(m.begin())->second == 3.5);
     65         assert(next(m.begin(), 2)->first == 5);
     66         assert(next(m.begin(), 2)->second == 5.5);
     67         assert(next(m.begin(), 3)->first == 6);
     68         assert(next(m.begin(), 3)->second == 6.5);
     69         assert(next(m.begin(), 4)->first == 7);
     70         assert(next(m.begin(), 4)->second == 7.5);
     71         assert(next(m.begin(), 5)->first == 8);
     72         assert(next(m.begin(), 5)->second == 8.5);
     73 
     74         i = m.erase(next(m.cbegin(), 5));
     75         assert(m.size() == 5);
     76         assert(i == m.end());
     77         assert(m.begin()->first == 2);
     78         assert(m.begin()->second == 2.5);
     79         assert(next(m.begin())->first == 3);
     80         assert(next(m.begin())->second == 3.5);
     81         assert(next(m.begin(), 2)->first == 5);
     82         assert(next(m.begin(), 2)->second == 5.5);
     83         assert(next(m.begin(), 3)->first == 6);
     84         assert(next(m.begin(), 3)->second == 6.5);
     85         assert(next(m.begin(), 4)->first == 7);
     86         assert(next(m.begin(), 4)->second == 7.5);
     87 
     88         i = m.erase(next(m.cbegin(), 1));
     89         assert(m.size() == 4);
     90         assert(i == next(m.begin()));
     91         assert(m.begin()->first == 2);
     92         assert(m.begin()->second == 2.5);
     93         assert(next(m.begin())->first == 5);
     94         assert(next(m.begin())->second == 5.5);
     95         assert(next(m.begin(), 2)->first == 6);
     96         assert(next(m.begin(), 2)->second == 6.5);
     97         assert(next(m.begin(), 3)->first == 7);
     98         assert(next(m.begin(), 3)->second == 7.5);
     99 
    100         i = m.erase(next(m.cbegin(), 2));
    101         assert(m.size() == 3);
    102         assert(i == next(m.begin(), 2));
    103         assert(m.begin()->first == 2);
    104         assert(m.begin()->second == 2.5);
    105         assert(next(m.begin())->first == 5);
    106         assert(next(m.begin())->second == 5.5);
    107         assert(next(m.begin(), 2)->first == 7);
    108         assert(next(m.begin(), 2)->second == 7.5);
    109 
    110         i = m.erase(next(m.cbegin(), 2));
    111         assert(m.size() == 2);
    112         assert(i == next(m.begin(), 2));
    113         assert(m.begin()->first == 2);
    114         assert(m.begin()->second == 2.5);
    115         assert(next(m.begin())->first == 5);
    116         assert(next(m.begin())->second == 5.5);
    117 
    118         i = m.erase(next(m.cbegin(), 0));
    119         assert(m.size() == 1);
    120         assert(i == next(m.begin(), 0));
    121         assert(m.begin()->first == 5);
    122         assert(m.begin()->second == 5.5);
    123 
    124         i = m.erase(m.cbegin());
    125         assert(m.size() == 0);
    126         assert(i == m.begin());
    127         assert(i == m.end());
    128     }
    129 #if __cplusplus >= 201103L
    130     {
    131         typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
    132         typedef std::pair<int, double> P;
    133         typedef M::iterator I;
    134         P ar[] =
    135         {
    136             P(1, 1.5),
    137             P(2, 2.5),
    138             P(3, 3.5),
    139             P(4, 4.5),
    140             P(5, 5.5),
    141             P(6, 6.5),
    142             P(7, 7.5),
    143             P(8, 8.5),
    144         };
    145         M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
    146         assert(m.size() == 8);
    147         I i = m.erase(next(m.cbegin(), 3));
    148         assert(m.size() == 7);
    149         assert(i == next(m.begin(), 3));
    150         assert(m.begin()->first == 1);
    151         assert(m.begin()->second == 1.5);
    152         assert(next(m.begin())->first == 2);
    153         assert(next(m.begin())->second == 2.5);
    154         assert(next(m.begin(), 2)->first == 3);
    155         assert(next(m.begin(), 2)->second == 3.5);
    156         assert(next(m.begin(), 3)->first == 5);
    157         assert(next(m.begin(), 3)->second == 5.5);
    158         assert(next(m.begin(), 4)->first == 6);
    159         assert(next(m.begin(), 4)->second == 6.5);
    160         assert(next(m.begin(), 5)->first == 7);
    161         assert(next(m.begin(), 5)->second == 7.5);
    162         assert(next(m.begin(), 6)->first == 8);
    163         assert(next(m.begin(), 6)->second == 8.5);
    164 
    165         i = m.erase(next(m.cbegin(), 0));
    166         assert(m.size() == 6);
    167         assert(i == m.begin());
    168         assert(m.begin()->first == 2);
    169         assert(m.begin()->second == 2.5);
    170         assert(next(m.begin())->first == 3);
    171         assert(next(m.begin())->second == 3.5);
    172         assert(next(m.begin(), 2)->first == 5);
    173         assert(next(m.begin(), 2)->second == 5.5);
    174         assert(next(m.begin(), 3)->first == 6);
    175         assert(next(m.begin(), 3)->second == 6.5);
    176         assert(next(m.begin(), 4)->first == 7);
    177         assert(next(m.begin(), 4)->second == 7.5);
    178         assert(next(m.begin(), 5)->first == 8);
    179         assert(next(m.begin(), 5)->second == 8.5);
    180 
    181         i = m.erase(next(m.cbegin(), 5));
    182         assert(m.size() == 5);
    183         assert(i == m.end());
    184         assert(m.begin()->first == 2);
    185         assert(m.begin()->second == 2.5);
    186         assert(next(m.begin())->first == 3);
    187         assert(next(m.begin())->second == 3.5);
    188         assert(next(m.begin(), 2)->first == 5);
    189         assert(next(m.begin(), 2)->second == 5.5);
    190         assert(next(m.begin(), 3)->first == 6);
    191         assert(next(m.begin(), 3)->second == 6.5);
    192         assert(next(m.begin(), 4)->first == 7);
    193         assert(next(m.begin(), 4)->second == 7.5);
    194 
    195         i = m.erase(next(m.cbegin(), 1));
    196         assert(m.size() == 4);
    197         assert(i == next(m.begin()));
    198         assert(m.begin()->first == 2);
    199         assert(m.begin()->second == 2.5);
    200         assert(next(m.begin())->first == 5);
    201         assert(next(m.begin())->second == 5.5);
    202         assert(next(m.begin(), 2)->first == 6);
    203         assert(next(m.begin(), 2)->second == 6.5);
    204         assert(next(m.begin(), 3)->first == 7);
    205         assert(next(m.begin(), 3)->second == 7.5);
    206 
    207         i = m.erase(next(m.cbegin(), 2));
    208         assert(m.size() == 3);
    209         assert(i == next(m.begin(), 2));
    210         assert(m.begin()->first == 2);
    211         assert(m.begin()->second == 2.5);
    212         assert(next(m.begin())->first == 5);
    213         assert(next(m.begin())->second == 5.5);
    214         assert(next(m.begin(), 2)->first == 7);
    215         assert(next(m.begin(), 2)->second == 7.5);
    216 
    217         i = m.erase(next(m.cbegin(), 2));
    218         assert(m.size() == 2);
    219         assert(i == next(m.begin(), 2));
    220         assert(m.begin()->first == 2);
    221         assert(m.begin()->second == 2.5);
    222         assert(next(m.begin())->first == 5);
    223         assert(next(m.begin())->second == 5.5);
    224 
    225         i = m.erase(next(m.cbegin(), 0));
    226         assert(m.size() == 1);
    227         assert(i == next(m.begin(), 0));
    228         assert(m.begin()->first == 5);
    229         assert(m.begin()->second == 5.5);
    230 
    231         i = m.erase(m.cbegin());
    232         assert(m.size() == 0);
    233         assert(i == m.begin());
    234         assert(i == m.end());
    235     }
    236 #endif
    237 }
    238