Поделиться через


list::insert (STL Samples)

Показывает, как использовать функцию список:: insert стандартной библиотеки шаблонов (STL) в Visual C++.

iterator insert(
   iterator It,
   const T& x = T( )
);
void insert(
   iterator It,
   size_type n,
   const T& x
);
void insert(
   iterator It,
   const_iterator First,
   const_iterator Last
);
void insert(
   iterator It,
   const T *First,
   const T *Last
);

Заметки

ПримечаниеПримечание

Имена класса и параметра в прототипе не соответствует версии в файле заголовка.Некоторые были изменены для улучшения удобочитаемости.

Каждые функции-члены вставки, прежде чем элемент указал на него в управляемой последовательности, последовательность указанная остальными операндами.Первый функцию-член вставляет один элемент со значением x и возвращает итератор, который указывает на вновь введенному элементу.Второй функцию-член вставляет повторение элементов n значения *x.*Последние 2 функции-члена входит в последовательность [First, Last).

Пример

// list_insert.cpp
// compile with: /EHsc
// Shows the various ways to insert elements into a
//               list<T>.

#include <list>
#include <iostream>

using namespace std ;

typedef list<int> LISTINT;

int main()
{
    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
    cout << "listInt:";
    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
    cout << "listInt:";
    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
    cout << "listInt:";
    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
    cout << "listInt:";
    for (i = listInt.begin(); i != listInt.end(); ++i)
        cout << " " << *i;
    cout << endl;
}

Output

listInt: 1 2 3
listInt: 1 2 3 4 4 4
listInt: 1 2 3 4 4 4 5 6 7
listInt: 1 2 3 4 4 4 5 6 7 10 11 12

Требования

Header: <LIST>

См. также

Основные понятия

Образец библиотеки стандартных шаблонов