Skip to content

Instantly share code, notes, and snippets.

@lvxejay
Created May 24, 2019 21:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvxejay/bea5555d4f76bfa59a21e2cadecdfe3d to your computer and use it in GitHub Desktop.
Save lvxejay/bea5555d4f76bfa59a21e2cadecdfe3d to your computer and use it in GitHub Desktop.
// 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