變動性演算法modifying algorithms

2021-09-30 05:46:47 字數 2283 閱讀 9615

目的:變動內容

注意:目標區間不能是關聯式容器

所有具有單一目標區間的演算法,都返回最後乙個被複製元素的下乙個位置

一。複製

1.outputiterator copy(inputiterator sourcebegin, inputiterator sourceend, outputiteratordestbegin);

2.bidirectionaliterator1 copy_backward(bidirectionaliterator1 sourcebegin, bidirectionaliterator1 sourceend,

bidirectionaliterator2destend);

copy正向遍歷,copy_backward逆向遍歷

注意:當源區間和目標區間重複時,1.複製到前端,則用copy,2. 複製到後端,用copy_backward

逆向遍歷:reverse_copy();

二。轉換

1.outputiterator transform(inputiterator sourcebegin, inputiterator sourceend, outiterator destbegin,

unaryfunc op);                              //op(elem);返回最後乙個被轉換的元素的下乙個位置

outputiterator transform(inputiterator1 source1begin, inputiterator1 source1end,

inputiterator2 source2begin, outputiterator destbegin, binaryfunc op);

// op(source1elem,source2elem);         返回最後乙個被轉換元素的下乙個元素

確保目標區間有足夠空間,或用插入迭代器。

三。互換

1.forwarditerator2 swap_ranges(forwarditerator1 begin1,forwarditerator1 end1,forwarditerator2 begin2);

//兩區間不能不能重疊

這個函式主要用於指定區間交換,swap()主要是全部元素交換

四。賦值

1.void fill(forwarditerator begin,forwarditerator end, const t&value);          //[begin,end)賦予value

2.void fill_n(outputiterator begin,size num, const t&value);  //從begin開始前num個元素賦予value

3.void generatr(forwarditerator begin, forwarditerator end, func op);   //op();產生新值給[begin,end);

4.void generate_n(outputiterator begin, size num, func op); //op()產生num個新值給begin開始的區間

五。替換

1.void replace(forwarditerator begin, forwarditerator end, const t&oldvalue, const t&newvalue);

//把[begin,end)的oldvalue值替換為newvalue

2.void replace_if(forwarditerator  begin, forwarditerator end, unarypredicate op, const t&newvalue);

//op(elem)為true的元素替換為newvalue

3.outputiterator replace_copy(inputiterator sourcebegin, inputiterator sourceend, outputiterator destbegin,

const t&oldvalue, const t&newvalue);

replace_copy 是copy再replace。

4.outputiterator replace_copy_if(inputiterator sourcebegin, inputiterator sourceend, outputiterator destbegin,

unarypredicate op, const t&newvalue);

replace_copy_if是copy和replace_if的結合。

stl演算法解析之非變動性演算法

函式 說明for each 對每個元素執行某操作 count 返回元素個數 count if 返回滿足某一準則 條件 的元素個數 min element 返回最小值元素 以迭代器表示 max element 返回最大值元素 以迭代器表示 find 搜尋等於某值的第乙個元素 find if 搜尋滿足某...

讀書筆記 演算法之變動性演算法

複製元素 copy begin,end,destbegin copy backward sourcebegin,source end,destend 將區間的元素複製到destbegin destend裡面去 copy正向,copy backward反向 stl沒有copy if 只有remove ...

讀書筆記 演算法之變動性演算法

複製元素 copy begin,end,destbegin copy backward sourcebegin,source end,destend 將區間的元素複製到destbegin destend裡面去 copy正向,copy backward反向 stl沒有copy if 只有remove ...