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 207 views
1 answer 157 views
1 answer 162 views
162 views asked Apr 20, 2018 by avibootz
1 answer 212 views
1 answer 167 views
1 answer 208 views
208 views asked Jul 22, 2020 by avibootz
...