📄 vector
字号:
resize(size() + n, x); for(size_type i = elements -1; (i > (index+n-1)); --i){ data[i] = data[i-n]; } for(size_type i = 0; i < n; i++){ data[i + index] = x; } } template <class InputIterator> _UCXXEXPORT void _insert_from_iterator(iterator position, InputIterator first, InputIterator last) { if (ispod && position==end()){ size_t sz=last-first; reserve(size()+sz); memcpy(end(),first,sz*sizeof(T)); elements+=sz; }else{ while(first !=last){ T temp = *first; position = insert(position, temp); ++position; ++first; } } } template <class InputIterator> inline void _dispatch_insert(iterator position, InputIterator first, InputIterator last, __true_type) { _insert_fill(position, first, last); } template <class InputIterator> inline void _dispatch_insert(iterator position, InputIterator first, InputIterator last, __false_type) { _insert_from_iterator(position, first, last); } inline void insert(iterator position, size_type n, const T& x ){ _insert_fill(position, n, x); } template <class InputIterator> inline void insert(iterator position, InputIterator first, InputIterator last){ typedef typename __is_integer<InputIterator>::value __some_type; _dispatch_insert(position, first, last, __some_type()); } _UCXXEXPORT iterator erase(iterator position){ size_type index = position - data; if (ispod){ if (index<elements-1) memmove(data+index,data+index+1,((elements-1)-index)*sizeof(T)); } else for(size_type i = index; i < (elements - 1); ++i){ data[i] = data[i+1]; } downsize(size() - 1); return (data + index); } _UCXXEXPORT iterator erase(iterator first, iterator last){ size_type index = first - data; size_type width = last - first; if (ispod){ if (index<elements-width) memmove(data+index,data+index+width,((elements-width)-index)*sizeof(T)); } else for(size_type i = index; i < (elements - width) ;++i){ data[i] = data[i+width]; } downsize(size() - width); return (data + index); } _UCXXEXPORT void swap(vector<T,Allocator>& v){ if(this == &v){ //Avoid dv.swap(v) return; } T* ptr; size_type temp; //Swap pointers first ptr = data; data = v.data; v.data = ptr; //Swap element counts temp = elements; elements = v.elements; v.elements = temp; //Swap data size temp = data_size; data_size = v.data_size; v.data_size = temp; } _UCXXEXPORT void clear(){ elements = 0; } protected: T* data; size_type data_size; size_type elements; Allocator a; }; //Here go template instantiations template<class T, class Allocator> _UCXXEXPORT vector<T, Allocator>::~vector(){ for(size_t i = 0; i < elements; ++i){ a.destroy(data + i); } a.deallocate(data, data_size); data = 0; elements = 0; data_size=0; } template<class T, class Allocator> _UCXXEXPORT void vector<T, Allocator>::reserve(size_type n){ if (!allocator_traits<Allocator>::is_static) { if(n > data_size){ //We never shrink... T * temp_ptr = data; size_type temp_size = data_size; data_size = n+n/2;//__UCLIBCXX_STL_BUFFER_SIZE__; data = a.allocate(data_size); if (ispod) memcpy(data,temp_ptr,elements*sizeof(T)); else for(size_type i = 0; i<elements; ++i){ a.construct(data+i, temp_ptr[i]); a.destroy(temp_ptr+i); } a.deallocate(temp_ptr, temp_size); } } else data_size = n; } template<class T, class Allocator> _UCXXEXPORT void vector<T, Allocator>::resize(size_type sz, const T & c){ if(sz > elements){ //Need to actually call constructor reserve(sz); for(size_type i = elements; i<sz ; ++i){ a.construct(data+i, c); } elements = sz; }else{ downsize(sz); } } template<class T, class Allocator> _UCXXEXPORT void vector<T, Allocator>::downsize(size_type sz){ if(sz < elements){ //Actually are downsizing for(size_t i = sz; i< elements; ++i){ a.destroy(data+i); } elements = sz; } }#ifndef __UCLIBCXX_COMPILE_VECTOR__#ifdef __UCLIBCXX_EXPAND_VECTOR_BASIC__#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__ template<> _UCXXEXPORT vector<char, allocator<char> >::vector(const allocator<char>& al); template<> _UCXXEXPORT vector<char, allocator<char> >::vector(size_type n, const char & value, const allocator<char> & al); template<> _UCXXEXPORT vector<char, allocator<char> >::~vector(); template<> _UCXXEXPORT vector<unsigned char, allocator<unsigned char> >::~vector();#endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__ template<> _UCXXEXPORT void vector<char, allocator<char> >::reserve(size_type n); template<> _UCXXEXPORT void vector<unsigned char, allocator<unsigned char> >::reserve(size_type n); template<> _UCXXEXPORT void vector<short int, allocator<short int> >::reserve(size_type n); template<> _UCXXEXPORT void vector<unsigned short int, allocator<unsigned short int> >::reserve(size_type n); template<> _UCXXEXPORT void vector<int, allocator<int> >::reserve(size_type n); template<> _UCXXEXPORT void vector<unsigned int, allocator<unsigned int> >::reserve(size_type n); template<> _UCXXEXPORT void vector<long int, allocator<long int> >::reserve(size_type n); template<> _UCXXEXPORT void vector<unsigned long int, allocator<unsigned long int> >::reserve(size_type n); template<> _UCXXEXPORT void vector<float, allocator<float> >::reserve(size_type n); template<> _UCXXEXPORT void vector<double, allocator<double> >::reserve(size_type n); template<> _UCXXEXPORT void vector<bool, allocator<bool> >::reserve(size_type n); template<> _UCXXEXPORT void vector<char, allocator<char> >::resize(size_type sz, const char & c); template<> _UCXXEXPORT void vector<unsigned char, allocator<unsigned char> >::resize(size_type sz, const unsigned char & c); template<> _UCXXEXPORT void vector<short int, allocator<short int> >::resize(size_type sz, const short & c); template<> _UCXXEXPORT void vector<unsigned short int, allocator<unsigned short int> >::resize(size_type sz, const unsigned short int & c); template<> _UCXXEXPORT void vector<int, allocator<int> >::resize(size_type sz, const int & c); template<> _UCXXEXPORT void vector<unsigned int, allocator<unsigned int> >::resize(size_type sz, const unsigned int & c); template<> _UCXXEXPORT void vector<long int, allocator<long int> >::resize(size_type sz, const long int & c); template<> _UCXXEXPORT void vector<unsigned long int, allocator<unsigned long int> >::resize(size_type sz, const unsigned long int & c); template<> _UCXXEXPORT void vector<float, allocator<float> >::resize(size_type sz, const float & c); template<> _UCXXEXPORT void vector<double, allocator<double> >::resize(size_type sz, const double & c); template<> _UCXXEXPORT void vector<bool, allocator<bool> >::resize(size_type sz, const bool & c);#elif defined __UCLIBCXX_EXPAND_STRING_CHAR__#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__ template<> _UCXXEXPORT vector<char, allocator<char> >::vector(const allocator<char>& al); template<> _UCXXEXPORT vector<char, allocator<char> >::vector(size_type n, const char & value, const allocator<char> & al); template<> _UCXXEXPORT vector<char, allocator<char> >::~vector();#endif template<> _UCXXEXPORT void vector<char, allocator<char> >::reserve(size_type n); template<> _UCXXEXPORT void vector<char, allocator<char> >::resize(size_type sz, const char & c);#endif#endif template <class T, class Allocator> _UCXXEXPORT bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y) { if(x.size() !=y.size() ){ return false; } for(size_t i = 0; i < x.size(); ++i){ if(x[i] != y[i]){ return false; } } return true; } template <class T, class Allocator> _UCXXEXPORT bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y) { less<typename iterator_traits<typename vector<T,Allocator>::iterator >::value_type> c; return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), c); } template <class T, class Allocator> _UCXXEXPORT bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y) { return !(x == y); } template <class T, class Allocator> _UCXXEXPORT bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y) { greater<typename iterator_traits<typename vector<T,Allocator>::iterator >::value_type> c; return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), c); } template <class T, class Allocator> _UCXXEXPORT bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y) { greater_equal<typename iterator_traits<typename vector<T,Allocator>::iterator >::value_type> c; return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), c); } template <class T, class Allocator> _UCXXEXPORT bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y) { less_equal<typename iterator_traits<typename vector<T,Allocator>::iterator >::value_type> c; return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), c); } template <class T, class Allocator> _UCXXEXPORT void swap(vector<T,Allocator>& x, vector<T,Allocator>& y){ x.swap(y); }}#pragma warning(pop)#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -