《C++标准程序库》读书笔记(四)

本文深入探讨了C++ STL中迭代器的应用,包括不同类型的迭代器如Insert iterator、Stream iterator和Reverse iterator的功能及使用场景,并介绍了如何利用这些迭代器进行元素查找、插入、删除等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include
<list>
#include
<algorithm>
usingnamespacestd;

intmain()
{
list
<int>coll;
list
<int>::iteratorpos25,pos35,pos;
for(inti=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");
return0;
}

使用仿函数

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<functional>
/*classforthecompose_f_gx_hxadapter*/
template
<classOP1,classOP2,classOP3>
classcompose_f_gx_hx_t
:
publicstd::unary_function<typenameOP2::argument_type,
typenameOP1::result_type
>
{
private:
OP1op1;
//process:op1(op2(x),op3(x))
OP2op2;
OP3op3;
public:
//constructor
compose_f_gx_hx_t(constOP1&o1,constOP2&o2,constOP3&o3)
:op1(o1),op2(o2),op3(o3){
}
//functioncall
typenameOP1::result_type
operator()(consttypenameOP2::argument_type&x)const
{
returnop1(op2(x),op3(x));
}
};
/*conveniencefunctionforthecompose_f_gx_hxadapter*/
template
<classOP1,classOP2,classOP3>
inlinecompose_f_gx_hx_t
<OP1,OP2,OP3>
compose_f_gx_hx(
constOP1&o1,constOP2&o2,constOP3&o3)
{
returncompose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
}

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include
<list>
#include
<algorithm>
#include
<functional>
#include
"compose21.hpp"
usingnamespacestd;

intmain()
{
list
<int>coll;
list
<int>::iteratorpos;
for(inti=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");
return0;
}

2,三种迭代器适配器:

1) Insert iterator 插入位置可以是容器的最前或最后,或是在某一特定位置上.

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include
<vector>
#include
<list>
#include
<deque>
#include
<set>
#include
<algorithm>
usingnamespacestd;

intmain()
{
list
<int>coll1;
//insertelementsfrom1to9intothefirstcollection
for(inti=1;i<=9;++i)
{
coll1.push_back(i);
}
//copytheelementsofcoll1intocoll2byappendingthem
vector<int>coll2;
copy(coll1.begin(),coll1.end(),
//source
back_inserter(coll2));//destination
//copytheelementsofcoll1intocoll3byinsertingthematthefront
//-reversestheorderoftheelements
deque<int>coll3;
copy(coll1.begin(),coll1.end(),
//source
front_inserter(coll3));//destination
//copyelementsofcoll1intocoll4
//-onlyinserterthatworksforassociativecollections
set<int>coll4;
copy(coll1.begin(),coll1.end(),
//source
inserter(coll4,coll4.begin()));//destination
return0;
}

back_inserter的内部调用push_back(),在容器尾端插入元素,只有在提供有push_back()成员函数的容器中才能使用,这样的容器有:vector,deque,list. front_inserter的内部调用push_front(),在容器最前端插入元素,只有在提供有push_ front()成员函数的容器中才能使用,这样的容器有dequelist;一般性的inserter,作用是将元素插入初始化时接受之第二参数所指的位置的前方.它内部调用insert().

2)Stream iterator.这是用来读写流的迭代器.

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include
<vector>
#include
<string>
#include
<algorithm>
#include
<iterator>
usingnamespacestd;
intmain()
{
vector
<string>coll;
copy(istream_iterator
<string>(cin),//startofsource
istream_iterator<string>(),//endofsource
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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include
<vector>
#include
<algorithm>
#include
<iterator>
usingnamespacestd;

intmain()
{
vector
<int>coll;
//insertelementsfrom1to9
for(inti=1;i<=9;++i)
{
coll.push_back(i);
}
//printallelementinreverseorder
copy(coll.rbegin(),coll.rend(),//source
ostream_iterator<int>(cout,""));//destination
cout<<endl;
}

3,移除元素

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include
<list>
#include
<algorithm>
#include
<iterator>
usingnamespacestd;
intmain()
{
list
<int>coll;
//insertelementsfrom6to1and1to6
for(inti=1;i<=6;++i)
{
coll.push_front(i);
coll.push_back(i);
}
//printallelementsofthecollection
copy(coll.begin(),coll.end(),ostream_iterator<int>(cout,""));
cout
<<endl;
list
<int>::iteratorend=remove(coll.begin(),coll.end(),3);//新的尾节点
//printresultingelementsofthecollection
copy(coll.begin(),end,ostream_iterator<int>(cout,""));
cout
<<endl;
//printnumberofresultingelements
cout<<"numberofremovedelements:"<<distance(end,coll.end())<<endl;
//remove``removed''elements
coll.erase(end,coll.end());
//printallelementsofthemodifiedcollection
copy(coll.begin(),coll.end(),ostream_iterator<int>(cout,""));
cout
<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值