📄 vector
字号:
void resize (size_type __new_size, value_type __value = value_type ()) {
if (__new_size > size())
insert(end(), __new_size - size(), __value);
else if (__new_size < size())
erase(begin() + __new_size, end());
}
size_type capacity () const {
return _C_end_of_storage - _C_start;
}
bool empty () const {
return begin() == end();
}
void reserve (size_type __n) {
_RWSTD_REQUIRES (__n <= max_size (),
(_RWSTD_ERROR_LENGTH_ERROR,
_RWSTD_FUNC ("vector::reserve(size_type)"),
__n, max_size ()));
if (capacity() < __n) {
size_t __new_capacity =
max (__n, (size_t)_RW::__rw_new_capacity(size(),this));
pointer __tmp =
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
allocate(__new_capacity,_C_start));
_TRY {
uninitialized_copy(begin(), end(), _C_make_iter (__tmp),
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_RWSTD_VALUE_ALLOC(_C_value_alloc_type, deallocate(__tmp,__n));
_RETHROW;
}
_C_destroy(begin (), end ());
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
deallocate(_C_start, _C_end_of_storage - _C_start));
_C_finish = __tmp + size();
_C_start = __tmp;
_C_end_of_storage = _C_start + __new_capacity;
}
}
//
// Element access.
//
reference operator[] (size_type __n) {
#ifdef _RWSTD_BOUNDS_CHECKING
_RWSTD_REQUIRES (__n < size (),
(_RWSTD_ERROR_OUT_OF_RANGE,
_RWSTD_FUNC ("vector::operator[](size_type)"),
__n, size ()));
#endif // _RWSTD_BOUNDS_CHECKING
return *(begin() + __n);
}
const_reference operator[] (size_type __n) const {
#ifdef _RWSTD_BOUNDS_CHECKING
_RWSTD_REQUIRES (__n < size (),
(_RWSTD_ERROR_OUT_OF_RANGE,
_RWSTD_FUNC ("vector::operator[](size_type) const"),
__n, size ()));
#endif // _RWSTD_BOUNDS_CHECKING
return *(begin() + __n);
}
reference at (size_type __n) {
_RWSTD_REQUIRES (__n < size (),
(_RWSTD_ERROR_OUT_OF_RANGE,
_RWSTD_FUNC ("vector::at (size_type)"),
__n, size ()));
return *(begin() + __n);
}
const_reference at (size_type __n) const {
_RWSTD_REQUIRES (__n < size (),
(_RWSTD_ERROR_OUT_OF_RANGE,
_RWSTD_FUNC ("vector::at(size_type) const"),
__n, size ()));
return *(begin() + __n);
}
reference front () {
return *begin();
}
const_reference front () const {
return *begin();
}
reference back () {
return *(end() - 1);
}
const_reference back () const {
return *(end() - 1);
}
//
// Modifiers.
//
void push_back (const_reference __x) {
if (_C_finish != _C_end_of_storage) {
++_C_finish;
_TRY {
_RWSTD_VALUE_ALLOC (_C_value_alloc_type,
construct (_C_finish - difference_type (1),
__x));
}
_CATCH (...) {
--_C_finish;
}
}
else
_C_insert_aux(end(), __x);
}
void pop_back()
{
_RWSTD_VALUE_ALLOC(_C_value_alloc_type, destroy(_C_finish-1));
--_C_finish;
}
//
// Insert x at __position.
//
iterator insert (iterator __position, const_reference __x) {
size_type __n = __position - begin();
if (_C_finish != _C_end_of_storage && __position == end()) {
++_C_finish;
_TRY {
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
construct(_C_finish - 1, __x));
}
_CATCH (...) {
--_C_finish;
_RETHROW;
}
}
else
_C_insert_aux(__position, __x);
return begin() + __n;
}
#ifndef _RWSTD_NO_MEMBER_TEMPLATES
template <class _InputIter>
void insert (iterator __pos, _InputIter __first, _InputIter __last) {
insert (__pos, __first, __last, _RWSTD_DISPATCH (_InputIter));
}
template <class _InputIter>
void insert (iterator __pos, _InputIter __first, _InputIter __last,
_RWSTD_DISPATCH_INT (false)) {
// unnamed arg is used for overload resolution
// _RWSTD_COMPILE_ASSERT (sizeof (*__first));
_C_insert_interval_dispatch (__pos, __first, __last,
_RWSTD_ITERATOR_CATEGORY (_InputIter,
__first));
}
void insert (iterator __pos, size_type __n, const_reference __val,
_RWSTD_DISPATCH_INT (true)) {
// unnamed arg is used for overload resolution
_C_insert_aux (__pos, __n, __val);
}
void insert (iterator __position, size_type __n, const_reference __value) {
_C_insert_aux(__position,__n,__value);
}
#else
void insert (iterator __position, size_type __n, const_reference __x) {
_C_insert_aux(__position,__n,__x);
}
void insert (iterator __position, const_iterator __first,
const_iterator __last) {
_C_insert_aux2(__position, __first, __last);
}
#endif // _RWSTD_NO_MEMBER_TEMPLATES
iterator erase (iterator __position) {
if (!empty ()) {
if (__position + 1 != end())
copy(__position + 1, end(), __position);
_RWSTD_VALUE_ALLOC(_C_value_alloc_type, destroy(_C_finish - 1));
--_C_finish;
}
return __position;
}
iterator erase (iterator __first, iterator __last) {
if (!empty ()) {
iterator __i = copy (__last, end(), __first);
iterator __tmp = end ();
_C_destroy(__i, __tmp);
_C_finish -= (__last - __first);
}
return __first;
}
void swap (vector& __x) {
if((_C_value_alloc_type)*this==(_C_value_alloc_type)__x) {
_STD::swap (_C_start, __x._C_start);
_STD::swap (_C_finish, __x._C_finish);
_STD::swap (_C_end_of_storage, __x._C_end_of_storage);
}
else {
vector __v = *this;
*this = __x;
__x=__v;
}
}
void clear () {
erase (begin (), end ());
}
};
template <class _TypeT, class _Allocator>
inline bool operator== (const vector<_TypeT,_Allocator>& __x,
const vector<_TypeT,_Allocator>& __y)
{
return __x.size() == __y.size()
&& equal(__x.begin(), __x.end(), __y.begin());
}
template <class _TypeT, class _Allocator>
inline bool operator< (const vector<_TypeT,_Allocator>& __x,
const vector<_TypeT,_Allocator>& __y)
{
return lexicographical_compare(__x.begin(), __x.end(),
__y.begin(), __y.end());
}
template <class _TypeT, class _Allocator>
inline bool operator!= (const vector<_TypeT,_Allocator>& __x,
const vector<_TypeT,_Allocator>& __y)
{
return !(__x == __y);
}
template <class _TypeT, class _Allocator>
inline bool operator> (const vector<_TypeT,_Allocator>& __x,
const vector<_TypeT,_Allocator>& __y)
{
return __y < __x;
}
template <class _TypeT, class _Allocator>
inline bool operator>= (const vector<_TypeT,_Allocator>& __x,
const vector<_TypeT,_Allocator>& __y)
{
return !(__x < __y);
}
template <class _TypeT, class _Allocator>
inline bool operator<= (const vector<_TypeT,_Allocator>& __x,
const vector<_TypeT,_Allocator>& __y)
{
return !(__y < __x);
}
#ifndef _RWSTD_NO_PART_SPEC_OVERLOAD
template <class _TypeT, class _Allocator>
inline void swap(vector<_TypeT,_Allocator>& __a, vector<_TypeT,_Allocator>& __b)
{
__a.swap(__b);
}
#endif // _RWSTD_NO_PART_SPEC_OVERLOAD
#ifndef _RWSTD_NO_VECTOR_BOOL
#ifndef _RWSTD_NO_BOOL
#ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
template <class _Allocator>
#else // if defined (_RWSTD_NO_CLASS_PARTIAL_SPEC)
// use a macro to mutate _Allocator into allocator<bool>
# define _Allocator allocator<bool>
_RWSTD_SPECIALIZED_CLASS
#endif // _RWSTD_NO_CLASS_PARTIAL_SPEC
class _RWSTD_EXPORT vector<bool, _Allocator > : private _Allocator
{
typedef _RWSTD_REBIND(_Allocator, unsigned int) _C_value_alloc_type;
typedef vector _C_self;
public:
typedef _Allocator allocator_type;
typedef bool value_type;
typedef _TYPENAME allocator_type::size_type size_type;
typedef _TYPENAME allocator_type::difference_type difference_type;
typedef _TYPENAME _C_value_alloc_type::pointer pointer;
typedef _TYPENAME _C_value_alloc_type::const_pointer const_pointer;
class iterator;
class const_iterator;
class reference {
friend class iterator;
friend class const_iterator;
protected:
unsigned int* _C_p;
unsigned int _C_mask;
reference (unsigned int* __x, unsigned int __y)
: _C_p (__x), _C_mask (__y) { }
public:
reference () : _C_p(0), _C_mask(0) {}
operator bool () const {
return !!(*_C_p & _C_mask);
}
reference& operator= (bool __x) {
if (__x)
*_C_p |= _C_mask;
else
*_C_p &= ~_C_mask;
return *this;
}
reference& operator= (const reference& __x) {
return *this = bool(__x);
}
#ifndef _RWSTD_NO_EXT_VECTOR_BOOL_REF_OPS
bool operator== (const reference& __x) const {
return bool(*this) == bool(__x);
}
bool operator< (const reference& __x) const {
#ifndef _MSC_VER
return bool(*this) < bool(__x);
#else
return int(*this) < int(__x);
#endif
}
bool operator!= (const reference& __x) const {
return !(*this == __x);
}
bool operator> (const reference& __x) const {
return __x < *this;
}
bool operator>= (const reference& __x) const {
return !(*this < __x);
}
bool operator<= (const reference& __x) const {
return !(*this > __x);
}
#endif // _RWSTD_NO_EXT_VECTOR_BOOL_REF_OPS
void flip () {
*_C_p ^= _C_mask;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -