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()函數