《C++标准程序库》读书笔记(四)
1,
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
list<int>::iterator pos25,pos35,pos;
for(int i=20;i<=40;++i)
coll.push_back(i);
pos25 = find(coll.begin(),coll.end(),25);
pos35 = find(coll.begin(),pos25,35);
if(pos35!=pos25)
{//pos35在pos25前
pos = find(coll.begin(),pos25,30);
}
else
{//pos25在pos35前
pos = find(pos25,coll.end(),30);
}
cout<<"num: "<<*pos<<endl;
system("pause");
return 0;
}
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
list<int>::iterator pos25,pos35,pos;
for(int i=20;i<=40;++i)
coll.push_back(i);
pos25 = find(coll.begin(),coll.end(),25);
pos35 = find(coll.begin(),pos25,35);
if(pos35!=pos25)
{//pos35在pos25前
pos = find(coll.begin(),pos25,30);
}
else
{//pos25在pos35前
pos = find(pos25,coll.end(),30);
}
cout<<"num: "<<*pos<<endl;
system("pause");
return 0;
}
使用仿函数
#include <functional>
/* class for the compose_f_gx_hx adapter*/
template <class OP1, class OP2, class OP3>
class compose_f_gx_hx_t
: public std::unary_function<typename OP2::argument_type,
typename OP1::result_type>
{
private:
OP1 op1; // process: op1(op2(x),op3(x))
OP2 op2;
OP3 op3;
public:
// constructor
compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
: op1(o1), op2(o2), op3(o3) {
}
// function call
typename OP1::result_type
operator()(const typename OP2::argument_type& x) const
{
return op1(op2(x),op3(x));
}
};
/* convenience function for the compose_f_gx_hx adapter*/
template <class OP1, class OP2, class OP3>
inline compose_f_gx_hx_t<OP1,OP2,OP3>
compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3)
{
return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
}
/* class for the compose_f_gx_hx adapter*/
template <class OP1, class OP2, class OP3>
class compose_f_gx_hx_t
: public std::unary_function<typename OP2::argument_type,
typename OP1::result_type>
{
private:
OP1 op1; // process: op1(op2(x),op3(x))
OP2 op2;
OP3 op3;
public:
// constructor
compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
: op1(o1), op2(o2), op3(o3) {
}
// function call
typename OP1::result_type
operator()(const typename OP2::argument_type& x) const
{
return op1(op2(x),op3(x));
}
};
/* convenience function for the compose_f_gx_hx adapter*/
template <class OP1, class OP2, class OP3>
inline compose_f_gx_hx_t<OP1,OP2,OP3>
compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3)
{
return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
}
#include <iostream>
#include <list>
#include <algorithm>
#include <functional>
#include "compose21.hpp"
using namespace std;
int main()
{
list<int> coll;
list<int>::iterator pos;
for(int i=20;i<=40;++i)
coll.push_back(i);
pos = find_if(coll.begin(),coll.end(),
compose_f_gx_hx(logical_or<bool>(),
bind2nd(equal_to<int>(),25),
bind2nd(equal_to<int>(),35)));
cout<<"num: "<<*pos<<endl;
system("pause");
return 0;
}
#include <list>
#include <algorithm>
#include <functional>
#include "compose21.hpp"
using namespace std;
int main()
{
list<int> coll;
list<int>::iterator pos;
for(int i=20;i<=40;++i)
coll.push_back(i);
pos = find_if(coll.begin(),coll.end(),
compose_f_gx_hx(logical_or<bool>(),
bind2nd(equal_to<int>(),25),
bind2nd(equal_to<int>(),35)));
cout<<"num: "<<*pos<<endl;
system("pause");
return 0;
}
2,三种迭代器适配器:
1) Insert iterator 插入位置可以是容器的最前或最后,或是在某一特定位置上.
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll1;
// insert elements from 1 to 9 into the first collection
for (int i=1; i<=9; ++i)
{
coll1.push_back(i);
}
// copy the elements of coll1 into coll2 by appending them
vector<int> coll2;
copy (coll1.begin(), coll1.end(), // source
back_inserter(coll2)); // destination
// copy the elements of coll1 into coll3 by inserting them at the front
// - reverses the order of the elements
deque<int> coll3;
copy (coll1.begin(), coll1.end(), // source
front_inserter(coll3)); // destination
// copy elements of coll1 into coll4
// - only inserter that works for associative collections
set<int> coll4;
copy (coll1.begin(), coll1.end(), // source
inserter(coll4,coll4.begin())); // destination
return 0;
}
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll1;
// insert elements from 1 to 9 into the first collection
for (int i=1; i<=9; ++i)
{
coll1.push_back(i);
}
// copy the elements of coll1 into coll2 by appending them
vector<int> coll2;
copy (coll1.begin(), coll1.end(), // source
back_inserter(coll2)); // destination
// copy the elements of coll1 into coll3 by inserting them at the front
// - reverses the order of the elements
deque<int> coll3;
copy (coll1.begin(), coll1.end(), // source
front_inserter(coll3)); // destination
// copy elements of coll1 into coll4
// - only inserter that works for associative collections
set<int> coll4;
copy (coll1.begin(), coll1.end(), // source
inserter(coll4,coll4.begin())); // destination
return 0;
}
back_inserter的内部调用push_back(),在容器尾端插入元素,只有在提供有push_back()成员函数的容器中才能使用,这样的容器有:vector,deque,list. front_inserter的内部调用push_front(),在容器最前端插入元素,只有在提供有push_ front()成员函数的容器中才能使用,这样的容器有deque和list;一般性的inserter,作用是将元素插入”初始化时接受之第二参数”所指的位置的前方.它内部调用insert().
2)Stream iterator.这是用来读写流的迭代器.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<string> coll;
copy (istream_iterator<string>(cin), // start of source
istream_iterator<string>(), // end of source
back_inserter(coll)); // destination
sort (coll.begin(), coll.end());
unique_copy (coll.begin(), coll.end(), // source
ostream_iterator<string>(cout,"\n")); // destination
}
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<string> coll;
copy (istream_iterator<string>(cin), // start of source
istream_iterator<string>(), // end of source
back_inserter(coll)); // destination
sort (coll.begin(), coll.end());
unique_copy (coll.begin(), coll.end(), // source
ostream_iterator<string>(cout,"\n")); // destination
}
3)Reverse iterator
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i)
{
coll.push_back(i);
}
// print all element in reverse order
copy (coll.rbegin(), coll.rend(), // source
ostream_iterator<int>(cout," ")); // destination
cout << endl;
}
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<int> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i)
{
coll.push_back(i);
}
// print all element in reverse order
copy (coll.rbegin(), coll.rend(), // source
ostream_iterator<int>(cout," ")); // destination
cout << endl;
}
3,移除元素
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
list<int> coll;
// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i)
{
coll.push_front(i);
coll.push_back(i);
}
// print all elements of the collection
copy (coll.begin(), coll.end(),ostream_iterator<int>(cout," "));
cout << endl;
list<int>::iterator end = remove (coll.begin(), coll.end(),3);//新的尾节点
// print resulting elements of the collection
copy (coll.begin(), end,ostream_iterator<int>(cout," "));
cout << endl;
// print number of resulting elements
cout << "number of removed elements: "<< distance(end,coll.end()) << endl;
// remove ``removed'' elements
coll.erase (end, coll.end());
// print all elements of the modified collection
copy (coll.begin(), coll.end(),ostream_iterator<int>(cout," "));
cout << endl;
}
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
list<int> coll;
// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i)
{
coll.push_front(i);
coll.push_back(i);
}
// print all elements of the collection
copy (coll.begin(), coll.end(),ostream_iterator<int>(cout," "));
cout << endl;
list<int>::iterator end = remove (coll.begin(), coll.end(),3);//新的尾节点
// print resulting elements of the collection
copy (coll.begin(), end,ostream_iterator<int>(cout," "));
cout << endl;
// print number of resulting elements
cout << "number of removed elements: "<< distance(end,coll.end()) << endl;
// remove ``removed'' elements
coll.erase (end, coll.end());
// print all elements of the modified collection
copy (coll.begin(), coll.end(),ostream_iterator<int>(cout," "));
cout << endl;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
分类:
C/C++/VC++
posted on 2008-08-27 22:06 Phinecos(洞庭散人) 阅读(1212) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
2007-08-27 OpenGL入门笔记(十五)
2007-08-27 OpenGL入门笔记(十四)
2007-08-27 OpenGL入门笔记(十三)
2006-08-27 操作系统复习笔记(三)