deque

Use a for loop with a counter starting from 0 to store even numbers into a deque sequence container. The list of numbers should look like this:
20 18 16 14 12 10 8 6 4 2 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <deque>
#include <iterator>
#include <algorithm>

using namespace std;
void main(void) {
	deque<double> values;
	ostream_iterator<double> output(cout, " ");

	values.push_front(0);
	values.push_back(2);
	values.push_front(4);
	values.push_back(6);
	values.push_front(8);
	values.push_back(10);
	values.push_front(12);
	values.push_back(16);
	values.push_front(16);
	values.push_back(18);
	values.push_front(20);

	cout << "Values contains (loop): ";
		for (int i; i < values.size(); ++i)
			cout << values[i] << ' ';
	cout << endl;
}


I got an error C4700 where it says uninitialized local variable 'i' used.
The error message is pretty clear. You're not initialising the value of i.

What do you think the result of i < values.size() will be, if you haven't given i a value?

What do you think the result of ++i will be, if you haven't given i a value?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <deque>
using namespace std;

int main() {
	
	deque<double> values;
	for(int i=0; i<=20; i+=2){
		values.push_front(i);
	}
	
	for(auto x: values){
		cout << x << " ";
	}
	
return 0;	
}
MikeyBoy
anup30


i got another warning in the compiler where it states:

Warning 1 warning C4018: '<' : signed/unsigned mismatch c:\users\user\documents\visual studio 2013\projects\lab5\lab5\lab5.cpp 25 1 Lab5
The value returned by deque::size() is of type size_t, which is unsigned.

You've defined i as int, which is signed.
MikeyBoy


Thank you. I will correct the error you state just now.
MikeyBoy


I found the error where i done silly mistake where i wish to declare deque < int > values instead of deque <double> values.

1
2
3
4
5
using namespace std;
void main(void) {
	
        deque <int > values
	ostream_iterator<int> output(cout, " ");


p/s: my code is working.
Last edited on
Topic archived. No new replies allowed.