From e939623c1d507ab64d3b73a02613907fa831cfb0 Mon Sep 17 00:00:00 2001 From: alvaro Date: Fri, 14 Jul 2023 22:23:00 +0200 Subject: [PATCH] Inproving deque description and fixing the example --- Data Structures and Algorithms.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Data Structures and Algorithms.md b/Data Structures and Algorithms.md index e044724..c56df0d 100644 --- a/Data Structures and Algorithms.md +++ b/Data Structures and Algorithms.md @@ -119,9 +119,11 @@ v.clear(); **Use for** * Similar purpose of `std::vector` * Basically `std::vector` with efficient `push_front` and `pop_front` +* More efficient management of dinamically growth than `std::vector` **Do not use for** * C-style contiguous storage (not guaranteed) +* Access by adding a offset to an iterator (do not use `iterator + index`) **Notes** * Pronounced 'deck' @@ -149,9 +151,9 @@ std::deque d; //--------------------------------- // Insert head, index, tail -d.push_front(value); // head -d.insert(d.begin() + index, value); // index -d.push_back(value); // tail +d.push_front(value); // head +d.insert(std::next(d.begin(), index), value); // index with std::next +d.push_back(value); // tail // Access head, index, tail int head = d.front(); // head @@ -167,9 +169,9 @@ for(std::deque::iterator it = d.begin(); it != d.end(); it++) { } // Remove head, index, tail -d.pop_front(); // head -d.erase(d.begin() + index); // index -d.pop_back(); // tail +d.pop_front(); // head +d.erase(std::next(d.begin(), index)); // index with std::next +d.pop_back(); // tail // Clear d.clear();