日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL 之swap, iter_swap, swap_ranges

發布時間:2024/4/11 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 之swap, iter_swap, swap_ranges 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作用:交換元素

聲明:

  • #include?<algorithm>??
  • template<class?Type>??
  • void?swap(Type&?objcet1,?Type&?objec2);??
  • ??
  • template<class?forwardItr1,class?forwardItr2>??
  • void?iter_swap(forwardItr1?first,forwardItr2?scecod);??
  • ??
  • template<class?forwardItr1,?class?forwardItr2>??
  • forwardItr2?swap_ranges(forwardItr1?first,forwardItr1?last,?forwardItr2?first2);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main()?{??
  • ????char?cList[10]?=?{'A','B','C','D','F','G','H','I','J','K'};??
  • ????vector<char>?charList(cList,cList+10);??
  • ??
  • ????vector<char>::iterator?charItr;??
  • ????ostream_iterator<char>?screen(cout,"?");??
  • ????cout?<<?"charList:"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?容器內部元素交互??
  • ????swap(charList[0],charList[1]);??
  • ????cout?<<?"charList.swap"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?用迭代器進行交互??
  • ????iter_swap(charList.begin()?+?2,charList.begin()?+?3);??
  • ????cout?<<?"charList.iter_swap"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ??
  • ????charItr?=?charList.begin()?+?4;??
  • ????iter_swap(charItr,charItr?+?1);??
  • ????cout?<<?"charList.iter_swap"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ??
  • ????int?list[10]?=?{1,2,3,4,5,6,7,8,9,10};??
  • ????vector<int>?intList(list,list?+?10);??
  • ????ostream_iterator<int>?screenInt(cout,?"?");??
  • ????cout?<<?"intList:"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????swap_ranges(intList.begin(),intList.begin()+4,intList.begin()+5);??
  • ????cout?<<?"intList.swap_ranges:"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????//?不同容器之間元素交互??
  • ????swap_ranges(list,list+10,intList.begin());??
  • ????cout?<<?"list:"?<<?endl;??
  • ????copy(list,list+10,screenInt);??
  • ????cout?<<?endl;???
  • ????cout?<<?"intList:?"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????return?0;??
  • }??
  • 運行結果:

    charList:
    A B C D F G H I J K
    charList.swap
    B A C D F G H I J K
    charList.iter_swap
    B A D C F G H I J K
    charList.iter_swap
    B A D C G F H I J K
    intList:
    1 2 3 4 5 6 7 8 9 10
    intList.swap_ranges:
    6 7 8 9 5 1 2 3 4 10
    list:
    6 7 8 9 5 1 2 3 4 10
    intList:
    1 2 3 4 5 6 7 8 9 10

    總結

    以上是生活随笔為你收集整理的STL 之swap, iter_swap, swap_ranges的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。