Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Add std::deque snippets #703

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions snippets/deque/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SRC_PATH=.
OUT_PATH=./bin
SRC_FILES:=$(wildcard ${SRC_PATH}/*.cpp)
SRC_FILES:= $(filter-out ${SRC_PATH}/sample.cpp, $(SRC_FILES))
SRC_BIN=$(SRC_FILES:${SRC_PATH}/%.cpp=${OUT_PATH}/%)

CXX=g++

all: $(SRC_BIN)

${OUT_PATH}/%: ${SRC_PATH}/%.cpp
mkdir -p ${OUT_PATH}
${CXX} -std=c++17 $^ -o $@
clean:
@rm -rf ${OUT_PATH}


23 changes: 23 additions & 0 deletions snippets/deque/at.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Author : Karina Murawko-Wisniewska
Date : Date format 07/12/2022
Time : Time format 11:37
Description : std::deque::at() returns a refernce to the element at position
passed as paramter
*/

#include <iostream>
#include <deque>

int main() {
std::deque<std::string> sampleDeque{"Alice", "Bob", "John"};
int position{1};

// Print element in position 1 (positions are numbered from 0)
std::cout << "Element in position " << position << ": "
<< sampleDeque.at(position) << ".\n";

// output: Element in position 1: Bob.

return 0;
}
24 changes: 24 additions & 0 deletions snippets/deque/empty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Author : Karina Murawko-Wisniewska
Date : Date format 07/12/2022
Time : Time format 11:45
Description : std::deque::empty() returns true if container is empty
*/

#include <deque>
#include <iostream>

int main() {
std::deque<std::string> sampleDeque{"Alice", "Bob", "John"};

// Check if sampleDeque is empty
if (sampleDeque.empty()) {
std::cout << "sampleDeque is empty.\n";
} else {
std::cout << "sampleDeque is not empty.\n";
}

// output: sampleDeque is not empty.

return 0;
}
28 changes: 28 additions & 0 deletions snippets/deque/push_back.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Author : Karina Murawko-Wisniewska
Date : Date format 07/12/2022
Time : Time format 11:37
Description : std::deque::push_back() push element at the end of the deque
*/

#include <iostream>
#include <deque>

int main() {
std::deque<std::string> sampleDeque{"Alice"};

// push 2 elements at the end of sampleDeque
sampleDeque.push_back("Bob");
sampleDeque.push_back("John");

// Print elements in sampleDeque
std::cout << "Elements in sampleDeque: ";
for (auto element : sampleDeque) {
std::cout << element << ", ";
}
std::cout << "\n";

// output: Elements in sampleDeque: Alice, Bob, John,

return 0;
}
29 changes: 29 additions & 0 deletions snippets/deque/push_front.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Author : Karina Murawko-Wisniewska
Date : Date format 07/12/2022
Time : Time format 12:17
Description : std::deque::push_front() push element at the beginning of the
deque
*/

#include <iostream>
#include <deque>

int main() {
std::deque<std::string> sampleDeque{"John"};

// push 2 elements at the beginning of sampleDeque
sampleDeque.push_front("Bob");
sampleDeque.push_front("Alice");

// Print elements in sampleDeque
std::cout << "Elements in sampleDeque: ";
for (auto element : sampleDeque) {
std::cout << element << ", ";
}
std::cout << "\n";

// output: Elements in sampleDeque: Alice, Bob, John,

return 0;
}
8 changes: 8 additions & 0 deletions snippets/deque/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Follow the Style Guide while submitting code PR.
// Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide
/*
Author : this must be your name ;)
Date : Date format dd/mm/yyyy
Time : Time format hh:mm
Description : description should be small & one liner.
*/
20 changes: 20 additions & 0 deletions snippets/deque/size.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Author : Karina Murawko-Wisniewska
Date : Date format 07/12/2022
Time : Time format 11:53
Description : std::deque::size() returns number of elements
*/

#include <deque>
#include <iostream>

int main() {
std::deque<std::string> sampleDeque{"Alice", "Bob", "John"};

// Print number of elements of sampleDeque
std::cout << "sampleDeque has " << sampleDeque.size() << " elements.\n";

// output: sampleDeque has 3 elements.

return 0;
}