C++ 中不同容器的子區間交換


在本教程中,我們將討論一個程式,以瞭解 C++ 中不同容器的子區間的交換。

為此,我們將提供向量和列表,我們需要交換其中的一些元素。

示例

 現場演示

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main(){
   vector<int> v = { -10, -15, -30, 20, 500 };
   list<int> lt = { 10, 50, 30, 100, 50 };
   swap_ranges(v.begin(), v.begin() + 3, lt.begin());
   for (int n : v)
      cout << n << ' ';
   cout << '\n';
   for (int n : lt)
      cout << n << ' ';
   cout << endl;
   return 0;
}

輸出

10 50 30 20 500
-10 -15 -30 100 50

更新於:2020-03-02

58 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.