Abstract
若想將container中的iterator,從一個值取代成另外一個值,但container並沒有提供replace()這個member function,而是提供了replace()這個Generic Algorithm。
Introduction
以下範例我們將vector中,所有的1取代成4。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
執行結果
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19行的
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第一個參數傳入vector的起始位址,第二個參數傳入vector的結束位址,第三個參數是要被取代的值,第四個參數是要取代成的值。
replace()完整的定義為
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
其他相關的algorithm還有replace_if(),replace_copy(),replace_copy_if()。
Conclusion
初學者學習STL,對於STL的container僅提供有限的功能感到困擾,因為STL是基於泛型(GP : Generic Programming)下的產物,和物件導向的.NET Framework、Java SDK不同。在物件導向下,algorithm和container綁在一起,每個container都自給自足的提供自己的algorithm(member function),但這樣的缺點是,很多container都必須提供相同的基礎功能,這樣會造成class的肥大,且要重複寫相同的功能,如vector需提供replace(),那list、deque、stack怎麼辦?也要重新寫replace()嗎?泛型強調algorithm和container分離,而algorithm可以支援各種container,對於開發STL的人員來說,只要開發一次replace()即可,對於使用STL的人員來說,也只要學習一次replace()即可,而且將來若要從vector改成list,只需換掉container即可,其他程式都不須修改。
所以使用STL時,若container沒有提供適當的功能,別忘了找找Generic Algorithm,也可順便欣賞這個由GP思維下library的beauty。