程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> STL學習系列之三:操作list容器

STL學習系列之三:操作list容器

編輯:關於C++

學習完了STL系列之二,自己寫了個程序練手!程序采用的還是系列之二文章的架構。學習了STL之一和之二,對於STL的基本原理算有個個基本的了解。其實關於這幾種容器,以前也都接觸過,不過是在java上,當時學習時也是囫囵吞棗!現在感覺那真是學習之大忌,還是一步一個腳印為好。速度可以放慢點,那要扎實!

注意:程序在vc6下調試通過,對於不清楚如何在vc下運行STL者,可以讀STL系列之一。

//TjuAiLab
//Author:zhangbufeng
//Time:2005.8.23 22:00
#include <string>
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;

PrintString(const string& StringToPrint);
const string NameCode("Bufeng");
class IsBufeng
{
public:
bool operator()(string &StringName)
{
 return StringName.substr(0,6)==NameCode;
}
};
void main(void)
{
//定義一個list
list<string>AIStudentName;
list<string>TargetAIStudentName;
list<string>::iterator AIStudentNameIterator;
//使用的list的成員函數push_back和puch_front插入元素到list中,使用成員函數insert任意插入
  AIStudentName.push_back("BufengZhang");
AIStudentName.push_back("YangZhang");
AIStudentName.push_back("KunHuang");
AIStudentName.push_front("ChengLuo");
AIStudentName.push_front("YonghuoYang");
AIStudentName.push_front("XiaoyuanCui");
AIStudentName.insert(AIStudentName.end(),"KefeiGong");
TargetAIStudentName.push_back("BufengZhang");
TargetAIStudentName.push_back("YangZhang");
//使用list的成員函數empty判斷list是否為空,size來返回成員個數
if(AIStudentName.empty())
{
 cout<<"AIStudentName的成員為空"<<endl;
}
else
{
 cout<<"AIStudentName的成員個數為"<<AIStudentName.size()<<endl;
}
//使用for循環和迭代器處理list的元素
cout<<"AIStudentName的成員如下(使用for循環和迭代器:"<<endl;
for(AIStudentNameIterator=AIStudentName.begin();AIStudentNameIterator!=AIStudentName.end();
   AIStudentNameIterator++)
 {
 cout<<*AIStudentNameIterator<<endl;
 }
  //使用STL的通用算法for_each來處理list中的元素
cout<<"AIStudentName的成員如下(使用STL的通用算法for_each:"<<endl;
for_each(AIStudentName.begin(),AIStudentName.end(),PrintString);
//學習使用STL的通用算法count和count_if
int NumberofStudent=0;
NumberofStudent=count (AIStudentName.begin(),AIStudentName.end(),"BufengZhang");
cout<<"TjuAIlab中有"<<NumberofStudent<<"個BufengZhang"<<endl;
NumberofStudent=count_if(AIStudentName.begin(),AIStudentName.end(),IsBufeng());
cout<<"TjuAIlab中有"<<NumberofStudent<<"個BufengZhang"<<endl;
  //使用STL通用算法find()在list中查找對象
AIStudentNameIterator=find(AIStudentName.begin(),AIStudentName.end(),"BufengZhang");
if(AIStudentNameIterator==AIStudentName.end())
 cout<<"AIStudentName中沒有BufengZhang"<<endl;
else
 cout<<"在AIStudentName中可以找到BufengZhang"<<endl;
//使用STL通用算法find_if在list中查找對象
AIStudentNameIterator=find_if(AIStudentName.begin(),AIStudentName.end(),IsBufeng());
if(AIStudentNameIterator==AIStudentName.end())
 cout<<"AIStudentName中沒有BufengZhang"<<endl;
else
 cout<<"在AIStudentName中可以找到BufengZhang"<<endl;
//使用STL通用算法search在list中找一個序列
  AIStudentNameIterator = search(AIStudentName.begin(),AIStudentName.end(),TargetAIStudentName.begin(),TargetAIStudentName.end());
  if(AIStudentNameIterator==AIStudentName.end())
 cout<<"AIStudentName中沒有找到BufengZhang And YangZhang"<<endl;
else
 cout<<"在AIStudentName中可以找到BufengZhang And YangZhang"<<endl;
//使用list的成員函數sort()排序一個list
AIStudentName.sort();
cout<<"排序如下:"<<endl;
for_each(AIStudentName.begin(),AIStudentName.end(),PrintString);
//使用list的成員函數刪除元素(pop_front,pop_back,erase(),remove())
  AIStudentName.pop_front();
AIStudentName.pop_back();
AIStudentName.erase(AIStudentName.begin());
AIStudentName.remove("XiaoyuanCui");
cout<<"剩余成員如下:"<<endl;
for_each(AIStudentName.begin(),AIStudentName.end(),PrintString);
//使用STL通用算法remove()從list中刪除元素
  AIStudentNameIterator=remove(AIStudentName.begin(),AIStudentName.end(),"YangZhang");
cout<<"remove後剩余成員如下:"<<endl;
for_each(AIStudentName.begin(),AIStudentNameIterator,PrintString);

}
PrintString(const string& StringToPrint)
{
cout<<StringToPrint<<endl;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved