iter_swap関数(アサイン)の意味を有するイテレータ型必要によって指さが同一ではない2つの反復子要素の値を交換するために使用され、その値は、相互に割り当てることができます。
myprint.hpp
書式#include <iostreamの> の#include < 文字列 > テンプレート <型名T> インライン無効 PRINT_ELEMENTS(のconst T&コル、constのはstd :: 文字列&optstr = "" ) { std :: coutの << optstr。 以下のため(のconstオート&:高専のelem) { std :: coutの << elemは<< " " ; } std :: coutの << はstd ::てendl; }
TEST.CPP
書式#include <iostreamの> の#include <リスト> の#include <アルゴリズム> 書式#include <反復子> #include 「myprint.hpp 」 使用して 名前空間はstdを、 int型のmain() { リスト < int型 > リスト1。 用(INT K = 1 ; K <= 9 ; ++ K) { list1.push_back(K)。 } PRINT_ELEMENTS(LIST1)。 iter_swap(list1.begin()、次の(list1.begin()))。 PRINT_ELEMENTS(LIST1)。 iter_swap(list1.begin()、PREV(list1.end()))。 PRINT_ELEMENTS(LIST1)。 システム(「一時停止」)。 リターン 0 ; }
2. 5. 4. 3. 1. 6 7 8 9
。5. 4. 3. 1. 6 7 8 9
9 5. 4. 3. 1. 6 7 8 2
続行する任意のキーを押し。
参照コード:C ++標準ライブラリ(第2版)