您的位置:首页 > 编程语言 > C语言/C++

C++ STL 学习笔记

2009-10-05 23:40 393 查看
(一)STL:
(A)#include<iostream>#include<algorithm>usingnamespacestd;
1)voidsort(iteratorstart,iteratorend);

voidsort(iteratorstart,iteratorend,StrictWeakOrderingcmp);

cmp函数的参数类型只能和sort中排序的类型相同。

sort_heap和sort的使用方法相同。

voidsort_heap(iteratorstart,iteratorend);

voidsort_heap(iteratorstart,iteratorend,StrictWeakOrderingcmp);


2)a:boolbinary_search(iteratorstart,iteratorend,constTYPE&val);boolbinary_search(iteratorstart,iteratorend,constTYPE&val,Compf);val表示的是要查找的值。

Ifvalisfound,binary_search()returnstrue,otherwisefalse

b:InputIteratorfind(InputIteratorfirst,InputIteratorlast,constT&value);

theusagewouldbe:

cout<<find(a,a+20,5)-a<<endl;

orusingthevector.

3)iteratorpartition(iteratorstart,iteratorend,Predicatep);

通过p将元素分为两个部分。

iteratorstable_partition(iteratorstart,iteratorend,Predicatep);

不改变元素的顺序。

4)reverse(start,end)whichcanbeusedbycombinedwiththe<for_each(start,end,action)>like:

reverse(a,a+20);for_each(a,a+20,act);voidact(inti){cout<<i<<”“;}cout<<endl;

5)remove(start,end,ele);removetheeleinthecontainer.starttocontainer.end.andthefunctionreturnthelastpositionofthecontainerafterremovetheele.

6)replace(start,end,old,new)usingthenewelementtoreplacetheoldelementintherangeof[start,end]

7)rotate(start,mid,end)changtherangeof[start,mid]with[mid,end]andtheusageofitisthesameofrp.

8)swap_ranges(arr1.start,arr1.end,arr2)thisfunctionchangetherangeofarr1[start,end]witharr2.

9)transform(arr1.start,arr1.end,arr2,act)thisfunctioniscasttheeffectofactontherangeofarry1fromstarttoend,andstorethenewelementinthearray2.Notethatweneedtoinitializethearray2withenoughspaceinordertostoretheelementbeforerecalltransform

10)unique(arr.start,arr.end)thisfunctionwillremoveanysequentialelementinthearray.Anditwillreturniterator(ifusingvector)orint*(youcanchecktheusageintheprogrampresentbelow)

(二)numeric:

(A)#include<numeric>#include<iostream>usingnamespacestd;

1)accumulate(start,end,init)initmeanstheinitialsum.Bydefaulttheoperationissum,whiletheusercandefineotheroperationbydefineafunctionLike:

a)intmyFunction(intx,inty){returnx+2*y;}

b)accumulate(start,end,init,myFunction);

2)numericalsoincludeotherstandardfunctionlike:

adjacent_difference,inner_product,partial_sumwhichIneedtoacknowledge.

#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
usingnamespacestd;

boolcmp(inta,intb)
{
returna>b;
}
structPoint
{
intx;
inty;
};
boolcmp1(Pointa,Pointb)
{
if(a.x==b.x)
returna.y>b.y;
else
a.x>b.x;
}
voidop1(inti) {cout<<i<<'';}
boolisOdd(inti){return(i%2)==1;}
intop_increase(inti){return++i;}
intmain()
{
inti,j,k;
inta[20]={10,23,5,34,5,56,34,53,304,457,3,
46,34,2345,35,34,444,45,34};
Pointp[5]={{1,2},{5,2},{1,7},{2,6},{3,3}};

//===============================================
cout<<"sort+struct"<<endl;
sort(p,p+5,cmp1);
for(j=0;j<5;j++)
printf("(%d,%d)",p[j].x,p[j].y);
cout<<endl;

//=====================================
vector<int>myVector(a,a+20);
vector<int>::iteratorbound=partition(myVector.begin(),myVector.end(),isOdd);
cout<<"partitionodd"<<endl;
vector<int>::iteratorit;
for(it=myVector.begin();it!=bound;it++)
cout<<*it<<"";
cout<<endl<<"partitioneven"<<endl;
for(it=bound;it!=myVector.end();it++)
cout<<*it<<"";
cout<<endl;
//==========================================
intn;
cout<<"sort/n";
//sortÒ²¿ÉÒÔ×Ô¶¨ÒåÓû§µÄ±È½Ïº¯Êý,µ«ÊǺÍqsort²»Í¬ÕâÀïcmpº¯ÊýµÄ²ÎÊýҪΪÌض¨µÄÖµ£¬²»ÄÜΪconstvoid*£»
sort(a,a+20,cmp);
//sort_heapÓësortµÄʹÓ÷½·¨Ïàͬ¡£
//sort_heap(a,a+20,cmp);
for(i=0;i<20;i++)
cout<<a[i]<<"";
cout<<endl;
//============================================================
cout<<"reversethearray"<<endl;
reverse(a,a+20);
for_each(a,a+20,op1);
cout<<endl;
//============================================================
//accumulateisnotstlbutnumeric;
vector<int>v1(a,a+20);
cout<<"thesumofvectorvis:"<<accumulate(v1.begin(),v1.end(),0)<<endl;
cout<<"thesumofarrayais:"<<accumulate(a,a+20,0)<<endl;
//==================================================================

cout<<"findinthearray"<<endl;
cout<<find(a,a+20,5)-a<<endl;
//==================================================================
cout<<"findthefirstof:inthearray"<<endl;
cout<<*find_first_of(a,a+10,a,a+20)<<endl;
//==================================================================
cout<<"removetheelementfromthearray:"<<endl;
int*start=a;//thepointeraftertheremovewillchange;
int*end=a+sizeof(a)/sizeof(int);
end=remove(start,end,5);
//for_each(a,a+sizeof(a)/sizeof(int),op1);
for_each(start,end,op1);
//for(int*pp=start;pp<end;++pp)
// cout<<*pp<<"";
//cout<<endl;
//==================================================================
cout<<"/nreplaceinthearray"<<endl;
replace(a,a+18,3,-1);
for_each(a,a+18,op1);
//==================================================================
cout<<"rotateinthearray"<<endl;
rotate(a,a+3,a+18);
for_each(a,a+18,op1);
cout<<endl;
//==================================================================
cout<<"swap_range"<<endl;
intar1[5]={1,1,1,1,1};
intar2[5]={2,2,2,2,3};
swap_ranges(ar1,ar1+5,ar2);
for_each(ar1,ar1+5,op1);
cout<<endl;
for_each(ar2,ar2+5,op1);
cout<<endl;
//================================================================
cout<<"transform"<<endl;
int*new_array=newint[sizeof(ar1)/sizeof(int)];
transform(ar1,ar1+5,new_array,op_increase);
for_each(new_array,new_array+5,op1);
cout<<endl;
//==================================================================
cout<<"unique"<<endl;
int*l=unique(ar1,ar1+5);
for_each(ar1,ar1+*l,op1);
cout<<endl;
//==================================================================
while(cin>>n)
{
//binary_searchʹÓÃ֮ǰһ¶¨Òª½«Êý×éÅźÃ˳Ðò²Å¿ÉÒÔʹÓá£
cout<<"binary_search()"<<endl;
if(binary_search(a,a+20,n))
printf("yes/n");
else
printf("no/n");
}

return0;
}[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: