#include<iostream>
using namespace std;
/*数组作为函数参数传递的处理办法,数组长度或哨兵*/
template<typename type>
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<typename type>
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<<endl;
result=find(arr,arr+5,3);
cout<<*result<<endl;
return 0;
}
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<functional>
using namespace std;
template<typename type1,typename type2>
void print(const type1 & v,ostream_iterator<type2> &os)
{
copy(v.begin(),v.end(),os);
cout<<endl;
}
template<typename type>
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<typename type>
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<typename type>
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<typename type>
bool less_than(type v1,type v2)
{
return v1<v2?true:false;
}
/*标准程序库预先定义好许多函数对象*/
/*所谓函数对象,是某类class实体对象,这类class对function call运算符进行重载操作
如此一来,可把函数对象当成一般函数使用*/
/*函数对象实现本来可以用独立函数加以定义的事物,又何必如此呢?
主要是为了“效率”,我们可以领call运算符成为inline,因而可以消除“通过函数指针来调用函数”时所付出的额外代价*/
template<typename type1,typename type2>
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<typename type1,typename type2>
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<typename InputIterator,typename OutputIterator,typename Elemtype,typename Comp>
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<int> v(arr,arr+arr_size);
ostream_iterator<int> out(cout," ");
print(v,out);
vector<int> t;
t=filter(v,10,less<int>());
print(t,out);
vector<int> ivec(arr_size);
filter_2(v.begin(),v.end(),ivec.begin(),10,less<int>());
print(ivec,out);
t.clear();
t=filter_1(v,10,less<int>());
print(t,out);
sort(v.begin(),v.end(),less<int>());/*调用标准函数对象*/
print(v,out);
sort(v.begin(),v.end(),greater<int>());
print(v,out);
t.clear();
t=less_than_10(v);
print(t,out);
t.clear();
t=filter(v,10,less_than);/*函数指针*/
cout<<count_occurs(v,7)<<endl;
return 0;
}
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<iterator>
using namespace std;
int main(){
map<string,int> words;/*内置类型int被默认初始化为0*/
string word;
while(cin>>word)
{
words[word]++;
}
map<string,int>::const_iterator it=words.begin();
while(it!=words.end())
{
cout<<it->first<<"\t"<<it->second<<endl;
it++;
}
cin.clear();
cout<<"what you want to search:";
string val;
cin>>val;
/*有三种查询方法*/
/*第2种*/
//it=words.find(val);
//if(it!=words.end())
// cout<<it->first<<'\t'<<it->second<<endl;
/*第3种*/
int count=0;
count=words.count(val);
if(count)
cout<<val<<"occurs "<<words[val]<<" times!"<<endl;
return 0;
}
/*迭代器适配器*/
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;
int main(){
ostream_iterator<int> os(cout," ");
int arr[5]={1,3,2,2,4};
vector<int> v(5);/*未指定大小,产生运行时错误*/
copy(arr,arr+5,v.begin());
copy(v.begin(),v.end(),os);
cout<<endl;
vector<int> t;
copy(arr,arr+5,back_inserter(t));/*使用迭代器适配器 back_inserter(),不用担心容器长度问题*/
copy(t.begin(),t.end(),os);
cout<<endl;
vector<int> t2;
copy(arr,arr+5,inserter(t2,t2.end()));/*使用迭代器适配器inserter(),指定容器与插入位置*/
copy(t2.begin(),t2.end(),os);
cout<<endl;
//vector<int> t3;/*vector容器不能在前端插入元素*/
list<int> t3;
copy(arr,arr+5,front_inserter(t3));/*使用front_inserter迭代器适配器,在指定容器前端插入元素*/
copy(t3.begin(),t3.end(),os);
cout<<endl;
return 0;
}
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
#include<iterator>
using namespace std;
int main(){
vector<int> ivec;
/*常规的输入方法*/
//int data;
/*while(cin>>data)
{
ivec.push_back(data);
}*/
istream_iterator<int> is(cin);
istream_iterator<int> eof;
copy(is,eof,back_inserter(ivec));
sort(ivec.begin(),ivec.end(),greater<int>());
ostream_iterator<int> os(cout," ");
copy(ivec.begin(),ivec.end(),os);
cout<<endl;
return 0;
}
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
#include<iterator>
#include<fstream>
#include<string>
using namespace std;
int main(){
ifstream infile("1.txt");
ofstream outfile("2.txt");
if(!infile || !outfile)
{
cerr<<"文件打开失败!"<<endl;
return -1;
}
istream_iterator<string> is(infile);
istream_iterator<string> eof;
vector<string> text;
copy(is,eof,back_inserter(text));
ostream_iterator<string> os(outfile," ");
sort(text.begin(),text.end());
copy(text.begin(),text.end(),os);
return 0;
}
#include<iostream>
#include<vector>
#include<fstream>
#include<algorithm>
#include<string>
#include<map>
#include<set>
using namespace std;
/*包含单词排除集的简单单词统计程序*/
/*具有简单的去除标点符号的功能*/
bool isPunctuate(const char & c)
{
bool flag;
switch (c)
{
case ',':
case ';':
case '.':
case '?':
case '"':
case '!':
flag=1;
break;
default:
flag=0;
break;
}
return flag;
}
int main(){
ifstream infile("1.txt");
if(!infile)
{
cerr<<"文件打开错误!"<<endl;
return -1;
}
map<string,int> words;
string word;
set<string> exclude;
string exc[]={"a","an","or","the","and","but"};
exclude.insert(exc,exc+sizeof(exc)/sizeof(string));
while(infile>>word)
{
if(exclude.count(word))/*存在排除单词集里的单词*/
continue;
if(isPunctuate(word[0]))
{
string s(word,1,word.size());
word=s;
}
while(isPunctuate(word[word.size()-1]))
{
string str(word,0,word.size()-1);
word=str;
}
words[word]++;
}
cout<<"input what you want to search:";
string w;
cin>>w;
if(words.count(w))
cout<<w<<" existed!"<<endl;
else
cout<<w<<" is not in the map!"<<endl;
cout<<"*+*+*+*+*+*+*+*+*+*+*+*"<<endl;
map<string,int>::const_iterator it=words.begin();
ofstream outfile("2.txt");
while(it!=words.end())
{
outfile<<it->first<<"\t"<<it->second<<endl;
it++;
}
infile.close();
outfile.close();
return 0;
}
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
void readFile(vector<string> &words,ifstream &infile)
{
istream_iterator<string> is(infile);
istream_iterator<string> eof;
copy(is,eof,back_inserter(words));
}
class caseStringLength
{
public:
bool operator()(const string &str1,const string &str2)
{
return str1.size()<str2.size();
}
};
void writeFile(const vector<string> &words,ofstream &outfile)
{
ostream_iterator<string> os(outfile," ");
copy(words.begin(),words.end(),os);
}
int main(){
ifstream infile("3.txt");
if(!infile)
{
cerr<<"文件打开失败!"<<endl;
return -1;
}
vector<string> words;
readFile(words,infile);
sort(words.begin(),words.end(),caseStringLength());
ofstream outfile("4.txt");
writeFile(words,outfile);
infile.close();
outfile.close();
return 0;
}
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
/*本程序针对中文名字,且名字内部无空格,名字之间空格隔开*/
void makeMap(map<string, vector<string> > & names, ifstream &infile)
{
string name;
map<string,vector<string> >::iterator it;
while(infile>>name)
{
string lastName(name,0,2);/*姓*/
string firstName(name,2,name.size()-1);/*名*/
if( (it=names.find(lastName) ) !=names.end())
{
it->second.push_back(firstName);
}
else
{
pair<string, vector<string>> t;
t.first=lastName;
t.second.push_back(firstName);
names.insert(it,t);
}
}
}
void printMap(const map<string,vector<string> >& names,ofstream &os)
{
map<string,vector<string> >::const_iterator it=names.begin();
while(it!=names.end())
{
os<<it->first<<"\t*";
vector<string>::const_iterator iter=it->second.begin();
while(iter!=it->second.end())
{
os<<"|"<<*iter;
iter++;
}
os<<endl;
it++;
}
}
void searchByLastName(const map<string,vector<string> > &names, const string &name)
{
map<string,vector<string> >::const_iterator it=names.begin();
if((it=names.find(name))!=names.end())
{
cout<<it->first<<"存在以下名字:"<<endl;
cout<<"------------------------"<<endl;
ostream_iterator<string> os(cout,"\n");
copy(it->second.begin(),it->second.end(),os);
}
else
cout<<name<<"不存在,sorry!"<<endl;
}
int main(){
ifstream infile("5.txt");
map<string,vector<string> > names;
makeMap(names,infile);
ofstream outfile("6.txt");
printMap(names,outfile);
cout<<"input what you want to search:";
string word;
cin>>word;
searchByLastName(names,word);
return 0;
}
#include<iostream>
#include<iterator>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
void inputData(vector<int> &data)
{
istream_iterator<int> is(cin);
istream_iterator<int> eof;
copy(is,eof,back_inserter(data));
}
void doData(const vector<int> &data, ofstream &infile1,ofstream &infile2)
{
vector<int>::const_iterator it=data.begin();
ostream_iterator<int> os1(infile1," ");
ostream_iterator<int> os2(infile2,"\n");
vector<int> v1;
vector<int> 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<int> datas;
inputData(datas);
ofstream infile1("7.txt");
ofstream infile2("8.txt");
doData(datas,infile1,infile2);
infile1.close();
infile2.close();
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
class even_elem{
public:
bool operator()(int elem)
{
return (elem%2)?true:false;
}
};
void Partition(vector<int> datas)
{
vector<int>::iterator division=partition(datas.begin(),datas.end(),even_elem());
ostream_iterator<int> os(cout," ");
copy(datas.begin(),datas.end(),os);
cout<<endl;
copy(datas.begin(),division,os);
cout<<endl;
copy(division,datas.end(),os);
cout<<endl;
}
void Stable_partition(vector<int> datas)
{
vector<int>::iterator division=stable_partition(datas.begin(),datas.end(),even_elem());
ostream_iterator<int> os(cout," ");
copy(datas.begin(),datas.end(),os);
cout<<endl;
copy(datas.begin(),division,os);
cout<<endl;
copy(division,datas.end(),os);
cout<<endl;
}
int main(){
int arr[]={29,23,20,22,17,15,26,51,19,12,35,40};
vector<int> datas(arr,arr+sizeof(arr)/sizeof(int));
Partition(datas);
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n";
Stable_partition(datas);
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
#include<fstream>
using namespace std;
class even_elem{
public:
bool operator()(int elem)
{
return (elem%2)?true:false;
}
};
void Stable_partition(vector<int> datas)
{
ofstream odd_out("7.txt");
ofstream even_out("8.txt");
if(!odd_out || !even_out)
{
cerr<<"文件打开失败!"<<endl;
exit(-1);
}
ostream_iterator<int> os_odd(odd_out," ");
ostream_iterator<int> os_even(even_out,"\n");
vector<int>::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<int> datas(arr,arr+sizeof(arr)/sizeof(int));
Stable_partition(datas);
return 0;
}
#include<iostream>
using namespace std;
class Matrix{
public:
Matrix(int r,int c):_row(r),_col(c)
{
_pmat=new double[r*c];
cout<<"构造函数"<<endl;
}
Matrix(const Matrix &rhs);
const Matrix & operator=(const Matrix &rhs);
~Matrix()
{
delete []_pmat;
cout<<"析构函数"<<endl;
}
private:
double *_pmat;
int _row,_col;
};
Matrix::Matrix(const Matrix &rhs)
{
*this=rhs;
cout<<"拷贝构造函数"<<endl;
}
const Matrix & Matrix::operator=(const Matrix &rhs)
{
if(this!=&rhs)
{
_row=rhs._row;
_col=rhs._col;
_pmat=new double[_row*_col];
for(int i=0;i!=_row*_col;i++)
_pmat[i]=rhs._pmat[i];
}
cout<<"赋值函数"<<endl;
return *this;
}
int main(){
Matrix m1(4,4);
Matrix m2=m1;/*浅拷贝,指针指向同一内存地址空间*/
return 0;
}
#include<iostream>
using namespace std;
class val_class
{
public:
val_class & val(const int & data )
{
cout<<"no-const:"<<data<<endl;
return *this;/*const对象无法调用,因为常量对象增加一个非常量引用!*/
}
// const val_class &val(const int & data)const
// {
// cout<<"const:"<<data<<endl;
// return *this;/*指向对象不可更改*/
// }
};
int main(){
val_class v1;
v1.val(1).val(2);
const val_class v2;
v2.val(3).val(4);/*const对象不能调用类的非const成员*/
return 0;
}