C++算法 reverse_copy()函数用于将参数[first,last)的元素复制到以结果开始的另一个参数,以使参数中的元素的顺序相反。
template <class BidirectionalIterator, class OutputIterator> OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
first:一个双向迭代器,它指向第一个元素在元素反转参数内的位置。
last:前向迭代器,在元素反转参数内,将位置指向最后一个元素之后的位置。
result:输出迭代器,该迭代器指向将元素复制到的参数的初始位置。
此函数返回一个输出迭代器,该迭代器指向复制参数的末尾[first,last],该位置将复制更改后的元素序列。
让我们看一个简单的示例来演示reverse_copy()的用法:
#include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { vector<int> v({1,2,3}); cout << "Before: "; for (const auto& value : v) { cout << value << " "; } cout << '\n'; vector<int> destination(3); reverse_copy(begin(v), end(v), begin(destination)); cout << "After: "; for (const auto& value : destination) { cout <<value << " "; } cout << '\n'; return 0; }
输出:
Before: 1 2 3 After: 3 2 1
让我们看另一个简单的示例来反转字符串:
#include <algorithm> #include <iostream> #include <string> #include <iterator> using namespace std; int main() { string str = "Hello Myself Nikita"; cout<<"Before Reverse: "<< str<< endl; cout<<"After Reverse: "; reverse_copy(str.begin(), str.end(), ostream_iterator<char>(cout, "")); return 0; }
输出:
Before Reverse: Hello Myself Nikita After Reverse: atikiN flesyM olleH
让我们看另一个简单的例子来反转数字参数:
#include <vector> #include <algorithm> #include <iostream> int main( ) { using namespace std; vector <int> v1, v2( 10 ); vector <int>::iterator Iter1, Iter2; int i; for ( i = 0 ; i <= 9 ; i++ ) { v1.push_back( i ); } cout << "The original vector v1 is:\n ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; //Reverse the elements in the vector reverse_copy (v1.begin( ), v1.end( ), v2.begin( ) ); cout << "The copy v2 of the reversed vector v1 is:\n ( " ; for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ ) cout << *Iter2 << " "; cout << ")." << endl; cout << "The original vector v1 remains unmodified as:\n ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; return 0; }
输出:
The original vector v1 is: ( 0 1 2 3 4 5 6 7 8 9 ). The copy v2 of the reversed vector v1 is: ( 9 8 7 6 5 4 3 2 1 0 ). The original vector v1 remains unmodified as: ( 0 1 2 3 4 5 6 7 8 9 ).
让我们看另一个简单的例子:
#include <iostream> #include <algorithm> #include <vector> #include <iomanip> #include <iterator> using namespace std; void print(string a[], int N) { for(int i = 0; i < N; i++) { cout << (i + 1) << ". " << setw(5) << a[i] << " "; } cout << endl; } int main() { string s[] = {"George", "John", "Nikki", "Alice", "Bob", "Watson"}; string t[6]; cout << "Original order : "; print(s, 6); cout << "\nReversing the order ... " << endl; //Doesn't modify original array s[] reverse_copy(s, s + 6, t); cout << "Reversed order : "; print(t, 6); return 0; }
输出:
Original order : 1. George 2. John 3. Nikki 4. Alice 5. Bob 6. Watson Reversing the order ... Reversed order : 1. Watson 2. Bob 3. Alice 4. Nikki 5. John 6. George
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)