Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to reverse the order of elements in deque using C++

2 Answers

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

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

template <typename T>
inline void print(const T& obj)
{
	for (auto element : obj) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	std::deque<int> dq(5);   
	std::deque<int>::size_type size = dq.size();

	for (unsigned i = 0; i < size; i++) dq[i] = i;
	
	print(dq);

	for (unsigned i = 0; i < size / 2; i++)
	{
		int tmp;
		
		tmp = dq[size - 1 - i];
		dq[size - 1 - i] = dq[i];
		dq[i] = tmp;
	}

	print(dq);

	return 0;
}

/*
run:

0 1 2 3 4
4 3 2 1 0

*/

 



answered Feb 22, 2018 by avibootz
0 votes
#include <iostream>
#include <deque>

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

template <typename T>
inline void print(const T& obj)
{
	for (auto element : obj) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	std::deque<int> dq(5);   
	std::deque<int>::size_type size = dq.size();

	for (unsigned i = 0; i < size; i++) dq[i] = i;
	
	print(dq);

	std::reverse(dq.begin(), dq.end());

	print(dq);

	return 0;
}

/*
run:

0 1 2 3 4
4 3 2 1 0

*/

 



answered Feb 22, 2018 by avibootz

Related questions

1 answer 144 views
1 answer 183 views
183 views asked Jul 22, 2020 by avibootz
2 answers 248 views
1 answer 188 views
1 answer 172 views
1 answer 140 views
...