STL中List容器类的使用方法

(A)使用List容器类处理整形数据和字符型数据的范例:

#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std;
//创建一个list容器的实例LISTINT
typedef list<int> LISTINT;

//创建一个list容器的实例LISTCHAR
typedef list<int> LISTCHAR;

int main(){
    //--------------------------
    //用list容器处理整型数据
    //--------------------------
    //用LISTINT创建一个名为listOne的list对象
    LISTINT listOne;
    //声明i为迭代器
    LISTINT::iterator i;

    //从前面向listOne容器中添加数据
    listOne.push_front (2);
    listOne.push_front (1);

    //从后面向listOne容器中添加数据
    listOne.push_back (3);
    listOne.push_back (4);

    //从前向后显示listOne中的数据
    cout<<"listOne.begin()--- listOne.end():"<<endl;
    for (i = listOne.begin(); i != listOne.end(); ++i)
        cout << *i << " ";
    cout << endl;

    //从后向前显示listOne中的数据
LISTINT::reverse_iterator ir;
    cout<<"listOne.rbegin()---listOne.rend():"<<endl;
    for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
        cout << *ir << " ";
    }
    cout << endl;

    //使用STL的accumulate(累加)算法
    int result = accumulate(listOne.begin(), listOne.end(),0);
    cout<<"Sum="<<result<<endl;
    cout<<"------------------"<<endl;

    //--------------------------
    //用list容器处理字符型数据
    //--------------------------

    //用LISTCHAR创建一个名为listOne的list对象
    LISTCHAR listTwo;
    //声明i为迭代器
    LISTCHAR::iterator j;

    //从前面向listTwo容器中添加数据
    listTwo.push_front ('A');
    listTwo.push_front ('B');

    //从后面向listTwo容器中添加数据
    listTwo.push_back ('x');
    listTwo.push_back ('y');

    //从前向后显示listTwo中的数据
    cout<<"listTwo.begin()---listTwo.end():"<<endl;
    for (j = listTwo.begin(); j != listTwo.end(); ++j)
        cout << char(*j) << " ";
    cout << endl;

    //使用STL的max_element算法求listTwo中的最大元素并显示
    j=max_element(listTwo.begin(),listTwo.end());
    cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
}
输出:



(B)使用List容器类处理字符串数组范例:

#include <iostream>
#include <iomanip>
#include <list>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	list<string> S;
	list<string>::iterator Index;

	//加入元素
	S.push_front("john");
	S.push_front("memi");
	S.push_front("bridge");
	S.push_back("daina");
	S.push_back("lulu");
	S.push_back("lisa");
	cout << "原来的:" << endl;
	for (Index = S.begin(); Index != S.end();Index++)
	{
		cout << *Index << " ";
	}
	cout << endl;

	//插入
	S.insert(find(S.begin(), S.end(), "daina"), "johnne");
	cout << "插入后的:" << endl;
	for (Index = S.begin(); Index != S.end(); Index++)
	{
		cout << *Index << " ";
	}
	cout << endl;

	//移除
	S.remove("bridge");
	cout << "移除后的:" << endl;
	for (Index = S.begin(); Index != S.end(); Index++)
	{
		cout << *Index << " ";
	}
	cout << endl;

	//取代
	*find(S.begin(), S.end(), "daina") = "William";
	cout << "取代后的:" << endl;
	for (Index = S.begin(); Index != S.end(); Index++)
	{
		cout << *Index << " ";
	}
	cout << endl;

	//排序
	S.sort();
	cout << "排序后的:" << endl;
	for (Index = S.begin(); Index != S.end(); Index++)
	{
		cout << *Index << " ";
	}
	cout << endl;
}
运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值