# Difference Between deque::assign and deque::empty in C++

• Last Updated : 02 Aug, 2022

Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.

Here we will see the difference between deque::assign and deque::at in C++.

## deque::assign

deque::assign is used to assign new contents to the deque container by replacing its current contents. It changes its size accordingly.

Syntax:

`dequename.assign(<int> size, <int> val)`

Parameters:

1. size – Number of values to be assigned to the container.
2. val – Value to be assigned to the container.

`<deque>`

Exceptions: If an exception is thrown, the container is in a valid state.

Iterator Validity: In this container, all the iterators, pointers, and references are invalidated.

Example:

## C++

 `// C++ program to implement deque::assign``#include ``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``// Declaration of Deque``    ``deque<``int``> d1 = { 1, 3, 6, 0, 0 };``    ``deque<``int``> d2;`` ` `    ``// Assigning d1 deque elements``    ``// into d2 deque``    ``d2.assign(d1.begin(), d1.end());`` ` `    ``cout << ``"Elements in d2 container are : "``;``    ``for` `(``auto` `it = d2.begin(); it != d2.end(); ++it) {``        ``cout << *it << ``" "``;``    ``}``    ``cout << endl;``    ``return` `0;``}`

Output

`Elements in d2 container are : 1 3 6 0 0 `
• Time Complexity – O(N)
• Space Complexity – O(N)

## deque::empty

deque::empty is used to check whether the deque container is empty or not. It does not change anything in the container.

Syntax:

`emtpy();`

`<deque>`

Exceptions: It never throws any exception

Iterator Validity: Iterator Validity does not change in this container.

Example:

## C++

 `// C++ program to implement deque::empty``#include ``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``// Declaration of Deque``    ``deque<``int``> GFG = { 1, 3, 6, 0, 0 };`` ` `    ``if` `(GFG.empty() == ``true``) {``        ``cout << ``"The deque is empty"` `<< endl;``    ``}``    ``else` `{``        ``cout << ``"The deque is not empty"` `<< endl;``    ``}``    ``return` `0;``}`

Output

`The deque is not empty`
• Time Complexity – O(1)
• Space Complexity – O(1)

## Differences between deque::assign and deque::empty

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.72264445,"math_prob":0.94756633,"size":2547,"snap":"2022-40-2023-06","text_gpt3_token_len":676,"char_repetition_ratio":0.1439245,"word_repetition_ratio":0.14553991,"special_character_ratio":0.28425598,"punctuation_ratio":0.19880715,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96628296,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-25T16:44:17Z\",\"WARC-Record-ID\":\"<urn:uuid:664699d4-1034-495f-98f1-201764920ae1>\",\"Content-Length\":\"133255\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bbe1fc22-d09f-4510-b6b1-b62c777e3d47>\",\"WARC-Concurrent-To\":\"<urn:uuid:0fe6acc4-cbb6-4a19-85f9-7af380fc666f>\",\"WARC-IP-Address\":\"104.97.85.57\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/difference-between-dequeassign-and-dequeempty-in-cpp/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:HUOWACM2NFLFUI6FNLOG6QS7Z62Q6R4E\",\"WARC-Block-Digest\":\"sha1:4UONEZGZRWPOXU4EDUGXWQZULG7SU3NL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030334591.19_warc_CC-MAIN-20220925162915-20220925192915-00565.warc.gz\"}"}