STL list输出和增加

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include<list>
#include<ctime>
using namespace std;
struct Node
    {
        int a;
        char c;
        //头增加
        Node(int d, int e)
        {
            a = d;
            c = e;
        }
    };
void fun(Node& d)
    {
        cout << d.a << " " << d.c << endl;
    }
void ListCoutAdd()
{
    //输出
    /*Node no = { 12, 'a' };
    list<Node> db1(6, no);*/
    
    //迭代器循环输出。
    /*for (list<Node>::iterator ite = db1.begin(); ite != db1.end(); ite++)
    {
        cout << ite->a << " " << ite->c << endl;
    }*/
    
    //输出
    //cout << db1.back().a << " " << db1.back().c << endl;//back()函数返回一个引用,指向list最后一个元素。
    //cout << db1.front().a << " " << db1.front().c << endl; //back()函数返回一个引用,指向list第一个元素。

    //头增加
    /*list <Node> db;
    db.push_front(Node(12, 'b'));
    for_each(db.begin(), db.end(), fun);
    db.push_front(Node(13, 'c'));
    for_each(db.begin(), db.end(), fun);*/

    //尾添加
    /*list <Node> db2; 
    db2.push_front(Node(12, 'b'));
    db2.push_back(Node(13, 'c'));
    for_each(db2.begin(), db2.end(), fun);*/

    //中间添加
    list <Node> db2;
    list <Node> db3;
    db3.push_front(Node(16, 'b'));
    db3.push_front(Node(16, 'b'));
    db3.push_front(Node(16, 'b'));
    db3.push_back(Node(16, 'b'));

    db2.push_front(Node(12, 'b'));
    db2.push_back(Node(13, 'c'));
    list<Node>::iterator ite = db2.begin(); //迭代器
    ite++;//将下标移位到第二位。(数据第二位,下标为1)
    //db2.insert(ite, Node(15, 'd'));//插入一个元素。
    //db2.insert(ite,3,Node(16, 'e'));//插入3(多)个元素。
    db2.insert(ite,++db3.begin(),db3.end());//在db2中下标位置为1(数据第二位,下标为1)处插入db3中下标为[1,3)的元素。
    
    for_each(db2.begin(), db2.end(), fun);


}

int main()
{
    ListCoutAdd();
    
    system("pause");
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

clown_30

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值