Skip to content

Instantly share code, notes, and snippets.

@cleoold
Last active August 26, 2020 00:10
Show Gist options
  • Save cleoold/27e6b1b077c5dac36009eb4cd3c45347 to your computer and use it in GitHub Desktop.
Save cleoold/27e6b1b077c5dac36009eb4cd3c45347 to your computer and use it in GitHub Desktop.
dirty convinient std::vector inserter
#pragma once
#include <vector>
// inspiration: boost/assign/std/vector.hpp
template<class VT> struct vector_inserter {
VT &vec;
template<class Arg> auto &operator,(Arg &&v) {
vec.emplace_back(std::forward<Arg>(v));
return *this;
}
};
template<class T, class Alloc, class Arg>
auto operator+=(std::vector<T, Alloc> &vec, Arg &&v) {
vec.emplace_back(std::forward<Arg>(v));
return vector_inserter<std::vector<T, Alloc>>{vec};
}
#include <iostream>
#include <iterator>
#include "vector_inserter.hpp"
int main() {
std::vector<int> v;
v += 1,2,5,8,1,9,11,'a';
std::copy(v.begin(), v.end(), std::ostream_iterator<decltype(v)::value_type>(std::cout, " "));
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment