fork(1) download
  1. /*
  2.   That's how operator<< and >> should've been overloaded.
  3.   Alexandr Poltavsky
  4. */
  5.  
  6. #include <vector>
  7. #include <iostream>
  8.  
  9. template<class T0, class T1> auto& operator<<( std::vector<T0>& vec, T1&& value ) {
  10. vec.push_back( std::forward<T1>( value ) );
  11. return vec;
  12. }
  13.  
  14. template<class T0, class T1> auto& operator>>( std::vector<T0>& vec, T1& value ) {
  15. value = vec.back();
  16. vec.pop_back();
  17. return vec;
  18. }
  19.  
  20. int main() {
  21.  
  22. std::vector<int> v{1,2,3};
  23. int i{};
  24.  
  25. for(auto i : v) std::cout << i <<" ";
  26. std::cout << std::endl;
  27.  
  28. v << 4 << 5;
  29.  
  30. for(auto i : v) std::cout << i <<" ";
  31. std::cout << std::endl;
  32.  
  33. v >> i;
  34. std::cout << "i = " << i << std::endl;
  35.  
  36. for(auto i : v) std::cout << i <<" ";
  37. std::cout << std::endl;
  38.  
  39. }
  40.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1 2 3 
1 2 3 4 5 
i = 5
1 2 3 4