cb46a_c++_STL_算法_逆转和旋转reverse_rotate_函数advance

cb46a_c++_STL_算法_逆转和旋转reverse_rotate
STL算法--变序性算法
reverse() 逆转
reverse_copy()一边复制一般逆转
rotate()旋转,某个位置开始前后交换位置
rotate(ivec2.begin(), ivec2.begin() + 2, ivec2.end());
1,2,3,4,5,6,7,8,9,
rotate后:
3,4,5,6,7,8,9,1,2,

rotate_copy()一边复制一般旋转

、、、
next_permutation()
prev_permutation()
random_shuffle()
partition()
stable_partition()

//ivec2(ivec);//写法错误
ivec2 = ivec;//这个可以,容器之间赋值
ivec2.assign(ivec.begin(), ivec.end());//这个可以,容器之间赋值

set<int>::iterator pos = iset.begin();
set迭代器是双向迭代器,不能pos=pos+1;只能用advance(pos,4);前进移动

其它一般迭代器是随机迭代器,可以使用pos=pos+1的方式。
#include <iterator>//输出流迭代器 ostream_iterator<int>()

/*cb46a_c++_STL_算法_逆转和旋转reverse_rotate
STL算法--变序性算法
reverse() 逆转
reverse_copy()一边复制一般逆转
rotate()旋转,某个位置开始前后交换位置
rotate(ivec2.begin(), ivec2.begin() + 2, ivec2.end());
1,2,3,4,5,6,7,8,9,
rotate后:
3,4,5,6,7,8,9,1,2,

rotate_copy()一边复制一般旋转

、、、
next_permutation()
prev_permutation()
random_shuffle()
partition()
stable_partition()

//ivec2(ivec);//写法错误
	ivec2 = ivec;//这个可以,容器之间赋值
	ivec2.assign(ivec.begin(), ivec.end());//这个可以,容器之间赋值
*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>//用于输出流迭代器

using namespace std;

template <typename TT5>
void print1(TT5 &ilist)
{
	for (TT5::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
	{
		cout << *iter << ' ';
		
	}
	cout << endl;
}

int main()
{
	vector<int> ivec,ivec2;

	for (int i = 1; i <= 9; ++i)
		ivec.push_back(i);
	print1(ivec);
	ivec2.push_back(1);
	//ivec2(ivec);//写法错误
	ivec2 = ivec;//这个可以,容器之间赋值
	ivec2.assign(ivec.begin(), ivec.end());//这个可以,容器之间赋值
	cout << "ivec2的值:" << endl;
	print1(ivec2);
	

	cout << "逆转后的数据:" << endl;
	reverse(ivec.begin(),ivec.end());
	print1(ivec);

	cout << "一边复制一般逆转,直接传递给cout" << endl;
	reverse_copy(ivec.begin(),ivec.end(),ostream_iterator<int>(cout," "));
	cout << endl;

	cout << "逆转一部分数据,输出到cout" << endl;
	
	//print1(ivec);
	reverse_copy(ivec.begin()+1,ivec.end()-1,ostream_iterator<int>(cout," "));
	cout << endl;
	cout << "旋转前:" << endl;
	print1(ivec2);
	cout << "旋转后:" << endl;
	rotate(ivec2.begin(), ivec2.begin() + 2, ivec2.end());
	print1(ivec2);

	//也可以这样:
	rotate(ivec2.begin(), ivec2.end() - 2, ivec2.end());
	print1(ivec2);

	cout << "先用find方法先找到一个数,然后再rotate," << endl;
    rotate(ivec2.begin(), find(ivec2.begin(), ivec2.end(), 5), ivec2.end());
	print1(ivec2);
	


	return 0;
}
/*cb46b

set<int>::iterator pos = iset.begin();
set迭代器是双向迭代器,不能pos=pos+1;只能用advance(pos,4);前进移动

其它一般迭代器是随机迭代器,可以使用pos=pos+1的方式。
#include <iterator>//输出流迭代器 ostream_iterator<int>()
*/
#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>//输出流迭代器

using namespace std;

void print3(int elem)
{
	cout << elem << ' ';
}
template <typename TT6>
void print2(TT6 iset)
{
	/*for (TT6::iterator iter = iset.begin(); iter != iset.end(); ++iter)
		cout << *iter << ' ';
	cout << endl;*/

	
	//两种输出cout的方法,一个是for循环,一个for_each算法。
	for_each(iset.begin(), iset.end(), print3);
	cout << endl;
}

int main()
{
	set<int> iset;
	for (int i = 1; i <= 9; ++i)
		iset.insert(iset.end(),i);
	print2(iset);

	cout << "使用rotate_copy" << endl;
	set<int>::iterator pos = iset.begin();
	advance(pos,3);//迭代器前进移动3。类似:pos=pos+3
	//set迭代器是双向迭代器,不能pos=pos+1;只能用函数advance(pos,4);前进移动
	cout << "从pos位子开始前后交换位置。结果如下:" << endl;
	rotate_copy(iset.begin(),pos,iset.end(),ostream_iterator<int>(cout," "));

	cout << "也可以这样做。" << endl;
	pos = iset.end();
	advance(pos,-2);//倒数第二个位置开始,整体前后交换
	rotate_copy(iset.begin(),pos,iset.end(),ostream_iterator<int>(cout," "));

	cout << "使用set的成员函数find来配合使用,位置交换功能" << endl;
	rotate_copy(iset.begin(), iset.find(6),iset.end(), ostream_iterator<int>(cout, " "));
	return 0;
}
发布了469 篇原创文章 · 获赞 211 · 访问量 95万+

猜你喜欢

转载自blog.csdn.net/txwtech/article/details/104505442