积累-destroy

xiaoxiao2021-02-28  80

#pragma once #include <vector> #include <list> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #define SAFE_DELETE(point) do {delete (point); (point) = nullptr;} while(false); template<class T> struct auto_delete { inline static void safe_delete( const T & ) { } }; template<class T> struct auto_delete<T *> { inline static void safe_delete( T* p ) { SAFE_DELETE( p ); } }; template<class T> struct auto_delete<const T *> { inline static void safe_delete( const T* p ) { SAFE_DELETE( p ); } }; template<class T> struct destroyer { inline static void destroy( const T & ) { } }; template<class T> struct destroyer<T *> { inline static void destroy( T* p ) { auto_delete<T *>::safe_delete( p ); } }; template<class T> struct destroyer<const T *> { inline static void destroy( const T* p ) { auto_delete<const T *>::safe_delete( p ); } }; template<class V, class A> struct destroyer<typename std::vector<V, A> > { inline static void destroy( const std::vector<V, A>& v ) { for( typename std::vector<V, A>::const_iterator iter = v.begin(); iter != v.end(); ++iter ) { destroyer<V>::destroy( *iter ); } } }; template<class V, class A> struct destroyer<typename std::list<V, A> > { inline static void destroy( const std::list<V, A>& l ) { for( typename std::list<V, A>::const_iterator iter = l.begin(); iter != l.end(); ++iter ) { destroyer<V>::destroy( *iter ); } } }; template<class K, class P, class A> struct destroyer<typename std::set<K, P, A> > { inline static void destroy( const std::set<K, P, A>& s ) { for( typename std::set<K, P, A>::const_iterator iter = s.begin(); iter != s.end(); ++iter ) { destroyer<K>::destroy( *iter ); } } }; template<class K, class V, class P, class A> struct destroyer<typename std::map<K, V, P, A> > { inline static void destroy( const std::map<K, V, P, A>& m ) { for( typename std::map<K, V, P, A>::const_iterator iter = m.begin(); iter != m.end(); ++iter ) { destroyer<K>::destroy( iter->first ); destroyer<V>::destroy( iter->second ); } } }; template<class K, class V, class P, class A> struct destroyer<typename std::multimap<K, V, P, A> > { inline static void destroy( const std::multimap<K, V, P, A>& mm ) { for( typename std::multimap<K, V, P, A>::const_iterator iter = mm.begin(); iter != mm.end(); ++iter ) { destroyer<K>::destroy( iter->first ); destroyer<V>::destroy( iter->second ); } } }; template< class K, class H, class E, class A > struct destroyer< typename std::unordered_set< K, H, E, A > > { inline static void destroy( const std::unordered_set< K, H, E, A >& us ) { for( typename std::unordered_set<K, H, E, A>::const_iterator iter = us.begin(); iter != us.end(); ++iter ) { destroyer<K>::destroy( *iter ); } } }; template< class K, class V, class H, class E, class A > struct destroyer< typename std::unordered_map< K, V, H, E, A > > { inline static void destroy( const std::unordered_map< K, V, H, E, A >& um ) { for( typename std::unordered_map< K, V, H, E, A >::const_iterator iter = um.begin(); iter != um.end(); ++iter ) { destroyer<K>::destroy( iter->first ); destroyer<V>::destroy( iter->second ); } } }; template<class T> void destroy( const T& v ) { destroyer<T>::destroy( v ); }

自行加入其它需要的类型即可,如果是容器调用过destroy后需要自行处理clear

template<class _Ty> vector<_Ty> split(const char* src, const char* tokens) { std::vector<_Ty> ret; if (!src) return ret; const char* str = src + strspn(src, tokens); while(*str) { const char* brk = strpbrk(str, tokens); if (NULL == brk) { std::istringstream oss(str); std::copy(std::istream_iterator<_Ty>(oss), std::istream_iterator<_Ty>(), std::back_inserter(ret)); break; } std::istringstream oss(string(str, brk)); std::copy(std::istream_iterator<_Ty>(oss), std::istream_iterator<_Ty>(), std::back_inserter(ret)); str = brk + strspn(brk, tokens); } return ret; }
转载请注明原文地址: https://www.6miu.com/read-2622711.html

最新回复(0)