內容講義案例transform iterator_第1頁
內容講義案例transform iterator_第2頁
內容講義案例transform iterator_第3頁
內容講義案例transform iterator_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

PAGE

1

TransformI tor

Author: DavidAbrahams,JeremySiek,ThomasWitt

Contact: dave@bo ,

jsiek@,

witt@ive.uni-hannover.de

Organization: BoostConsulting, naUniversityOpenSystemsLab,UniversityofHanoverInstituteforTransportRailwayOperationandConstruction

Date: 2004-11-01

Copyright: CopyrightDavidAbrahams,JeremySiek,andThomasWitt2003.

:Thetransformi toradaptsani torbymodifyingtheoperator*toapplyafunctionobjecttotheresultofdereferencingthei torandreturningtheresult.

TableofContents

transform_i torsynopsistransform_i torrequirementstransform_i tormodelstransform_i toroperationsExample

transform_i torsynopsis

temte<classUnaryFunction,

classI tor,

classReference=use_default,classValue=use_default>

classtransform_i tor

{

public:

typedef/*seebelow*/value_type;typedef/*seebelow*/reference;typedef/*seebelow*/pointer;

typedefi tor_traits<I tor>::difference_typedifference_type;typedef/*seebelow*/i tor_category;

transform_i tor();

transform_i tor(I torconst&x,UnaryFunctionf);

temte<classF2,classI2,classR2,classV2>transform_i tor(

transform_i tor<F2,I2,R2,V2>const&t

PAGE

2

,typenameenable_if_convertible<I2,I tor>::type*=0 //ex-positiononly

,typenameenable_if_convertible<F2,UnaryFunction>::type*=0//ex-positiononly

);

UnaryFunctionfunctor()const;I torconst&base()const;referenceoperator*()const;

transform_i tor&operator++();transform_i tor&operator--();

private:

I torm_i tor;//expositiononlyUnaryFunctionm_f; //expositiononly

};

IfReferenceisuse_defaultthenthereferencememberoftransform_i torisresult_of<UnaryFunction(iteOtherwise,referenceisReference.

IfValueisuse_defaultthenthevalue_typememberisremove_cv<remove_reference<reference>

>::type.Otherwise,value_typeisValue.

IfItormodelsReadableLvalueItorandifItormodelsRandomAccessTraver-salItor,thenitor_categoryisconvertibletorandom_access_itor_tag.Otherwise,ifItormodelsBidirectionalTraversalItor,thenitor_categoryisconvertibletobidi-rectional_itor_tag.Otherwiseitor_categoryisconvertibletoforward_itor_tag.IfItordoesnotmodelReadableLvalueItorthenitor_categoryisconvertibletoin-put_itor_tag.

transform_itorrequirements

ThetypeUnaryFunctionmustbeAssignable,CopyConstructible,andtheexpressionf(*i)mustbevalidwherefisanobjectoftypeUnaryFunction,iisanobjectoftypeItor,andwherethetypeoff(*i)mustberesult_of<UnaryFunction(itor_traits<Itor>::reference)>::type.

TheargumentItorshallmodelReadableItor.

transform_itormodels

Theresultingtransform_itormodelsthemostrefinedofthefollowingthatisalsomodeledby

I tor.

WritableLvalueI toriftransform_i tor::referenceisanon-constreference.

ReadableLvalueI toriftransform_i tor::referenceisaconstreference.

ReadableI torotherwise.

Thetransform_itormodelsthemostrefinedstandardtraversalconceptthatismodeledbytheItorargument.

Iftransform_itorisamodelofReadableLvalueItorthenitmodelsthefollowingoriginalitorconceptsdependingonwhattheItorargumentmodels.

IfI tormodels thentransform_i tormodels

SinglePassI tor InputI tor

ForwardTraversalI tor ForwardI tor

BidirectionalTraversalI tor BidirectionalI torRandomAccessTraversalI tor RandomAccessI tor

PAGE

3

Iftransform_i tormodelsWritableLvalueI torthenitisamutablei tor(asdefinedintheoldi torrequirements).

transform_i tor<F1,X,R1,V1>isinteroperablewithtransform_i tor<F2,Y,R2,V2>

ifandonlyifXisinteroperablewithY.

transform_i toroperations

Inadditiontotheoperationsrequiredbytheconceptsmodeledbytransform_i tor,trans-form_i torprovidesthefollowingoperations.

transform_i tor();

Returns:Aninstanceoftransform_i torwithm_fandm_i tordefaultcon-structed.

transform_i tor(I torconst&x,UnaryFunctionf);

Returns:Aninstanceoftransform_i torwithm_finitializedtofandm_i tor

initializedtox.

temte<classF2,classI2,classR2,classV2>transform_i tor(

transform_i tor<F2,I2,R2,V2>const&t

,typenameenable_if_convertible<I2,I tor>::type*=0 //expo-sitiononly

,typenameenable_if_convertible<F2,UnaryFunction>::type*=0//expo-sitiononly

);

Returns:Aninstanceoftransform_i torwithm_finitializedtot.functor()and

m_i torinitializedtot.base().

Requires:OtherI torisimplicitlyconvertibletoI tor.UnaryFunctionfunctor()const;

Returns:m_f

I torconst&base()const;

Returns:m_i torreferenceoperator*()const;

Returns:m_f(*m_i tor)transform_i tor&operator++();

Effects:++m_i tor

Returns:*this

transform_i tor&operator--();

Effects:--m_i tor

Returns:*this

temte<classUnaryFunction,classI tor>transform_i tor<UnaryFunction,I tor>make_transform_i tor(I torit,UnaryFunctionfun);

Returns:Aninstanceoftransform_i tor<UnaryFunction,I tor>withm_fini-tializedtofandm_i torinitializedtox.

temte<classUnaryFunction,classI tor>transform_i tor<UnaryFunction,I tor>make_transform_i tor(I torit);

Returns:Aninstanceoftransform_i tor<UnaryFunction,I tor>withm_fde-faultconstructedandm_i torinitializedtox.

Example

Thisisasimpleexampleofusingthetransformitorsclasstogenerateitorsthatmultiply(oraddto)thevaluereturnedbydereferencingtheitor.Itwouldbecoolertouselambdalibraryinthiample.

intx[]={1,2,3,4,5,6,7,8};

constintN=sizeof(x)/sizeof(int);

typedefboost::binder1st<std::multiplies<int>>Function;

typedefboost::transform_i tor<Function,int*>doubling_i tor;

doubling_i tori(x,boost::bind1st(std::multiplies<i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論