5、指向类模板成员函数的指针
其实函数对象就是提供了operator()重载的对象。C++标准中定义了指向类成员函数的指针。下面举一个例子:
#include "iostream"
using namespace std;
template
class A
{
public:
T f(T t){return t;} //定义模板类的成员函数
};
template
struct A_traits
{
typedef T (A::*f_ptr)(T); //定义指向A 类的成员函数的指针类型
};
int
main(void)
{
A a;
A_traits::f_ptr fp;
fp = &A::f; //指向具体对象的成员函数
cout<<(a.*fp)(3); //调用该成员函数
}
6、list
#include "iostream"
#include "list"
#include "numeric"
using namespace std;
void print(const list &lst)
{
list::const_iterator p;
for(p=lst.begin();p!=lst.end();++p)
cout<<*p<<' ';
cout<
}
int main()
{
double w[4]={0.8,0.9,88,-99.99};
list z;
for(int i=0;i<4;i++)
z.push_front(w[i]);
print(z);
z.sort();
print(z);
cout<<"sum is"
<
<
}
7、输入流迭代器(istream_iterator)
#include "iostream"
#include "vector"
#include "string"
#include "iterator"
using namespace std;
int main()
{
vector d(5);
int i,sum;
istream_iterator in(cin);
ostream_iterator out(cout," ");
cout<<"enter 5 number:";
sum=d[0]=*in;
for(i=1;i<5;i++){
d[i]=*++in;
sum+=d[i];
}
for(i=0;i<5;i++)
*out=d[i];
cout<<"sum="<<
}
8、反向迭代器
#include "iostream"
#include "vector"
#include "iterator"
using namespace std;
template
void print(ForwIter first,ForwIter last,const char* title)
{
cout<<
while(first!=last)
cout<<*first++<<' ';
cout<
}
int main()
{
int data[3]={9,10,11};
vector d(data,data+3);
vector::reverse_iterator p=d.rbegin();
print(d.begin(),d.end(),"Original");
print(p,d.rend(),"Reverse");
return 1;
}
9、copy
#include "iostream"
#include "vector"
#include "iterator"
#include "algorithm"
#include "string"
using namespace std;
int main()
{
string first_names[5]={"1","2","3","4","5"};
string last_names[5]={"6","7","8","9","10"};
vector names(first_names,first_names+5);
vector names2(10);
vector::iterator p;
copy(last_names,last_names+5,names2.begin());
for(p=names2.begin();p!=names2.end();p++)
cout<<*p<<" ";
copy(names.begin(),names.end(),names2.begin()+5);
for(p=names2.begin();p!=names2.end();p++)
cout<<*p<<" ";
reverse(names2.begin(),names2.end());
for(p=names2.begin();p!=names2.end();p++)
cout<<*p<<" ";
return 1;
}
+++++++++++++++++++++
template
OutputIter copy(InputIter b1,InputIter,e1,OutputIter b2);
这是从元素b1到e1的复制算法,这个副本放到b2的开始处。返回的位置是这一副本的结尾。
template
BidiIter2 copy_backward(BidiIter1 b1,BidiIter1,e1,BidiIter2 b2);
这是从元素b1到e1的复制算法,这个副本放到b2的开始处。复制从e1向后进行到b2,b2也可以向后进行。返回的位置是b2-(e1-b1)。
template
void reverse(BidiIter b,BidiIter e);
将b到e范围内的元素的位置反向
template
void reverse_copy(BidiIter b1,BidiIter e1,OutputIter b2);
这是从元素b1到e1的逆向复制的算法。逆向复制的内容从b2处开始。复制从b1开始向后进行,写入b2,b2也是向后进行,返回位置是b2+(e1-b1)
template
FwdIt unique(FwdIt b, FwdIt e);
清除b到e的相邻元素。返回位置是所得到范围的末端。
template
FwdIt unique(FwdIt b, FwdIt e, Pred pr);
清除从b到e的相邻元素中满足二元排列pr,返回位置是所得范围的末端。
10、数值算法,accumulate
#include "iostream"
#include "numeric"
using namespace std;
int main()
{
double v1[3]={1.0,2.5,4.6},
v2[3]={1.0,2.0,-3.5};
double sum,inner_p;
sum=accumulate(v1,v1+3,0.0);
inner_p=inner_product(v1,v1+3,v2,0.0);
cout<<"sum="
<
<<",product="
<<
return 1;
}
+++++++++++++++
#include
#include "numeric"
using namespace std;
int main()
{
double v1[3]={1.0,2.5,3.6},sum;
sum=accumulate(v1,v1+3,0.0,minus());
cout<<"map ="
<<
return 1;
}
11、transform,bind2nd
#include
#include "functional"
#include "string"
#include "algorithm"
using namespace std;
template
void print(ForwIter first,ForwIter last,const char* title)
{
cout<<
while(first!=last)
cout<<*first++<<' ';
cout<
}
int main()
{
int data[3]={9,10,11};
print(data,data+3,"Original");
transform(data,data+3,data,bind2nd(multiplies(),2));
print(data,data+3,"New");
return 1;
}
说明:
template
OutIt transform(InIt first, InIt last, OutIt x, Unop uop);
The first template function evaluates *(x + N) = uop(*(first + N)) once for each N in the range [0, last - first). It then returns x + (last - first). The call uop(*(first + N)) must not alter *(first + N).
template
class binder2nd
: public unary_function
Pred::result_type> {
public:
binder2nd(const Pred& pr, const Pred::second_argument_type y);
result_type operator()(const argument_type& x) const;
protected:
Pred op;
Pred::second_argument_type value;
};