C++中std::reverse和std::reverse_copy的使用

std::reverse:反转排序容器内指定范围中的元素。

        std::reverse_copy与std::reverse唯一的区别是:reverse_copy会将结果拷贝到另外一个容器中,而不影响原容器的内容。

std::reverse: defined in header <algorithm>,  reverses the order of the elements in the range [first, last).

std::reverse_copy: defined in header <algorithm>, copies the elements in the range[first,last) to the range beginning at result, but in reverse order.

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "reverse.hpp"
#include <iostream>
#include <algorithm> // std::reverse/std::reverse_copy
#include <vector>
#include <string>

/* The behavior of this function template(std::reverse) is equivalent to
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
	while ((first!=last)&&(first!=--last)) {
		std::iter_swap (first,last);
		++first;
	}
} */

/* The behavior of this function template(std::reverse_copy) is equivalent to:
template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first,
		BidirectionalIterator last, OutputIterator result)
{
	while (first!=last) {
		--last;
		*result = *last;
		++result;
	}
	return result;
} */

/
// reference: http://en.cppreference.com/w/cpp/algorithm/reverse
int test_reverse_1()
{
	std::vector<int> v({ 1, 2, 3 });
	std::reverse(std::begin(v), std::end(v));
	std::cout << v[0] << v[1] << v[2] << '\n';

	int a[] = { 4, 5, 6, 7 };
	std::reverse(std::begin(a), std::end(a));
	std::cout << a[0] << a[1] << a[2] << a[3] << '\n';

	return 0;
}

/
// reference: http://www.cplusplus.com/reference/algorithm/reverse/
int test_reverse_2()
{
	std::vector<int> myvector;

	// set some values:
	for (int i = 1; i<10; ++i) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9

	//std::reverse(myvector.begin(), myvector.end());    // 9 8 7 6 5 4 3 2 1
	std::reverse(myvector.begin(), myvector.begin()+5);    // 5 4 3 2 1 6 7 8 9

	// print out content:
	std::cout << "myvector contains:";
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
		std::cout << ' ' << *it;
	std::cout << '\n';

	std::string str{ "abcdef" };
	std::reverse(str.begin(), str.end());
	fprintf(stderr, "str: %s\n", str.c_str());

	return 0;
}

/
// reference: http://www.cplusplus.com/reference/algorithm/reverse_copy/
int test_reverse_copy_1()
{
	int myints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	std::vector<int> myvector;

	myvector.resize(5);    // allocate space

	std::reverse_copy(myints, myints + 5, myvector.begin());

	// print out content:
	std::cout << "myvector contains:";
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
		std::cout << ' ' << *it;

	std::cout << '\n';

	return 0;
}

//
// reference: http://en.cppreference.com/w/cpp/algorithm/reverse_copy
int test_reverse_copy_2()
{
	std::vector<int> v({ 1, 2, 3 });
	for (const auto& value : v) {
		std::cout << value << " ";
	}
	std::cout << '\n';

	std::vector<int> destination(3);
	std::reverse_copy(std::begin(v), std::end(v), std::begin(destination));
	for (const auto& value : destination) {
		std::cout << value << " ";
	}
	std::cout << '\n';

	return 0;
}

GitHubhttps://github.com/fengbingchun/Messy_Test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值