//预定义仿函数
negate();
plus();
multiplies();
divides();
modulus();
equal_to();
not_equal_to();
less();
greater();
less_equal();
greater_equal();
logical_and();
logical_not();
logical_or();
//函数配接器
mem_fun();
mem_fun_ref();
ptr_fun();
bind2nd();
bind1st();
not1();
not2();
//算法
min();
max();
min_element();
max_element();
swap();
iter_swap();
//非变动性算法
for_each(); //对每个元素执行某操作
count(); //返回元素个数
count_if(); //返回满足某一条件的元素个数
min_element(); //返回最小值元素迭代器
max_element(); //返回最大值元素迭代器
find(); //搜寻等于某值的第一个元素,返回该元素迭代器
find_if(); //搜寻满足某个条件的第一个元素,返回该元素迭代器
search_n(); //搜寻具有某特性的第一段"n个连续元素"
search(); //搜寻某个子区间第一次出现位置
find_end(); //搜寻某个子区间最后一次出现位置
find_first_of();//搜寻等于"某数个值之一"的第一个元素
adjacent_find();//搜寻连续两个相等(或说符合特定准则)的元素
equal(); //判断两区间是否相等
mismatch(); //返回两个序列的各组对应元素中,第一队不相等的元素
lexicographical_compare();//判断某一序列在"字典顺序"下是否小于另一序列
//变动性算法 不能用于关联式容器
for_each();
copy(); //从第一个元素开始,复制某段区间
copy_backward();//从最后一个元素开始,复制某段区间
transform(); //变动(并复制)元素,将两个区间的元素合并
merge(); //合并两个区间
swap_ranges(); //交换两区间内的元素
fill(); //以给定值替换每一个元素
fill_n(); //以给定值替换n个元素
generate(); //以某项操作的结果替换每一个元素
generate_n(); //以某项操作的结果替换n个元素
replace(); //将具有某特定值的元素替换为另一个值
replace_if(); //将符合某条件的元素替换为另一个值
replace_copy(); //复制整个区间,同时并将具有某特定值的元素替换为另一值
replace_copy_if();//复制整个区间,同时将符合某条件的元素替换为另一值
//移除性算法 是一种特殊的变序性算法
//只是逻辑上移除,并没有删除元素,
//手段是:将不需要被移除的元素往前覆盖应被移除的元素
remove(); //将等于某特定值的元素全部移除
remove_if(); //将满足某条件的元素全部移除
remove_copy(); //将不等于某特地值的元素全部复制到它处
remove_copy_if();//将不满足某条件的元素全部复制到它处
unique(); //移除毗邻的重复元素
unique_copy(); //移除毗邻的重复元素,并复制到它处
//变序性算法 不能作用于关联式容器
reverse(); //将元素的次序逆转
reverse_copy(); //复制的同时, 逆转元素顺序
rotate(); //旋转元素次序
rotate_copy(); //复制的同事,旋转元素次序
next_permutation();//得到元素的下一个排序次序
prev_permutation();//得到元素的上一个排序次序
random_shuffle(); //将元素的次序随机打乱
partition(); //改变元素次序,使"符合某条件"者移到前面
stable_partition();//与partition()相似,但保持符合准则与不符合准则之各个元素之间的相对位置
//排序算法 排序算法是一种特殊的变序性算法
sort(); //对所有元素排序
stable_sort(); //对所有元素排序,并保持相等元素间的相对次序
partial_sort(); //排序,直到前n个元素就位
partial_sort_copy();//排序,直到前n个元素就位,结果复制于它处
nth_element(); //根据第n个位置进行排序
partition(); //改变元素次序,使符合某条件的元素放在前面
stable_partition();
make_heap(); //将一个区间转换成一个heap
push_heap(); //将元素加入一个heap
pop_heap(); //从heap移除一个元素
sort_heap(); //对heap进行排序(执行后就不再是个heap了)
//下面是写的一些代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
//#include
#include
#include "stl.h"
using namespace std;
#define MAX_SIZE 100
const string ToothbrushCode("0003");
/*模板函数和模板类必须放在头文件
template
const T& max(const T& a,const T& b)
{
return a}*/
class isToolBrush
{
public:
bool operator()(string& SalesRecord)
{
return SalesRecord.substr(0,4) == ToothbrushCode;
}
};
bool IsToolBrush(string& record)
{
return record.substr(0,4)==ToothbrushCode;
}
void printd(string& record)
{
cout << record << endl;
}
/*
template void myprint(type& n)
{
cout << n << endl;
}*/
void test1()
{
list int_list;
int_list.push_front(1);
int_list.push_front(2);
list::iterator iter;
iter = max_element(int_list.begin(),int_list.end());
cout << *iter << endl;
int result = accumulate(int_list.begin(),int_list.end(),0);
reverse(int_list.begin(),int_list.end());
//for_each()
double A[6] = {1.2,1.3,1.4,1.5,1.6,1.7};
reverse(A, A + 6);
for(int i=0; i<6; ++i)
{
cout << "A[" << i << "]" << A[i];
}
int *a = new int[34];
int *b = new int[];
int (*c)[2] = new int[34][2];
int (*d)[2] = new int[][2];
int (*e)[2][3] = new int[34][2][3];
int (*f)[2][3] = new int[][2][3];
a[0] = 1;
// b[0] = 1; //运行时错误,无分配的内存,b只起指针的作用,用来指向相应的数据
c[0][0] = 1;
// d[0][0] = 1;//运行时错误,无分配的内存,d只起指针的作用,用来指向相应的数据
e[0][0][0] = 1;
// f[0][0][0] = 1;//运行时错误,无分配的内存,f只起指针的作用,用来指向相应的数据
cout< cout< cout< cout< cout< cout<
delete[] a;
delete[] b;
delete[] c;
delete[] d;
delete[] e;
delete[] f;
enum CMD
{
quit=0,
add = 1,
search,
sort
};
CMD cmd;
cmd=(CMD)0;
if(0 == quit)
{
cout << "success" << endl;
}
list SalesRecords;
SalesRecords.push_back("0001 Soap");
SalesRecords.push_back("0002 Shampoo");
SalesRecords.push_back("0003 Toothbrush");
SalesRecords.push_back("0004 Toothpaste");
SalesRecords.push_back("0003 Toothbrush");
string abc("123456");
myprint(abc);
for_each(SalesRecords.begin(),SalesRecords.end(),printd);
int NumberOfToothbrushes(0);
try
{
int num = count(SalesRecords.begin(),SalesRecords.end(),ToothbrushCode);
NumberOfToothbrushes = count_if (SalesRecords.begin(), SalesRecords.end(),isToolBrush());
}
catch(exception e)
{
cout << e.what() << endl;
}
catch(...)
{
cout << __FILE__ << __LINE__ << " error" << endl;
}
cout << "There were "
<< NumberOfToothbrushes
<< " toothbrushes sold" << endl;
vector vector1;
vector1.push_back(1);
vector1.push_back(2);
vector::iterator vec_iter = vector1.begin();
cout << *vec_iter << endl;
cout << vec_iter[0] << endl;
cout << vec_iter[1] << endl;
}
class IsAFlag {
public:
bool operator () (string& PossibleFlag) {
return PossibleFlag.substr(0,1)=="-";
}
};
#
class IsAFileName {
public:
bool operator () (string& StringToCheck) {
return !IsAFlag()(StringToCheck);
}
};
#
class IsHelpFlag {
public:
bool operator () (string& PossibleHelpFlag) {
return PossibleHelpFlag=="-h";
}
};
void test2(int argc,char* argv[])
{
list CmdLineParameters; // the command line parameters
list::iterator StartOfFiles; // start of filenames
list Flags; // list of flags
list FileNames; // list of filenames
#
for (int i = 0; i < argc; ++i) CmdLineParameters.push_back(argv[i]);
#
CmdLineParameters.pop_front(); // we don't want the program name
#
// make sure we have the four mandatory file names
int NumberOfFiles(0);
NumberOfFiles = count_if(CmdLineParameters.begin(), CmdLineParameters.end(),
IsAFileName());
#
cout << "The "
<< (NumberOfFiles == 4 ? "correct " : "wrong ")
<< "number ("
<< NumberOfFiles
<< ") of file names were specified" << endl;
#
// move any flags to the beginning
StartOfFiles =
stable_partition(CmdLineParameters.begin(), CmdLineParameters.end(),
IsAFlag());
#
cout << "Command line parameters after stable partition" << endl;
for_each(CmdLineParameters.begin(), CmdLineParameters.end(), printd);
#
// Splice any flags from the original CmdLineParameters list into Flags list.
Flags.splice(Flags.begin(), CmdLineParameters,
CmdLineParameters.begin(), StartOfFiles);
cout << "Command line parameters after splice" << endl;
for_each(CmdLineParameters.begin(), CmdLineParameters.end(), printd);
#
if (!Flags.empty()) {
cout << "Flags specified were:" << endl;
for_each(Flags.begin(), Flags.end(), printd);
}
else {
cout << "No flags were specified" << endl;
}
#
// parameters list now contains only filenames. Splice them into FileNames list.
FileNames.splice(FileNames.begin(), CmdLineParameters,
CmdLineParameters.begin(), CmdLineParameters.end());
#
if (!FileNames.empty()) {
cout << "Files specified (in order) were:" << endl;
for_each(FileNames.begin(), FileNames.end(), printd);
}
else {
cout << "No files were specified" << endl;
}
#
// check if the help flag was specified
if (find_if(Flags.begin(), Flags.end(), IsHelpFlag())!=Flags.end()) {
cout << "The help flag was specified" << endl;
}
#
// open the files and do whatever you do
#
}
typedef struct myst
{
int a;
int b;
myst(){a=0;b=0;}
}MySt;
void printst(const MySt& st)
{
cout << st.a << st.b << endl;
}
void printn(int& n)
{
cout << n << endl;
}
/*
template
void add(int& elem)
{
elem += theValue;
}*/
template
class AddValue
{
private:
T theValue;
public:
AddValue(const T& v):theValue(v){
cout << "AddValue构造函数" << endl;
}
void operator ()(T& elem)const
{
elem += theValue;
}
};
class Nth
{
private:
int count;
int nth;
public:
Nth(int n):nth(n),count(0){}
bool operator()(int)
{
return ++count==nth;
}
};
bool checkn(int elem)
{
if(elem == 4)
{
return true;
}
else
{
return false;
}
}
int main(int argc,char* argv[])
{
//test1();
cout << "sizeof string == " << sizeof(string) << endl;
//char cmd[1024] = {0};
//printf("输入参数\r\n");
//scanf("%s",cmd);
//cout << cmd << endl;
//test2(argc,argv);
list mylist;
MySt st1,st2,st3;
st1.a = 1;
st1.b = 1;
st2.a = 2;
st2.b = 2;
st3.a = 3;
st3.b = 3;
mylist.push_back(st1);
mylist.push_back(st2);
mylist.push_back(st3);
for_each(mylist.begin(),mylist.end(),printst);
stack st;
st.push(1);
st.push(2);
int n = st.size();
union check
{
int a;
char b;
};
check ut;
ut.a = 1;
cout << ut.b << endl;
enum hh
{
CMD_LOGIN=1,
CMD_LOGOUT=2
};
hh eh;
eh = (hh)3;
cout << sizeof(eh) << eh << endl;
int *p = NULL;
int a[3][2] = {(0,1),(2,3),(4,5)};
int d = (0,1);
bitset<32> falgs0;
try
{
char* pch = new char(2);
delete[] pch;
}
catch(exception& e)
{
cout << e.what() << endl;
}
int nsize = mylist.size();
int nd = distance(mylist.begin(),mylist.end());
cout << "size = " << nsize << ";" << "nd = " << nd << endl;
vector vec;
for(int i = 1; i < 11; ++i)
{
vec.push_back(i);
}
for_each(vec.begin(),vec.end(),printn);
cout << endl;
for_each(vec.begin(),vec.end(),AddValue(10));
for_each(vec.begin(),vec.end(),printn);
cout << endl;
//for_each(vec.begin(),vec.end(),add<10>);
//bind2nd(less,20);
vector vec1;
copy(vec.begin(),vec.end(),back_inserter(vec1));
cout << "打印vec1:" << endl;
//for_each(vec1.begin(),vec1.end(),printn);
copy(vec1.begin(),vec1.end(),ostream_iterator(cout,"\n"));
swap(vec1[0],vec1[1]);
swap(vec,vec1);
cout << "打印swap 后vec1:" << endl;
//cout << vec1[0] << vec[1] << endl;
//for_each(vec1.begin(),vec1.end(),printn);
//cout << vec1.begin()+1;
copy(vec1.begin(),vec1.end(),ostream_iterator(cout,"\n"));
//copy(vec.begin(),vec.end(),ostream_iterator(cout,"\n"));
//int arr[8] = {1,2,3,3,3,3,4,5};
//vectorvec2(&arr[0],&arr[5]);
//cout << "打印vec2:" << endl;
//copy(vec2.begin(),vec2.end(),ostream_iterator(cout,"\n"));
//cout << "打印个数" << count(&arr[0],&arr[8],3) << endl;
vector vec3;
vec3.push_back(3);
vec3.push_back(1);
vec3.push_back(2);
vec3.push_back(4);
sort(vec3.begin(),vec3.end());
cout << "打印vec3" << endl;
copy(vec3.begin(),vec3.end(),ostream_iterator(cout,"\n"));
vector vec4;
vec4.reserve(10);
//transform(vec3.begin(),vec3.end(),vec4.begin(),back_inserter(vec4));
copy(vec3.begin(),vec3.end(),back_inserter(vec4));
cout << "打印vec4" << endl;
copy(vec4.begin(),vec4.end(),ostream_iterator(cout,"\n"));
vector::iterator itf = find(vec4.begin(),vec4.end(),3);
if(itf == vec4.end())
{
cout << "not found" << endl;
}
else
{
cout << "find 3 in vec4 " << *itf << endl;
}
string ch("hello world!");
copy(ch.begin(),ch.end(),ostream_iterator(cout));
cout << endl;
vector::iterator pos = vec4.begin();
advance(pos,2);
cout << *pos << endl;
int dst = distance(vec4.begin(),vec4.end());
cout << dst << endl;
dst = distance(mylist.begin(),mylist.end());
reverse(vec4.begin(),vec4.end());
copy(vec4.begin(),vec4.end(),ostream_iterator(cout,"\n"));
istream_iterator reader(cin);
istream_iterator enditer;
while(reader != enditer)
{
cout << "once :" << *reader << endl;
cout << "once again: " << *reader << endl;
++reader;
}
//list list1;
//generate_n(back_inserter(list1),9,2);
listlist2;
for(i = 1; i < 10; i++)
{
list2.push_back(i);
}
for_each(list2.begin(),list2.end(),printn);
list::iterator pos1;
pos1 = remove_if(list2.begin(),list2.end(),Nth(3));
list2.erase(pos1,list2.end());
copy(list2.begin(),list2.end(),ostream_iterator(cout," "));
cout << endl;
pos1 = find_if(list2.begin(),list2.end(),ptr_fun(checkn));
cout << "find 4 " << *pos1 << endl;
transform(list2.begin(),list2.end(),ostream_iterator(cout," "),bind2nd(multiplies(),2));
return 0;
}