链表添加元素,然后插入、删除、最后遍历

链表添加元素,然后插入、删除、最后遍历

#include "stdafx.h"
#include <string>
#include <list>
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    list<string> staff;

    staff.push_back("Cracker, Carl");
    staff.push_back("Cacker, Harry");
    staff.push_back("Lam, Larry");
    staff.push_back("Sandam, Susan");

    /* add a value in fourth place */
    list<string>::iterator pos;
    pos = staff.begin();
    pos++;
    pos++;
    pos++;

    staff.insert(pos, "Reindeer, Rudolf");

    /* remove the value in second place */
    pos = staff.begin();
    pos++;

    staff.erase(pos);
    
    /* print all values */
    for ( pos = staff.begin(); pos != staff.end(); pos++ )
        cout << *pos << "\n";
    system("pause");
    return 0;
    
}

posted on 2015-12-29 15:00  抽筋的马  阅读(244)  评论(0编辑  收藏  举报

导航