Deque Help!

Good day guys! can someone explain to me the exact use of deq.assign(n,elem);?
,how can i display the value of it in the prompt and how this code(below) obtain its output.it is said that its output are

Size of first: 7
Size of second: 5
Size of third: 3

explain please! :)
Thank you in advance! :)

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
// deque::assign
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> first;
  std::deque<int> second;
  std::deque<int> third;

  first.assign (7,100);             // 7 ints with a value of 100

  std::deque<int>::iterator it;
  it=first.begin()+1;

  second.assign (it,first.end()-1); // the 5 central values of first

  int myints[] = {1776,7,4};
  third.assign (myints,myints+3);   // assigning from array.

  std::cout << "Size of first: " << int (first.size()) << '\n';
  std::cout << "Size of second: " << int (second.size()) << '\n';
  std::cout << "Size of third: " << int (third.size()) << '\n';
  return 0;
}


What do you have problems with?

Code is working and clear.
.assign() member function have several overloads.
One of them takes two integers. First one tells how many values should be in new deque, second one tells what these values are.
So after line 11 first will contain 7 integers with value of 100.

Secon assign() variant takes pair of iterators and assigns to deque values from first up to second iterator.
So after line 16 second will contain 5 integers with value 100
And after line 16 third will contain values 1776, 7, 4

size() member function returns number of values stored inside container. So it is expected to return 7, 5, 3 for respective containers.
http://en.cppreference.com/w/cpp/container/deque/assign


Also I noted that your example is outdated (does not showing C++11 Initializer list assigment) and does not use C++11 features. Also on lines 21-23 you do not need custom cast to int. In fact deque size can be larger that int can hold, so it is dangerous.
my problem is that how this program list the output 7,5,3 and well, you explained it very well! thank you very much sir! :)
Topic archived. No new replies allowed.