#include
using namespace std;
/*数组作为函数参数传递的处理办法,数组长度或哨兵*/
template
type *find(type *arr,int size,const type & value)
{
for(int i=0;i!=size;i++)
if(arr[i]==value)
return &arr[i];
return 0;
}
template
type *find(type *arr,type * sentinel,const type & value)
{
while(arr!=sentinel)
{
if(*arr==value)
return arr;
arr++;
}
return 0;
}
int main(){
int arr[5]={1,2,3,4,5};
int *result=find(arr,5,2);
cout<<*result<
#include
#include
#include
#include
#include
using namespace std;
template
void print(const type1 & v,ostream_iterator &os)
{
copy(v.begin(),v.end(),os);
cout<
type less_than_10(const type & v)
{
type t;
size_t i=0;
while(i!=v.size())
{
if(v[i]<=10)
t.push_back(v[i]);
i++;
}
return t;
}
template
type filter(const type &v, typename type::value_type filter_value, bool (*pred)(typename type::value_type,typename type::value_type))/*函数指针*/
{
type t;
size_t i=0;
while(i!=v.size())
{
if(pred(v[i],filter_value))
t.push_back(v[i]);
i++;
}
return t;
}
template
int count_occurs(const type &v,typename type::value_type val)
{
typename type::const_iterator it=v.begin();/*注意迭代器类型*/
int occurs=0;
while((it=find(it,v.end(),val))!=v.end())
{
occurs++;
it++;
}
return occurs;
}
template
bool less_than(type v1,type v2)
{
return v1
type1 filter(const type1 & v,typename type1::value_type val,type2 functionobject)
{
type1 t;
typename type1::const_iterator it=v.begin();
while(it!=v.end())
{
if(functionobject(*it,val))
t.push_back(*it);
it++;
}
return t;
}
/*绑定适配器*/
/*将函数对象的参数绑定到某特定值身上,使二元函数对象转化为一元函数对象*/
/*标准库提供了两个绑定适配器,
bind1st会将指定值绑定到第一个操作数
bind2nd会将指定值绑定到第二个操作数*/
template
type1 filter_1(const type1 & v,typename type1::value_type val,type2 function_object)
{
type1 t;
typename type1::const_iterator it=v.begin();
while((it=find_if(it,v.end(),bind2nd(function_object,val)))!=v.end())
{
t.push_back(*it);
it++;
}
return t;
}
/*传迭代器*/
template
OutputIterator filter_2(InputIterator first,InputIterator last,OutputIterator at,const Elemtype &val,Comp pred)
{
while((first=find_if(first,last,bind2nd(pred,val)))!=last)
{
*at++=*first++;
}
return at;
}
int main(){
int arr[]={2,5,3,7,11,13,4,7,16,9};
const size_t arr_size=sizeof(arr)/sizeof(int);
vector v(arr,arr+arr_size);
ostream_iterator out(cout," ");
print(v,out);
vector t;
t=filter(v,10,less());
print(t,out);
vector ivec(arr_size);
filter_2(v.begin(),v.end(),ivec.begin(),10,less());
print(ivec,out);
t.clear();
t=filter_1(v,10,less());
print(t,out);
sort(v.begin(),v.end(),less());/*调用标准函数对象*/
print(v,out);
sort(v.begin(),v.end(),greater());
print(v,out);
t.clear();
t=less_than_10(v);
print(t,out);
t.clear();
t=filter(v,10,less_than);/*函数指针*/
cout<
#include
#include
/*迭代器适配器*/
#include
#include
#include
#include
#include
using namespace std;
int main(){
ostream_iterator os(cout," ");
int arr[5]={1,3,2,2,4};
vector v(5);/*未指定大小,产生运行时错误*/
copy(arr,arr+5,v.begin());
copy(v.begin(),v.end(),os);
cout< t;
copy(arr,arr+5,back_inserter(t));/*使用迭代器适配器 back_inserter(),不用担心容器长度问题*/
copy(t.begin(),t.end(),os);
cout< t2;
copy(arr,arr+5,inserter(t2,t2.end()));/*使用迭代器适配器inserter(),指定容器与插入位置*/
copy(t2.begin(),t2.end(),os);
cout< t3;/*vector容器不能在前端插入元素*/
list t3;
copy(arr,arr+5,front_inserter(t3));/*使用front_inserter迭代器适配器,在指定容器前端插入元素*/
copy(t3.begin(),t3.end(),os);
cout<
#include
#include
#include
#include
#include
using namespace std;
int main(){
vector ivec;
/*常规的输入方法*/
//int data;
/*while(cin>>data)
{
ivec.push_back(data);
}*/
istream_iterator is(cin);
istream_iterator eof;
copy(is,eof,back_inserter(ivec));
sort(ivec.begin(),ivec.end(),greater());
ostream_iterator os(cout," ");
copy(ivec.begin(),ivec.end(),os);
cout<
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main(){
ifstream infile("1.txt");
ofstream outfile("2.txt");
if(!infile || !outfile)
{
cerr<<"文件打开失败!"< is(infile);
istream_iterator eof;
vector text;
copy(is,eof,back_inserter(text));
ostream_iterator os(outfile," ");
sort(text.begin(),text.end());
copy(text.begin(),text.end(),os);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void readFile(vector &words,ifstream &infile)
{
istream_iterator is(infile);
istream_iterator eof;
copy(is,eof,back_inserter(words));
}
class caseStringLength
{
public:
bool operator()(const string &str1,const string &str2)
{
return str1.size() &words,ofstream &outfile)
{
ostream_iterator os(outfile," ");
copy(words.begin(),words.end(),os);
}
int main(){
ifstream infile("3.txt");
if(!infile)
{
cerr<<"文件打开失败!"< words;
readFile(words,infile);
sort(words.begin(),words.end(),caseStringLength());
ofstream outfile("4.txt");
writeFile(words,outfile);
infile.close();
outfile.close();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void inputData(vector &data)
{
istream_iterator is(cin);
istream_iterator eof;
copy(is,eof,back_inserter(data));
}
void doData(const vector &data, ofstream &infile1,ofstream &infile2)
{
vector::const_iterator it=data.begin();
ostream_iterator os1(infile1," ");
ostream_iterator os2(infile2,"\n");
vector v1;
vector v2;
while(it!=data.end())
{
if(*it%2==0)
v1.push_back(*it);
else
v2.push_back(*it);
it++;
}
copy(v1.begin(),v1.end(),os1);
copy(v2.begin(),v2.end(),os2);
}
int main(){
vector datas;
inputData(datas);
ofstream infile1("7.txt");
ofstream infile2("8.txt");
doData(datas,infile1,infile2);
infile1.close();
infile2.close();
return 0;
}
#include
#include
#include
#include
using namespace std;
class even_elem{
public:
bool operator()(int elem)
{
return (elem%2)?true:false;
}
};
void Partition(vector datas)
{
vector::iterator division=partition(datas.begin(),datas.end(),even_elem());
ostream_iterator os(cout," ");
copy(datas.begin(),datas.end(),os);
cout< datas)
{
vector::iterator division=stable_partition(datas.begin(),datas.end(),even_elem());
ostream_iterator os(cout," ");
copy(datas.begin(),datas.end(),os);
cout< datas(arr,arr+sizeof(arr)/sizeof(int));
Partition(datas);
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n";
Stable_partition(datas);
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
class even_elem{
public:
bool operator()(int elem)
{
return (elem%2)?true:false;
}
};
void Stable_partition(vector datas)
{
ofstream odd_out("7.txt");
ofstream even_out("8.txt");
if(!odd_out || !even_out)
{
cerr<<"文件打开失败!"< os_odd(odd_out," ");
ostream_iterator os_even(even_out,"\n");
vector::iterator division=stable_partition(datas.begin(),datas.end(),even_elem());
copy(datas.begin(),division,os_even);
copy(division,datas.end(),os_odd);
}
int main(){
int arr[]={29,23,20,22,17,15,26,51,19,12,35,40};
vector datas(arr,arr+sizeof(arr)/sizeof(int));
Stable_partition(datas);
return 0;
}
#include
using namespace std;
class Matrix{
public:
Matrix(int r,int c):_row(r),_col(c)
{
_pmat=new double[r*c];
cout<<"构造函数"<
#include
using namespace std;
class val_class
{
public:
val_class & val(const int & data )
{
cout<<"no-const:"<