1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <iomanip>
#include <iostream>
#include <memory>
#include <vector>
// Some operator magic ----------------------------------------------------------------------------
template <typename T, typename Allocator>
std::vector <T, Allocator> &
operator += ( std::vector <T, Allocator> & a, const std::vector <T, Allocator> & b )
{
a.insert( a.end(), b.begin(), b.end() );
return a;
}
template <typename T, typename Allocator>
std::vector <T, Allocator>
operator + ( const std::vector <T, Allocator> & a, const std::vector <T, Allocator> & b )
{
auto result = a;
return result += b;
}
// Tests ------------------------------------------------------------------------------------------
std::vector <int> f()
{
return { 2, 4, 6, 8 };
}
int main()
{
std::vector <int> xs{ 2, 3, 5, 7, 11 };
std::cout << "xs: "; for (int x : xs) std::cout << " " << x; std::cout << "\n";
std::cout << "xs + f(): "; for (int x : xs + f()) std::cout << " " << x; std::cout << "\n";
std::cout << "xs: "; for (int x : xs) std::cout << " " << x; std::cout << "\n";
std::cout << "xs += f():"; xs += f(); for (int x : xs) std::cout << " " << x; std::cout << "\n";
std::cout << "xs: "; for (int x : xs) std::cout << " " << x; std::cout << "\n";
}
|