How to reverse copy a deque of chars into other deque in C++

2 Answers

0 votes
#include <iostream>
#include <deque>

using std::cout;
using std::endl;
using std::deque;

int main()
{
	deque<char> dq{ 'a', 'b', 'c', 'd' }, dq_rev;

	while (!dq.empty()) {
		dq_rev.push_front(dq.front());
		dq.pop_front();
	}
	
	for (char val : dq_rev)
		cout << val << " ";
	
	cout << endl;

	return 0;
}

/*
run:

d c b a

*/

 



answered May 4, 2018 by avibootz
0 votes
#include <iostream>
#include <deque>
#include <string>

using std::cout;
using std::endl;
using std::deque;
using std::string;

int main()
{
	deque<char> dq{ 'a', 'b', 'c', 'd' }, dq_rev(4);

	copy(dq.rbegin(), dq.rend(), dq_rev.begin());

	for (char val : dq_rev)
		cout << val << " ";
	
	cout << endl;

	return 0;
}

/*
run:

d c b a

*/

 



answered May 4, 2018 by avibootz

Related questions

2 answers 206 views
1 answer 157 views
1 answer 158 views
158 views asked Apr 20, 2018 by avibootz
1 answer 211 views
1 answer 165 views
1 answer 206 views
206 views asked Jul 22, 2020 by avibootz
...