From edf11fcb9776ef528c2630299d91625aa268e830 Mon Sep 17 00:00:00 2001 From: Mohamed Faaris <74762980+Mohamed-faaris@users.noreply.github.com> Date: Fri, 3 Jan 2025 01:34:45 +0530 Subject: [PATCH 1/7] added vector-print-utility --- snippets/cpp/basics/vector-print-utility.md | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/cpp/basics/vector-print-utility.md diff --git a/snippets/cpp/basics/vector-print-utility.md b/snippets/cpp/basics/vector-print-utility.md new file mode 100644 index 00000000..8f4626fd --- /dev/null +++ b/snippets/cpp/basics/vector-print-utility.md @@ -0,0 +1,24 @@ +--- +title: std::vector Print Utility +description: Overloads the << operator to print the contents of a vector just like in python. +author: Mohamed-faaris +tags: cpp,printing,vector,utility +--- + +```cpp +#include // Includes the input/output stream library +#include // Includes the vector container + +template +std::ostream& operator<<(std::ostream& os, const std::vector& vec) { + os << "["; // Begin vector formatting with an opening bracket + for (size_t i = 0; i < vec.size(); ++i) { + os << vec[i]; // Print each vector element + if (i != vec.size() - 1) { + os << ", "; // Add separator between elements except after the last one + } + } + os << "]"; // Close vector formatting with a closing bracket + return os; // Return the stream to enable chaining +} +``` From 22fd73a75cb2ae12bfdb4c6528794563084731ce Mon Sep 17 00:00:00 2001 From: Mohamed Faaris <74762980+Mohamed-faaris@users.noreply.github.com> Date: Fri, 3 Jan 2025 04:16:59 +0530 Subject: [PATCH 2/7] Update vector-print-utility.md reduced comments and added examples --- snippets/cpp/basics/vector-print-utility.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/snippets/cpp/basics/vector-print-utility.md b/snippets/cpp/basics/vector-print-utility.md index 8f4626fd..d95ebcb9 100644 --- a/snippets/cpp/basics/vector-print-utility.md +++ b/snippets/cpp/basics/vector-print-utility.md @@ -2,23 +2,27 @@ title: std::vector Print Utility description: Overloads the << operator to print the contents of a vector just like in python. author: Mohamed-faaris -tags: cpp,printing,vector,utility +tags: cpp,printing,debuging,vector,utility --- ```cpp -#include // Includes the input/output stream library -#include // Includes the vector container +#include +#include template std::ostream& operator<<(std::ostream& os, const std::vector& vec) { - os << "["; // Begin vector formatting with an opening bracket + os << "["; for (size_t i = 0; i < vec.size(); ++i) { os << vec[i]; // Print each vector element if (i != vec.size() - 1) { - os << ", "; // Add separator between elements except after the last one + os << ", "; // Add separator } } - os << "]"; // Close vector formatting with a closing bracket - return os; // Return the stream to enable chaining + os << "]"; + return os; // Return the stream } + +//std::vector numbers = {1, 2, 3, 4, 5}; +//std::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5] + ``` From d91dbda1f61491296c75c013fdbcc9dc5244520a Mon Sep 17 00:00:00 2001 From: Mohamed Faaris Date: Fri, 3 Jan 2025 13:59:56 +0530 Subject: [PATCH 3/7] Refactor vector print utility: move to debugging section and update file structure --- .../vector-print-utility.md => debuging/vector-print.md} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename snippets/cpp/{basics/vector-print-utility.md => debuging/vector-print.md} (89%) diff --git a/snippets/cpp/basics/vector-print-utility.md b/snippets/cpp/debuging/vector-print.md similarity index 89% rename from snippets/cpp/basics/vector-print-utility.md rename to snippets/cpp/debuging/vector-print.md index d95ebcb9..28e685fb 100644 --- a/snippets/cpp/basics/vector-print-utility.md +++ b/snippets/cpp/debuging/vector-print.md @@ -1,8 +1,8 @@ --- -title: std::vector Print Utility +title: vector-print description: Overloads the << operator to print the contents of a vector just like in python. author: Mohamed-faaris -tags: cpp,printing,debuging,vector,utility +tags: printing,debuging,vector --- ```cpp From 872f957d7070f842969597d7cf067f867156e9d2 Mon Sep 17 00:00:00 2001 From: Mohamed Faaris <74762980+Mohamed-faaris@users.noreply.github.com> Date: Fri, 3 Jan 2025 15:03:25 +0530 Subject: [PATCH 4/7] Update vector-print.md --- snippets/cpp/debuging/vector-print.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/cpp/debuging/vector-print.md b/snippets/cpp/debuging/vector-print.md index 28e685fb..33a9a34d 100644 --- a/snippets/cpp/debuging/vector-print.md +++ b/snippets/cpp/debuging/vector-print.md @@ -1,5 +1,5 @@ --- -title: vector-print +title: Vector Print description: Overloads the << operator to print the contents of a vector just like in python. author: Mohamed-faaris tags: printing,debuging,vector @@ -22,7 +22,7 @@ std::ostream& operator<<(std::ostream& os, const std::vector& vec) { return os; // Return the stream } -//std::vector numbers = {1, 2, 3, 4, 5}; -//std::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5] +std::vector numbers = {1, 2, 3, 4, 5}; +std::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5] ``` From 0acdc7d50f7db37d2cdefd9dd62b762d1f61c82b Mon Sep 17 00:00:00 2001 From: Mohamed Faaris <74762980+Mohamed-faaris@users.noreply.github.com> Date: Fri, 3 Jan 2025 15:37:00 +0530 Subject: [PATCH 5/7] Rename vector-print.md to Vector Print.md --- snippets/cpp/debuging/{vector-print.md => Vector Print.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/cpp/debuging/{vector-print.md => Vector Print.md} (100%) diff --git a/snippets/cpp/debuging/vector-print.md b/snippets/cpp/debuging/Vector Print.md similarity index 100% rename from snippets/cpp/debuging/vector-print.md rename to snippets/cpp/debuging/Vector Print.md From bd9cdc8d7998b8749151aced24a7e0ab4d76c752 Mon Sep 17 00:00:00 2001 From: Mohamed Faaris <74762980+Mohamed-faaris@users.noreply.github.com> Date: Fri, 3 Jan 2025 15:43:20 +0530 Subject: [PATCH 6/7] Rename Vector Print.md to vector-print.md --- snippets/cpp/debuging/{Vector Print.md => vector-print.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/cpp/debuging/{Vector Print.md => vector-print.md} (100%) diff --git a/snippets/cpp/debuging/Vector Print.md b/snippets/cpp/debuging/vector-print.md similarity index 100% rename from snippets/cpp/debuging/Vector Print.md rename to snippets/cpp/debuging/vector-print.md From dbf6add4db0151b8bc48e33804d9e91b23f65e4d Mon Sep 17 00:00:00 2001 From: Mohamed Faaris <74762980+Mohamed-faaris@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:09:20 +0530 Subject: [PATCH 7/7] Update vector-print.md --- snippets/cpp/debuging/vector-print.md | 1 + 1 file changed, 1 insertion(+) diff --git a/snippets/cpp/debuging/vector-print.md b/snippets/cpp/debuging/vector-print.md index 33a9a34d..9fe8550b 100644 --- a/snippets/cpp/debuging/vector-print.md +++ b/snippets/cpp/debuging/vector-print.md @@ -22,6 +22,7 @@ std::ostream& operator<<(std::ostream& os, const std::vector& vec) { return os; // Return the stream } +// Usage: std::vector numbers = {1, 2, 3, 4, 5}; std::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5]