Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- namespace std11 {
- template< class T > struct remove_const { typedef T type; };
- template< class T > struct remove_const<const T> { typedef T type; };
- template< class T > struct remove_volatile { typedef T type; };
- template< class T > struct remove_volatile<volatile T> { typedef T type; };
- template< class T >
- struct remove_cv {
- typedef typename std11::remove_volatile<typename std11::remove_const<T>::type>::type type;
- };
- template<class T, T v>
- struct integral_constant {
- static const T value = v;
- typedef T value_type;
- typedef integral_constant type;
- operator value_type() const { return value; }
- const value_type operator()() const { return value; }
- };
- typedef integral_constant<bool, true> true_type;
- typedef integral_constant<bool, false> false_type;
- template<bool B, class T = void>
- struct enable_if {};
- template<class T>
- struct enable_if<true, T> { typedef T type; };
- template< class T > struct is_pointer_helper : false_type {};
- template< class T > struct is_pointer_helper<T*> : true_type {};
- template< class T > struct is_pointer : is_pointer_helper<typename std11::remove_cv<T>::type> {};
- template <typename T>
- typename T::iterator begin(T &c) {
- return c.begin();
- }
- template <typename T>
- const typename T::const_iterator begin(const T &c) {
- return c.begin();
- }
- template <typename T>
- typename T::iterator end(T &c) {
- return c.end();
- }
- template <typename T>
- const typename T::const_iterator end(const T &c) {
- return c.end();
- }
- template <typename T, size_t size>
- T* begin(T(&c)[size]) {
- return &c[0];
- }
- template <typename T, size_t size>
- const T* begin(const T(&c)[size]) {
- return &c[0];
- }
- template <typename T, size_t size>
- const T* end(const T(&c)[size]) {
- return &c[0] + size;
- }
- template <typename T, size_t size>
- T* end(T(&c)[size]) {
- return &c[0] + size;
- }
- } // std11
- // #ifdef keeps us safe from UB
- namespace std {
- #if !(__cplusplus > 1)
- using std11::true_type;
- using std11::false_type;
- using std11::is_pointer;
- using std11::integral_constant;
- using std11::enable_if;
- #endif
- #if (__cplusplus < 199711L) && !defined(_MSC_VER)
- using std11::begin;
- using std11::end;
- #endif
- }
- template <class S, class D>
- typename D::iterator copy_all(const S& src, D& dst, typename D::iterator* =0)
- {
- return std::copy( std::begin(src), std::end(src), std::begin(dst));
- }
- template <class S, class OutIt>
- OutIt copy_all(const S& src, OutIt dstIt, typename OutIt::iterator_category* = 0 )
- {
- return std::copy( std::begin(src), std::end(src), dstIt);
- }
- // unsafe
- template <class S, class D, size_t SN, size_t TN>
- D* copy_all(S (&src)[SN], D (&dst)[TN])
- {
- return std::copy( std::begin(src), std::end(src), std::begin(dst));
- }
- template <class S, class OutIt, size_t SN>
- OutIt copy_all(S (&src)[SN], OutIt dstIt, typename std::enable_if<std::is_pointer<OutIt>::value>::type* = 0)
- {
- std::cerr << __PRETTY_FUNCTION__<<":" << __LINE__<< "\n" << std::begin(src) << "," << std::end(src) << std::endl;
- return std::copy( std::begin(src), std::end(src), dstIt);
- }
- template <class S, class D, size_t SN>
- typename D::iterator
- copy_all(S (&src)[SN], D& dst ) {
- std::cerr << __PRETTY_FUNCTION__<<":" << __LINE__<< "\n" << std::begin(src) << "," << std::end(src) << std::endl;
- return std::copy( std::begin(src), std::end(src), std::begin(dst));
- }
- /**
- printv
- */
- template <class V>
- void printv ( std::string name, V va) {
- std::cout << name << " = { ";
- for (typename V::const_iterator i = std::begin(va), endv = std::end(va); i != endv; ++i)
- std::cout << *i << ' ';
- std::cout << "} \n";
- }
- template <class V, size_t len>
- void printv ( std::string name, V (&va)[len]) {
- std::cout << name << " = { ";
- for (V* i = std::begin(va), *endv = std::end(va); i != endv; ++i)
- std::cout << *i << ' ';
- std::cout << "} \n";
- }
- template <class OutIt>
- void printv ( std::string name, OutIt va, size_t len) {
- std::cout << name << " = { ";
- for (OutIt i = va, endv = va[len]; i != endv; ++i)
- std::cout << *i << ' ';
- std::cout << "} \n";
- }
- int main()
- {
- const int a[4] = {1,2,3,4};
- const int b[4] = {5,6,7,8};
- int c[8];
- std::vector<int> va, vb, vc;
- std::cerr << "Array to vector copy" << std::endl;
- va.resize(std::end(a)-std::begin(a));
- copy_all(a,va);
- vb.resize(std::end(b)-std::begin(b));
- copy_all(b,vb);
- vc.resize(va.size()+vb.size());
- copy_all (vb, copy_all(va,vc));
- //std::vector<int>::iterator vci = copy_all(va,vc);
- //copy_all (vb, vci);
- printv("va", va);
- printv("vb", vb);
- printv("vc", vc);
- std::cerr << "Array copy" << std::endl;
- copy_all(b,
- copy_all(a,c));
- printv("a", a);
- printv("b", b);
- printv("c", c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement