• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // <deque>
11 
12 // iterator erase(const_iterator f)
13 
14 //  Erasing items from the beginning or the end of a deque shall not invalidate iterators
15 //  to items that were not erased.
16 
17 #include <deque>
18 #include <cassert>
19 
20 template <typename C>
del_at_start(C c)21 void del_at_start(C c)
22 {
23     typename C::iterator first = c.begin();
24     typename C::iterator it1 = first + 1;
25     typename C::iterator it2 = c.end() - 1;
26 
27     c.erase (first);
28 
29     typename C::iterator it3 = c.begin();
30     typename C::iterator it4 = c.end() - 1;
31     assert(  it1 ==   it3);
32     assert( *it1 ==  *it3);
33     assert(&*it1 == &*it3);
34     assert(  it2 ==   it4);
35     assert( *it2 ==  *it4);
36     assert(&*it2 == &*it4);
37 }
38 
39 template <typename C>
del_at_end(C c)40 void del_at_end(C c)
41 {
42     typename C::iterator first = c.end() - 1;
43     typename C::iterator it1 = c.begin();
44     typename C::iterator it2 = first - 1;
45 
46     c.erase (first);
47 
48     typename C::iterator it3 = c.begin();
49     typename C::iterator it4 = c.end() - 1;
50     assert(  it1 ==   it3);
51     assert( *it1 ==  *it3);
52     assert(&*it1 == &*it3);
53     assert(  it2 ==   it4);
54     assert( *it2 ==  *it4);
55     assert(&*it2 == &*it4);
56 }
57 
main()58 int main()
59 {
60     std::deque<int> queue;
61     for (int i = 0; i < 20; ++i)
62         queue.push_back(i);
63 
64     while (queue.size() > 1)
65     {
66         del_at_start(queue);
67         del_at_end(queue);
68         queue.pop_back();
69     }
70 }
71