stl中copy()函数
C ++ STL std :: rotate_copy()函数 (C++ STL std::rotate_copy() function)
rotate_copy() function is a library function of algorithm header, it is used to rotate left the elements of a sequence within a given range and copy the rotating elements to another sequence, it accepts the range (start, end) of the input sequence, a middle point, and an iterator pointing to start element of result sequence. It rotates the elements in such a way that the element pointed by the middle iterator becomes the new first element.
rotation_copy()函数是算法标头的库函数,用于在给定范围内向左旋转序列的元素,并将旋转的元素复制到另一个序列,它接受输入序列的范围(开始,结束),一个中间点,以及一个指向结果序列开始元素的迭代器。 它以使中间迭代器指向的元素成为新的第一个元素的方式旋转元素。
Note: To use rotate_copy() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.
注意:要使用rotate_copy()函数 –包括<algorithm>头文件,或者您可以简单地使用<bits / stdc ++。h>头文件。
Syntax of std::rotate_copy() function
std :: rotate_copy()函数的语法
std::rotate_copy(
iterator start,
iterator middle,
iterator end,
iterator start_result);
Parameter(s):
参数:
iterator start – an iterator pointing to the first element of the sequence.
迭代器开始 –指向序列第一个元素的迭代器。
iterator middle – an iterator pointing to the middle or any other elements from where we want to start the rotation.
中间迭代器 –指向中间或我们要开始旋转的位置的任何其他元素的迭代器。
iterator end – an iterator pointing to the last element of the sequence.
迭代器末端 –指向序列的最后一个元素的迭代器。
iterator start_result – an iterator pointing to the first element in result sequence.
iterator start_result –指向结果序列中第一个元素的迭代器。
Return value: void – it returns noting.
返回值: void –返回注释。
Example:
例:
Input:
//an array (source)
int arr[] = { 10, 20, 30, 40, 50 };
//vector
vector<int> v(5);
//rotating and copy array elements to the vector
rotate_copy(arr + 0, arr + 2, arr + 5, v.begin());
Output:
vector elements: 30 40 50 10 20
C ++ STL程序演示了std :: rotate_copy()函数的使用 (C++ STL program to demonstrate use of std::rotate_copy() function)
In this program, we have an array and a vector; we are rotating its elements from 2nd index and copying into the vector.
在这个程序中,我们有一个数组和一个向量。 我们将其元素从第二索引旋转并复制到向量中。
//C++ STL program to demonstrate use of
//std::rotate_copy() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//main code
int main()
{
//an array (source)
int arr[] = { 10, 20, 30, 40, 50 };
//vector
vector<int> v(5);
//printing array and vector elements
cout << "array elements..." << endl;
for (int x : arr)
cout << x << " ";
cout << endl;
cout << "vector elements begfore rotating..." << endl;
for (int x : v)
cout << x << " ";
cout << endl;
//rotating and copy array elements to the vector
rotate_copy(arr + 0, arr + 2, arr + 5, v.begin());
cout << "vector elements after rotating..." << endl;
for (int x : v)
cout << x << " ";
cout << endl;
return 0;
}
Output
输出量
array elements...
10 20 30 40 50
vector elements begfore rotating...
0 0 0 0 0
vector elements after rotating...
30 40 50 10 20
Reference: C++ std::rotate_copy()
参考: C ++ std :: rotate_copy()
翻译自: https://www.includehelp.com/stl/std-rotate_copy-function-with-example.aspx
stl中copy()函数