码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-12-29 16:15:01      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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

#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;
    
}

技术分享

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

标签:

原文地址:http://www.cnblogs.com/david-zhao/p/5085753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!