// File: iZhangHui[P]CppPrimer/CH09_SequentialContainers/examples/erase1.cc
// Function: main
int main()
{
    list<string> slist;
    string s;
    while (cin >> s)
        slist.push_back(s);  // read the contents into slist

    /* we'll explain find in chapter 10
     * find looks in the sequence denoted by its first two
     * iterator arguments for the value of its third argument
     * returns an iterator to the first element with that value
     * if that element exists in the input sequence
     * otherwise returns the end iterator
     */
    auto iter = find(slist.begin(), slist.end(), "Quasimodo");
    if (iter != slist.end())
         slist.erase(iter);

    auto orig = slist; // keep a copy before we destroy the contents
    slist.clear();     // delete all the elements within the container
    cout << "after clear, size is: " << slist.size() << endl;

    slist = orig; // restore the data
    slist.erase(slist.begin(), slist.end()); // equivalent
    cout << "after erase begin to end, size is: " << slist.size() << endl;

    slist = orig; // restore the data
    auto elem1 = slist.begin(), elem2 = slist.end();
    // ++elem2;
    // ++elem2;

    /* delete the range of elements between two iterators
     * returns an iterator to the element just after the last removed element
     * after the call elem1 == elem2
     */
    elem1 = slist.erase(elem1, elem2);
    cout << "after erase elem1 to elem2 size is: " << slist.size() << endl;

    if (elem1 != elem2)
        cout << "somethings wrong" << endl;
    else
        cout << "okay, they're equal " << endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movl $0x0, -0x4(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2700
leaq -0x40(%rbp), %rdi
callq 0x2160
movq 0x4cd5(%rip), %rdi      # 0x6fe0
leaq -0x40(%rbp), %rsi
callq 0x2150
movq %rax, -0xf0(%rbp)
jmp 0x231d
movq -0xf0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2140
movb %al, -0xf1(%rbp)
jmp 0x233b
movb -0xf1(%rbp), %al
testb $0x1, %al
jne 0x2347
jmp 0x2369
leaq -0x20(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x2720
jmp 0x2356
jmp 0x2304
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26db
leaq -0x20(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x27b0
movq -0x108(%rbp), %rdi
movq %rax, -0x60(%rbp)
callq 0x27e0
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
leaq 0x1c68(%rip), %rdx      # 0x4004
callq 0x2760
movq %rax, -0x100(%rbp)
jmp 0x23aa
movq -0x100(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x27e0
movq %rax, -0x70(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x2810
testb $0x1, %al
jne 0x23d5
jmp 0x23f3
leaq -0x78(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2890
movq -0x78(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x2840
movq %rax, -0x80(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x28b0
jmp 0x2405
leaq -0x20(%rbp), %rdi
callq 0x2970
movq 0x4bbb(%rip), %rdi      # 0x6fd0
leaq 0x1bf2(%rip), %rsi      # 0x400e
callq 0x2100
movq %rax, -0x110(%rbp)
jmp 0x242a
leaq -0x20(%rbp), %rdi
callq 0x29a0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, -0x118(%rbp)
jmp 0x244b
movq -0x118(%rbp), %rdi
movq 0x4b67(%rip), %rsi      # 0x6fc0
callq 0x2130
jmp 0x2460
leaq -0x20(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x29d0
jmp 0x2472
leaq -0x20(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x27b0
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2890
movq -0x128(%rbp), %rdi
callq 0x27e0
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x2890
movq -0x128(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xb0(%rbp), %rdx
callq 0x2a30
movq %rax, -0xc0(%rbp)
movq 0x4ae6(%rip), %rdi      # 0x6fd0
leaq 0x1b34(%rip), %rsi      # 0x4025
callq 0x2100
movq %rax, -0x120(%rbp)
jmp 0x24ff
leaq -0x20(%rbp), %rdi
callq 0x29a0
movq -0x120(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, -0x130(%rbp)
jmp 0x2520
movq -0x130(%rbp), %rdi
movq 0x4a92(%rip), %rsi      # 0x6fc0
callq 0x2130
jmp 0x2535
leaq -0x20(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x29d0
jmp 0x2547
leaq -0x20(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x27b0
movq -0x140(%rbp), %rdi
movq %rax, -0xc8(%rbp)
callq 0x27e0
movq %rax, -0xd0(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x2890
leaq -0xe8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x2890
movq -0x140(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
callq 0x2a30
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq 0x4a03(%rip), %rdi      # 0x6fd0
leaq 0x1a75(%rip), %rsi      # 0x4049
callq 0x2100
movq %rax, -0x138(%rbp)
jmp 0x25e2
leaq -0x20(%rbp), %rdi
callq 0x29a0
movq -0x138(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, -0x148(%rbp)
jmp 0x2603
movq -0x148(%rbp), %rdi
movq 0x49af(%rip), %rsi      # 0x6fc0
callq 0x2130
jmp 0x2618
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x2810
testb $0x1, %al
jne 0x2631
jmp 0x267e
movq 0x4998(%rip), %rdi      # 0x6fd0
leaq 0x1a2f(%rip), %rsi      # 0x406e
callq 0x2100
movq %rax, -0x150(%rbp)
jmp 0x264d
movq -0x150(%rbp), %rdi
movq 0x4965(%rip), %rsi      # 0x6fc0
callq 0x2130
jmp 0x2662
jmp 0x26b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2ab0
jmp 0x26db
movq 0x494b(%rip), %rdi      # 0x6fd0
leaq 0x19f3(%rip), %rsi      # 0x407f
callq 0x2100
movq %rax, -0x158(%rbp)
jmp 0x269a
movq -0x158(%rbp), %rdi
movq 0x4918(%rip), %rsi      # 0x6fc0
callq 0x2130
jmp 0x26af
jmp 0x26b1
leaq -0x98(%rbp), %rdi
callq 0x2ab0
leaq -0x40(%rbp), %rdi
callq 0x20d0
leaq -0x20(%rbp), %rdi
callq 0x2ab0
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x20d0
leaq -0x20(%rbp), %rdi
callq 0x2ab0
movq -0x48(%rbp), %rdi
callq 0x2190
nopw %cs:(%rax,%rax)