Skip to content

Commit 22fd73a

Browse files
Update vector-print-utility.md
reduced comments and added examples
1 parent edf11fc commit 22fd73a

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

snippets/cpp/basics/vector-print-utility.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
title: std::vector Print Utility
33
description: Overloads the << operator to print the contents of a vector just like in python.
44
author: Mohamed-faaris
5-
tags: cpp,printing,vector,utility
5+
tags: cpp,printing,debuging,vector,utility
66
---
77

88
```cpp
9-
#include <iostream> // Includes the input/output stream library
10-
#include <vector> // Includes the vector container
9+
#include <iostream>
10+
#include <vector>
1111

1212
template <typename T>
1313
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
14-
os << "["; // Begin vector formatting with an opening bracket
14+
os << "[";
1515
for (size_t i = 0; i < vec.size(); ++i) {
1616
os << vec[i]; // Print each vector element
1717
if (i != vec.size() - 1) {
18-
os << ", "; // Add separator between elements except after the last one
18+
os << ", "; // Add separator
1919
}
2020
}
21-
os << "]"; // Close vector formatting with a closing bracket
22-
return os; // Return the stream to enable chaining
21+
os << "]";
22+
return os; // Return the stream
2323
}
24+
25+
//std::vector<int> numbers = {1, 2, 3, 4, 5};
26+
//std::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5]
27+
2428
```

0 commit comments

Comments
 (0)