C++ 算法 swap_ranges() 将范围 [first1, last2) 中的元素与存在于从 first2 开始的范围中的元素进行交换。
简而言之,我们可以说,swap_ranges() 交换了两个序列的元素,即第一个序列中某个位置的每个元素都被第二个序列中相同位置的元素替换,反之亦然。
用法
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);
参数
first1:指向第一个范围的第一个位置的前向迭代器,其元素将被交换。
last1:一个前向迭代器,指向第一个范围的最终位置,其元素将被交换。
first2:指向第二个范围的第一个位置的前向迭代器,其元素将被交换。
返回值
swap_ranges() 返回一个迭代器,指向要交换元素的第二个范围的末尾。
复杂度
复杂度在 first 和 last 之间是线性的。对范围内的每个元素执行交换操作。
数据竞争
两个范围内的对象都被修改。
异常安全
如果元素赋值或迭代器上的操作抛出异常,则抛出异常。
例子1
让我们看一个简单的例子,用给定的范围交换两个向量的元素:
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main() {
vector<int> v1 = { 1, 2, 3 };
vector<int> v2 = { 4, 5, 6, 7, 8 };
swap_ranges(v1.begin(), v1.end(), v2.begin());
cout << "v1:";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
cout << endl;
cout << "v2:";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", "));
cout << endl;
}
输出:
v1:4, 5, 6, v2:1, 2, 3, 7, 8,
在上面的例子中,向量 v1 的元素被向量 v2 从范围开始到 v1 的结尾交换。
例子2
让我们看一个简单的例子来交换两个向量的内容:
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main ()
{
vector<int> v, c;
for(int j=0; j < 10; j++)
{
v.push_back(j);
c.push_back(j+1);
}
swap_ranges(v.begin(), v.begin()+5, c.begin());
/* swaps the first five element of
vector v by the elements of vector c */
for(vector<int>::iterator i = v.begin() ; i!= v.end() ; i++)
cout<< *i <<" ";
cout<<endl;
for(vector<int>::iterator k = c.begin() ; k!= c.end() ; k++)
cout<<*k<<" ";
}
输出:
1 2 3 4 5 5 6 7 8 9 0 1 2 3 4 6 7 8 9 10
在上面的例子中,swap_range() 函数将向量 v 的前五个元素与向量 c 的元素交换。
例子3
让我们看一个简单的例子来交换 vector 和 deque 的内容:
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
int main( )
{
using namespace std;
vector <int> v1;
deque <int> d1;
vector <int>::iterator v1Iter1;
deque<int>::iterator d1Iter1;
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( i );
}
int ii;
for ( ii =4 ; ii <= 9 ; ii++ )
{
d1.push_back( 6 );
}
cout << "Vector v1 is:( " ;
for ( v1Iter1 = v1.begin( ) ; v1Iter1 != v1.end( ) ;v1Iter1 ++ )
cout << *v1Iter1 << " ";
cout << ")." << endl;
cout << "Deque d1 is: ( " ;
for ( d1Iter1 = d1.begin( ) ; d1Iter1 != d1.end( ) ;d1Iter1 ++ )
cout << *d1Iter1 << " ";
cout << ")." << endl;
swap_ranges ( v1.begin ( ) , v1.end ( ) , d1.begin ( ) );
cout << "After the swap_range vector v1 is:( " ;
for ( v1Iter1 = v1.begin( ) ; v1Iter1 != v1.end( ) ;v1Iter1 ++ )
cout << *v1Iter1 << " ";
cout << ")." << endl;
cout << "After the swap_range deque d1 is: ( " ;
for ( d1Iter1 = d1.begin( ) ; d1Iter1 != d1.end( ) ;d1Iter1 ++ )
cout << *d1Iter1 << " ";
cout << ")." << endl;
return 0;
}
输出:
Vector v1 is:( 0 1 2 3 4 5 ). Deque d1 is: ( 6 6 6 6 6 6 ). After the swap_range vector v1 is:( 6 6 6 6 6 6 ). After the swap_range deque d1 is: ( 0 1 2 3 4 5 ).
在上面的例子中,vector 和 deque 的元素相互交换。
示例 4
让我们看另一个简单的例子:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("Betty Botter bought some butter") ;
string s2("But she said the butter was bitter") ;
string s3("So she got some better butter") ;
string s4("to make the bitter butter better") ;
cout << "s1 = " << s1 << endl ;
cout << "s2 = " << s2 << endl ;
cout << "s3 = " << s3 << endl ;
cout << "s4 = " << s4 << endl ;
cout << "\nJumble them up!!!" << std::endl ;
//swap_ranges
swap_ranges(s1.begin(), s1.begin()+15, s2.begin()) ;
swap_ranges(s3.begin(), s3.begin()+15, s4.begin()) ;
cout << "s1 = " << s1 << endl ;
cout << "s2 = " << s2 << endl ;
cout << "s3 = " << s3 << endl ;
cout << "s4 = " << s4 << endl ;
return 0 ;
}
输出:
s1 = Betty Botter bought some butter s2 = But she said the butter was bitter s3 = So she got some better butter s4 = to make the bitter butter better Jumble them up!!! s1 = But she said thught some butter s2 = Betty Botter boe butter was bitter s3 = to make the bit better butter s4 = So she got someter butter better
相关用法
- C++ Algorithm set_union()用法及代码示例
- C++ Algorithm set_intersection()用法及代码示例
- C++ Algorithm set_difference()用法及代码示例
- C++ Algorithm stable_sort()用法及代码示例
- C++ Algorithm stable_partition()用法及代码示例
- C++ Algorithm set_symmetric_difference()用法及代码示例
- C++ Algorithm sort()用法及代码示例
- C++ Algorithm sort_heap()用法及代码示例
- C++ Algorithm shuffle()用法及代码示例
- C++ Algorithm remove_if()用法及代码示例
- C++ Algorithm remove()用法及代码示例
- C++ Algorithm max_element()用法及代码示例
- C++ Algorithm next_permutation()用法及代码示例
- C++ Algorithm upper_bound()用法及代码示例
- C++ Algorithm minmax()用法及代码示例
- C++ Algorithm remove_copy_if()用法及代码示例
- C++ Algorithm random_shuffle()用法及代码示例
- C++ Algorithm pop_heap()用法及代码示例
- C++ Algorithm replace()用法及代码示例
- C++ Algorithm lower_bound()用法及代码示例
注:本文由纯净天空筛选整理自 C++ Algorithm swap_ranges()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。