[英]Reverse vector, and build another vector
I was wondering if there was a way to reverse a vector and build that into another vector? 我想知道是否有一种方法可以反转一个向量并将其构建到另一个向量中?
#include "std_lib_facilities.h"
int main()
{
vector<int> oldvector;
oldvector[1];
oldvector[3];
oldvector[5];
oldvector[7];
oldvector[9];
}
this is what i have so far, any help would be great. 这是我到目前为止所拥有的,任何帮助都将是很大的。
If you want to create a new vector then you can do this: 如果要创建一个新向量,则可以执行以下操作:
std::vector<int> oldvector = {1,2,3,4,5};
std::vector<int> newvector(oldvector.rbegin(), oldvector.rend());
For reverisng a vector you can simply use 要验证向量,您可以简单地使用
std::reverse(a.begin(), a.end());
and then use it as a new vector. 然后将其用作新载体。
You may want to use std::reverse()
to reverse the vector in place , or reverse iterators returned by std::vector::rbegin()
/ rend()
to build a reversed copy of the original vector. 您可能需要使用
std::reverse()
扭转矢量到位 ,或逆转返回迭代器std::vector::rbegin()
/ rend()
联手打造的原创矢量的反转副本 。
Consider the following compilable sample code ( live here on Ideone.com ): 考虑以下可编译示例代码( 在Ideone.com上位于此处 ):
#include <algorithm> // For std::reverse
#include <iostream> // For console output
#include <vector> // For std::vector
using namespace std;
// Used to print elements of a sequence (like vector).
template <typename Sequence>
void print(const Sequence& s) {
cout << "[ ";
for (const auto& x : s) {
cout << x << ' ';
}
cout << "]" << endl;
}
int main() {
cout << "Original vector:\n";
vector<int> v{10, 20, 30, 40, 50};
print(v);
vector<int> v2(v.rbegin(), v.rend());
cout << "\nReversed copy using reverse iterators:\n";
print(v2);
reverse(v.begin(), v.end());
cout << "\nReversed in-place using std::reverse():\n";
print(v);
}
Output: 输出:
Original vector: [ 10 20 30 40 50 ] Reversed copy using reverse iterators: [ 50 40 30 20 10 ] Reversed in-place using std::reverse(): [ 50 40 30 20 10 ]
There are several approaches to do the task. 有几种方法可以完成任务。 The simplest way is to use either the constructor or member function
assign
with parameters as iterators 最简单的方法是使用任一构造或成员函数
assign
与参数迭代
For example 例如
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v1 = { 1, 3, 5, 7, 9 };
std::vector<int> v2( v1.rbegin(), v1.rend() );
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
or 要么
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v1 = { 1, 3, 5, 7, 9 };
std::vector<int> v2;
v2.assign( v1.rbegin(), v1.rend() );
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
Another approach is to use standard algorithm std::reverse_copy
. 另一种方法是使用标准算法
std::reverse_copy
。 For example 例如
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> v1 = { 1, 3, 5, 7, 9 };
std::vector<int> v2;
v2.reserve( v1.size() );
std::reverse_copy( v1.begin(), v1.end(), std::back_inserter( v2 ) );
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
Or you can write the same using an ordinary loop 或者您可以使用普通循环编写相同的内容
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v1 = { 1, 3, 5, 7, 9 };
std::vector<int> v2;
v2.reserve( v1.size() );
for ( std::vector<int>::size_type i = v1.size(); i != 0; )
{
--i;
v2.push_back( v1[i] );
}
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
Or even you can use a stack as an intermediate container!:) 甚至可以将堆栈用作中间容器!:)
#include <iostream>
#include <vector>
#include <stack>
int main()
{
std::vector<int> v1 = { 1, 3, 5, 7, 9 };
std::stack<int> s;
for ( int x : v1 ) s.push( x );
std::vector<int> v2;
v2.reserve( v1.size() );
while ( !s.empty() )
{
v2.push_back( s.top() );
s.pop();
}
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
And what about a recursive function? 递归函数又如何呢? Well, it could look for example as
好吧,它看起来像
#include <iostream>
#include <vector>
std::vector<int>::iterator reverse( std::vector<int>::const_iterator first,
std::vector<int>::const_iterator last,
std::vector<int>::iterator out )
{
if ( first != last )
{
int x = *first++;
out = ::reverse( first, last, out );
*out++ = x;
}
return out;
}
int main()
{
std::vector<int> v1 = { 1, 3, 5, 7, 9 };
std::vector<int> v2( v1.size() );
::reverse( v1.cbegin(), v1.cend(), v2.begin() );
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
I am sorry! 对不起! I forgot to mention standard algorithm
std::copy_backward
. 我忘了提到标准算法
std::copy_backward
。 It also can be applied 也可以应用
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v1 = { 1, 3, 5, 7, 9 };
std::vector<int> v2( v1.size() );
std::copy_backward( v1.begin(), v1.end(), v2.rend() );
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.