C++中std::reverse和std::reverse_copy的使用
生活随笔
收集整理的這篇文章主要介紹了
C++中std::reverse和std::reverse_copy的使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
std::reverse:反轉(zhuǎn)排序容器內(nèi)指定范圍中的元素。
??????? std::reverse_copy與std::reverse唯一的區(qū)別是:reverse_copy會(huì)將結(jié)果拷貝到另外一個(gè)容器中,而不影響原容器的內(nèi)容。
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的測(cè)試代碼,詳細(xì)內(nèi)容介紹可以參考對(duì)應(yīng)的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 1std::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 spacestd::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;
}
GitHub: https://github.com/fengbingchun/Messy_Test
總結(jié)
以上是生活随笔為你收集整理的C++中std::reverse和std::reverse_copy的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++11中std::addressof
- 下一篇: C++/C++11中std::queue