Qvector默认使用隐式共享,可以用setSharable改变其隐式共享。使用non-const操作和函数将引起深拷贝。at()比operator[](),快,因为它不进行深拷贝.Qvector取值都会检查越界问题。
看看简单的例子:
QVector<int> vecA;
QVector<int> vecB;
vecA.push_back(1);
vecA.push_back(10);
vecB= vecA;
cout<<"&vecA.at(0) : "<<&vecA.at(0)< cout<<"&vecB.at(0) : "<<&vecB.at(0)< QVector<int> vecC; vecA.setSharable(false); vecC = vecA; cout<<"&vecA.at(0): "<<&vecA.at(0)< cout<<"&vecC.at(0): "<<&vecC.at(0)< 对比发现,禁用了隐式共享之后,元素的地址就不再一样了。 Vector Vector没有隐式共享,operator [ ]不检查越界,at()才检查越界。 Vector的构造函数C++98版: C++11版: Qvector的构造函数: QVector::QVector () QVector::QVector ( int size ) QVector::QVector ( int size, const T & value ) QVector::QVector ( const QVector QVector::QVector ( std::initializer_list QVector<int> second (4,100); QVector<int> third (second.begin(),second.end()); int myints[] = {16,2,77,29}; QVector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );构造函数:
explicit vector (const allocator_type& alloc = allocator_type());
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
template
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
vector (const vector& x);
explicit vector (const allocator_type& alloc = allocator_type());
explicit vector (size_type n);
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
template
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
vector (initializer_list
const allocator_type& alloc = allocator_type());
通过比较我们发现,vector可以指定内存分配器,而且Qvector少了类似template
vector (InputIterator first, InputIterator last,构造函数。所以以下代码是肯定不行的:
迭代器:
Vector迭代器begin C++98版本:
iterator begin();
const_iterator begin() const;
C++11版:
iterator begin() noexcept;
const_iterator begin() const noexcept;
noexcept指定这两个函数是不能抛出异常的。
Vector迭代器end C++98版本:
iterator end();
const_iterator end() const;
C++11版:
iterator end() noexcept;
const_iterator end() const noexcept;
Vector迭代器rbegin C++98版本:
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
C++11版:
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
noexcept指定这两个函数是不能抛出异常的。
Vector迭代器rend C++98版本:
reverse_iterator rend();
const_reverse_iterator rend() const
C++11版:
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
常量迭代器只要C++11版本的:
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
Qvector的迭代器:
iterator
begin ()
const_iterator
begin () const
const_iterator
constBegin () const
const_iterator
constEnd () const
iterator
end ()
const_iterator
end () const
Qvector没有反向迭代器。
Vector有而Qvector没有的函数或功能:
size_type max_size() const;
返回vector可以存在最大元素个数。
void shrink_to_fit(); 这个函数是C++11独有的
使得vector减少其容量为适合大小的容量。
赋值函数:
C++98:
template
void assign (InputIterator first, InputIterator last);
void assign (size_type n, const value_type& val);
C++11:
template
void assign (InputIterator first, InputIterator last);
void assign (size_type n, const value_type& val);
void assign (initializer_list
下面两个函数其实跟insert差不多
template
iterator emplace (const_iterator position, Args&&... args);
template
void emplace_back (Args&&... args);
Qvector有而vector没有的函数:
int QVector::count ( const T & value ) const
返回Qvector中值为value的个数。
bool QVector::contains ( const T & value ) const
判断Qvector中是否包含元素value,要求Qvector中的类型必须支持 比较操作==
bool QVector::endsWith ( const T & value ) const
判断Qvector中是否以value值结尾。
int QVector::indexOf ( const T & value, int from = 0 ) const
int QVector::lastIndexOf ( const T & value, int from = -1 ) const
QVector
void QVector::squeeze ()
该函数释放不用的内存,类似于vector的void shrink_to_fit()
bool QVector::startsWith ( const T & value ) const
QList
std::vector
QVector
QVector
bool QVector::operator!= ( const QVector
QVector
QVector
QVector
QVector
QVector
QVector
bool QVector::operator== ( const QVector