// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %rbx
subq $0x58, %rsp
leaq -0x30(%rbp), %rbx
movq %rbx, 0x8(%rbx)
movq %rbx, (%rbx)
andq $0x0, 0x10(%rbx)
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x2d20(%rip), %r14      # 0x3fe0
leaq -0x68(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1100
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x12ea
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1458
jmp 0x12c4
leaq -0x30(%rbp), %rbx
movq (%rbx), %rdi
leaq 0xd0c(%rip), %rdx       # 0x2004
movq %rbx, %rsi
callq 0x166a
cmpq %rbx, %rax
je 0x1311
leaq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x1468
leaq -0x48(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1480
leaq -0x30(%rbp), %rdi
callq 0x14ba
movq 0x2ca2(%rip), %rdi      # 0x3fd0
leaq 0xcd9(%rip), %rsi       # 0x200e
callq 0x10d0
movq -0x20(%rbp), %rsi
movq %rax, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1070
leaq -0x30(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x14dc
leaq -0x30(%rbp), %rdi
movq (%rdi), %rsi
movq %rdi, %rdx
callq 0x1502
movq 0x2c5f(%rip), %rdi      # 0x3fd0
leaq 0xcad(%rip), %rsi       # 0x2025
callq 0x10d0
movq -0x20(%rbp), %rsi
movq %rax, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1070
leaq -0x30(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x14dc
movq (%rbx), %rsi
movq %rbx, %rdi
movq %rbx, %rdx
callq 0x1502
movq %rax, %r14
movq 0x2c1a(%rip), %rdi      # 0x3fd0
leaq 0xc8c(%rip), %rsi       # 0x2049
callq 0x10d0
movq -0x20(%rbp), %rsi
movq %rax, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1070
cmpq %rbx, %r14
leaq 0xc9f(%rip), %rax       # 0x207f
leaq 0xc87(%rip), %rsi       # 0x206e
cmoveq %rax, %rsi
movq 0x2bde(%rip), %rdi      # 0x3fd0
callq 0x10d0
movq %rax, %rdi
callq 0x1070
leaq -0x48(%rbp), %rdi
callq 0x152a
leaq -0x68(%rbp), %rdi
callq 0x1060
leaq -0x30(%rbp), %rdi
callq 0x152a
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x143b
jmp 0x143b
jmp 0x142d
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x152a
jmp 0x143e
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x1060
leaq -0x30(%rbp), %rdi
callq 0x152a
movq %rbx, %rdi
callq 0x1140