STL之 - list疯狂练习了一把
2008-01-22 19:21
330 查看
网上曰:我曾用STL写过几个商业程序。它在很多方面减少了我的工作量,也排除了很多逻辑错误。
最大的一个程序有大约5000行。可能最惊人的事情就是它的速度。它读入并处理一个1-2兆的报告文件仅花大约20秒。我是在linux上用gcc2.7.2开发的,现在运行在HP-UX机器上。它一共用了大约50和函数对象和很多容器,这些容器的大小从小list到一个有14,000个元素的map都有。
一个程序中的函数对象是处于一个继承树中,顶层的函数对象调用低层的函数对象。我大量的使用STL算法for_each() ,find(),find_if(),count()和count_if(),我尽量减少使用程序内部的函数,而使用STL的算法调用。
STL倾向于自动的把代码组织成清晰的控制和支持模块。通过小心使用函数对象并给它们起有意义的名字,我使它们在我的软件的控制流中流动。
所以,今天疯狂练习了STL的基本。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <vector>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <algorithm>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* for count_if */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class IsAToothbrush
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IsAToothbrush(string& ToothbrushCode):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
ValToothbrushCode(ToothbrushCode)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool operator() (string& SalesRecord)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return SalesRecord.substr(0, 4) == ValToothbrushCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ValToothbrushCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* for find_if */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class findListMember
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
findListMember(string& findListString):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
valFindListString(findListString)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
bool operator()(string& strFindList) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return strFindList.at(4) > valFindListString.at(4);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string valFindListString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void print_it(string& string_to_printer)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << string_to_printer << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int count_int;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int print_count(int inter_val)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (inter_val == 2) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count_int++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return count_int;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> str_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_back("A1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_back("A2");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_back("A3");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_front("A4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_front("A5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string>::iterator iter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (iter = str_list.begin(); iter != str_list.end(); iter++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *iter << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.reverse();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string>::iterator iter_;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (iter_ = str_list.begin(); iter_ != str_list.end(); iter_++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *iter_ << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!str_list.empty()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "str_list is not empty..." << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (str_list.empty()) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "str_list is empty..." << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* for_each test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(str_list.begin(), str_list.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* count test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int>::iterator int_list_inter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int count_int_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count_int_list = count(int_list.begin(), int_list.end(), 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "The int list count_int_list: " << count_int_list << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* count_if test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> SalesRecords;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0001 Soap");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0002 Shampoo");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0003 Toothbrush");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0004 Toothpaste");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0003 Toothbrush");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ToothbrushCode("0003");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int NumberOfToothbrushes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NumberOfToothbrushes =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count_if(SalesRecords.begin(), SalesRecords.end(), IsAToothbrush(ToothbrushCode));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "There were "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< NumberOfToothbrushes
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< " toothbrushes sold" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* find test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> str_list_find;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list2");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list3");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list6");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string>::iterator find_iter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
find_iter = find(str_list_find.begin(), str_list_find.end(), "list5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (find_iter == str_list_find.end()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found not!!" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found: " << *find_iter << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* find_if test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strMember("list4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
find_iter = find_if(str_list_find.begin(), str_list_find.end(), findListMember(strMember));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (find_iter == str_list_find.end()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found not!!" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found: " << *find_iter << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* search test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_search_list1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_search_list2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_search_list3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list1.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list1.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int>::iterator search_list_iter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search_list_iter =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search(int_search_list1.begin(), int_search_list1.end(),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.begin(), int_search_list2.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (search_list_iter != int_search_list2.end()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "We are found " << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* sort test ...*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.sort();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search_list_iter = int_search_list2.begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (search_list_iter; search_list_iter != int_search_list2.end(); search_list_iter++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *search_list_iter << " - ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* insert() test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.insert(int_search_list3.begin(), -1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.insert(int_search_list3.end(), 5);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search_list_iter = int_search_list3.begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int arrary[2] = ...{ 7, 9 };
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.insert(int_search_list3.end(), &arrary[0], &arrary[1]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (search_list_iter; search_list_iter != int_search_list3.end(); search_list_iter++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *search_list_iter << " - ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* list construct */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector<string> vector_string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("ABC");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("DEF");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("YUI");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("MNP");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("LOI");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("QWE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> list_string(vector_string.begin(), vector_string.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(list_string.begin(), list_string.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* pop_back pop_front erase test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list_string.pop_back();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list_string.pop_front();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list_string.erase(list_string.begin());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "list_string size: " << list_string.size() << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(list_string.begin(), list_string.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* remove test ...*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> remove_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.push_back("OOOO");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.push_back("PPPP");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.push_back("MMMM");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.remove("PPPP");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(remove_list.begin(), remove_list.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
最大的一个程序有大约5000行。可能最惊人的事情就是它的速度。它读入并处理一个1-2兆的报告文件仅花大约20秒。我是在linux上用gcc2.7.2开发的,现在运行在HP-UX机器上。它一共用了大约50和函数对象和很多容器,这些容器的大小从小list到一个有14,000个元素的map都有。
一个程序中的函数对象是处于一个继承树中,顶层的函数对象调用低层的函数对象。我大量的使用STL算法for_each() ,find(),find_if(),count()和count_if(),我尽量减少使用程序内部的函数,而使用STL的算法调用。
STL倾向于自动的把代码组织成清晰的控制和支持模块。通过小心使用函数对象并给它们起有意义的名字,我使它们在我的软件的控制流中流动。
所以,今天疯狂练习了STL的基本。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <string>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <vector>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <algorithm>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* for count_if */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class IsAToothbrush
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IsAToothbrush(string& ToothbrushCode):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
ValToothbrushCode(ToothbrushCode)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool operator() (string& SalesRecord)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return SalesRecord.substr(0, 4) == ValToothbrushCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ValToothbrushCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* for find_if */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class findListMember
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
findListMember(string& findListString):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
valFindListString(findListString)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
bool operator()(string& strFindList) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return strFindList.at(4) > valFindListString.at(4);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string valFindListString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void print_it(string& string_to_printer)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << string_to_printer << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int count_int;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int print_count(int inter_val)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (inter_val == 2) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count_int++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return count_int;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> str_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_back("A1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_back("A2");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_back("A3");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_front("A4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.push_front("A5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string>::iterator iter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (iter = str_list.begin(); iter != str_list.end(); iter++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *iter << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.reverse();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string>::iterator iter_;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (iter_ = str_list.begin(); iter_ != str_list.end(); iter_++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *iter_ << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (!str_list.empty()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "str_list is not empty..." << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list.clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (str_list.empty()) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "str_list is empty..." << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* for_each test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(str_list.begin(), str_list.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* count test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_list.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int>::iterator int_list_inter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int count_int_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count_int_list = count(int_list.begin(), int_list.end(), 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "The int list count_int_list: " << count_int_list << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* count_if test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> SalesRecords;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0001 Soap");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0002 Shampoo");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0003 Toothbrush");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0004 Toothpaste");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SalesRecords.push_back("0003 Toothbrush");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string ToothbrushCode("0003");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int NumberOfToothbrushes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NumberOfToothbrushes =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count_if(SalesRecords.begin(), SalesRecords.end(), IsAToothbrush(ToothbrushCode));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "There were "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< NumberOfToothbrushes
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<< " toothbrushes sold" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* find test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> str_list_find;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list2");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list3");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str_list_find.push_back("list6");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string>::iterator find_iter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
find_iter = find(str_list_find.begin(), str_list_find.end(), "list5");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (find_iter == str_list_find.end()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found not!!" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found: " << *find_iter << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* find_if test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strMember("list4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
find_iter = find_if(str_list_find.begin(), str_list_find.end(), findListMember(strMember));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (find_iter == str_list_find.end()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found not!!" << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "found: " << *find_iter << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* search test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_search_list1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_search_list2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int> int_search_list3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list1.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list1.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.push_back(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<int>::iterator search_list_iter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search_list_iter =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search(int_search_list1.begin(), int_search_list1.end(),
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.begin(), int_search_list2.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (search_list_iter != int_search_list2.end()) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "We are found " << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* sort test ...*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list2.sort();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search_list_iter = int_search_list2.begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (search_list_iter; search_list_iter != int_search_list2.end(); search_list_iter++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *search_list_iter << " - ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* insert() test ... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.insert(int_search_list3.begin(), -1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.insert(int_search_list3.end(), 5);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
search_list_iter = int_search_list3.begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int arrary[2] = ...{ 7, 9 };
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int_search_list3.insert(int_search_list3.end(), &arrary[0], &arrary[1]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (search_list_iter; search_list_iter != int_search_list3.end(); search_list_iter++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << *search_list_iter << " - ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* list construct */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector<string> vector_string;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("ABC");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("DEF");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("YUI");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("MNP");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("LOI");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vector_string.push_back("QWE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> list_string(vector_string.begin(), vector_string.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(list_string.begin(), list_string.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* pop_back pop_front erase test... */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list_string.pop_back();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list_string.pop_front();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list_string.erase(list_string.begin());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cout << "list_string size: " << list_string.size() << endl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(list_string.begin(), list_string.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* remove test ...*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list<string> remove_list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.push_back("OOOO");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.push_back("PPPP");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.push_back("MMMM");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
remove_list.remove("PPPP");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for_each(remove_list.begin(), remove_list.end(), print_it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- STL之 - 排序函数疯狂练习了一把
- STL之List的一些编程练习
- STL之list容器
- STL-list
- 关于List的几个小练习
- C++ STL List
- STL中list的使用(理论)
- STL提供了三个最基本的容器:vector,list,deque
- STL 标准模板库 vector queue list map multimap的使用基础
- STL中list的erase()方法的使用
- STL — list容器用法的详解
- 集合List,SetDemo练习
- STL中的list/set/map等容器clear之后的内存占用
- UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)
- STL中list用法详解
- STL容器之list
- STL<list stack queue 模板类>
- 【数据结构】STL中的vector和list
- 网易2009年校园招聘--一道关于<list>的STL题目
- STL中的list详解