C++STL算法篇rotate旋转元素次序的算法

rotate(beg,newbeg,end)和rotate_copy(beg,newbeg,end,destBeg)的特点

1:迭代器类型:前向迭代器,destBeg时输出迭代器
2:返回值类型:前者为void,后者为输出迭代器
3:算法功能:将newbeg成为新第一元素,[beg,newbeg)区间元素放到end后面,后者将结果输出到目标迭代器
4:复杂度:线性复杂度
5:调用者必须保证newbeg是[beg,end)内的有效位置,目标区间没有足够空间时应采用插入迭代器

#include
#include
#include
using namespace std;

int main()
{
    //以第4个元素为中心进行旋转
	vector<int>c1 = {1,2,3,3,4,5,6,7,8,9};
	vector<int>c2;
	cout << "c1:";
	copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	rotate(c1.begin(), c1.begin() + 3, c1.end());

	cout << "c1:";
	copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
	cout << endl;	
}	
#include
#include
#include
using namespace std;

int main()
{
   //以第4个元素为中心进行旋转,旋转结果复制到c2中
	vector<int>c1 = {1,2,3,3,4,5,6,7,8,9};
	vector<int>c2;
	cout << "c1:";
	copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	rotate_copy(c1.begin(), c1.begin() + 3, c1.end(),back_inserter(c2));

	cout << "c1:";
	copy(c1.begin(), c1.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	cout << "c2:";
	copy(c2.begin(), c2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;	
}

你可能感兴趣的:(C++学习笔记)