diff --git a/snippets/cpp/debuging/vector-print.md b/snippets/cpp/debuging/vector-print.md new file mode 100644 index 00000000..9fe8550b --- /dev/null +++ b/snippets/cpp/debuging/vector-print.md @@ -0,0 +1,29 @@ +--- +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 +--- + +```cpp +#include +#include + +template +std::ostream& operator<<(std::ostream& os, const std::vector& vec) { + 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 + } + } + os << "]"; + 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] + +```