Last active
August 26, 2020 00:10
-
-
Save cleoold/27e6b1b077c5dac36009eb4cd3c45347 to your computer and use it in GitHub Desktop.
dirty convinient std::vector inserter
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
#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}; | |
} |
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
#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