insert_3.cpp

来自「《C/C++程序员实用大全》配套代码,适用初学C++的人员。」· C++ 代码 · 共 56 行

CPP
56
字号
#ifdef __BCPLUSPLUS__
#include <list.h>
#else
#include <list>
#endif
#include <iostream>

using namespace std;

typedef list<int> LISTINT;

void main(void)
{
    int rgTest1[] = {5,6,7};
    int rgTest2[] = {10,11,12};

    LISTINT listInt;
    LISTINT listAnother;
    LISTINT::iterator i;

    // Insert one at a time
    listInt.insert (listInt.begin(), 2);
    listInt.insert (listInt.begin(), 1);
    listInt.insert (listInt.end(), 3);

    // 1 2 3
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;

    // Insert 3 fours
    listInt.insert (listInt.end(), 3, 4);

    // 1 2 3 4 4 4
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;

    // Insert an array in there
    listInt.insert (listInt.end(), rgTest1, rgTest1 + 3);

    // 1 2 3 4 4 4 5 6 7
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;

    // Insert another LISTINT
    listAnother.insert (listAnother.begin(), rgTest2, rgTest2+3);
    listInt.insert (listInt.end(), listAnother.begin(), listAnother.end());

    // 1 2 3 4 4 4 5 6 7 10 11 12
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << *i << " ";
    cout << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?