Created
May 24, 2019 21:48
-
-
Save lvxejay/bea5555d4f76bfa59a21e2cadecdfe3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector/11335634#11335634 | |
#include <iostream> | |
#include <algorithm> // for copy | |
#include <iterator> // for ostream_iterator | |
#include <vector> | |
int main() { | |
/* Set up vector to hold chars a-z */ | |
std::vector<char> path; | |
for (int ch = 'a'; ch <= 'z'; ++ch) | |
path.push_back(ch); | |
/* Print path vector to console */ | |
std::copy(path.begin(), path.end(), std::ostream_iterator<char>(std::cout, " ")); | |
return 0; | |
} | |
// Overloading osstream operator, applied to the above solution ^ | |
template <typename T> | |
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) { | |
if ( !v.empty() ) { | |
out << '['; | |
std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", ")); | |
out << "\b\b]"; | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment