Comparison of Qvector and vectors (Qvector is implicitly shared by default, and there are more functions available)

Source: Internet
Author: User

comparison of Qvector and vectors:

Qvector uses implicit sharing by default and can change its implicit sharing with setsharable. Using NON-CONST operations and functions will cause deep copies. at () than operator[] (), fast because it does not make deep copies. The Qvector value checks for out-of-bounds issues.

Take a look at the simple example:

Qvector<int> Veca;

Qvector<int> VECB;

Veca.push_back (1);

Veca.push_back (10);

Vecb= Veca;

cout<< "&veca.at (0):" <<&veca.at (0) <<endl;

cout<< "&vecb.at (0):" <<&vecb.at (0) <<endl;

Qvector<int> VECC;

Veca.setsharable (FALSE);

VECC = Veca;

cout<< "&veca.at (0):" <<&veca.at (0) <<endl;

cout<< "&vecc.at (0):" <<&vecc.at (0) <<endl;

The comparison found that when implicit sharing was disabled, the address of the element was no longer the same.

Vector

Vector is not implicitly shared, operator [] does not check out of bounds, and at () checks out of bounds.

constructor Function:

Vector's constructor c++98 version:

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 <class inputiterator>
Vector (Inputiterator First, Inputiterator last,
Const allocator_type& alloc = Allocator_type ());
Vector (const vector& x);

Version c++11:

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 <class inputiterator>
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<value_type> il,
Const allocator_type& alloc = Allocator_type ());

Qvector constructor:

Qvector::qvector ()

Qvector::qvector (int size)

Qvector::qvector (int size, const T & value)

Qvector::qvector (const qvector<t> & Other)

Qvector::qvector (std::initializer_list<t> args)

By comparison we found that vector can specify a memory allocator, and qvector less like template <class inputiterator>
Vector (Inputiterator First, Inputiterator last, constructor. So the following code is definitely not possible:

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));

Iterators:
Vector iterator begin c++98 Version:
Iterator begin ();
Const_iterator begin () const;
Version c++11:
Iterator begin () noexcept;
Const_iterator begin () const noexcept;
NOEXCEPT specifies that the two functions cannot throw an exception.
Vector iterator End c++98 version:
Iterator End ();
Const_iterator end () const;
Version c++11:
Iterator end () noexcept;
Const_iterator End () const noexcept;
Vector iterator Rbegin c++98 version:
Reverse_iterator Rbegin ();
Const_reverse_iterator rbegin () const;
Version c++11:
Reverse_iterator Rbegin () noexcept;
Const_reverse_iterator rbegin () const noexcept;
NOEXCEPT specifies that the two functions cannot throw an exception.
Vector iterator rend c++98 version:
Reverse_iterator rend ();
Const_reverse_iterator rend () const
Version c++11:
Reverse_iterator rend () noexcept;
Const_reverse_iterator rend () const noexcept;
Constant iterators as long as the c++11 version of:
Const_iterator cbegin () const noexcept;
Const_iterator cend () const noexcept;
Const_reverse_iterator crbegin () const noexcept;
Const_reverse_iterator crend () const noexcept;
Qvector iterators:
Iterator
Begin ()
Const_iterator
Begin () const
Const_iterator
Constbegin () const
Const_iterator
Constend () const
Iterator
End ()
Const_iterator
End () const
The qvector does not have a reverse iterator.
A function or function in which the vector has no qvector:
Size_type max_size () const;
Returns the maximum number of elements that can exist in a vector.
void Shrink_to_fit (); This function is c++11 alone.
Allows vectors to reduce their capacity to fit the size of the capacity.
Assignment function:
C++98:
Template <class inputiterator>
void Assign (Inputiterator first, Inputiterator last);
void Assign (size_type N, const value_type& val);
C++11:
Template <class inputiterator>
void Assign (Inputiterator first, Inputiterator last);
void Assign (size_type N, const value_type& val);
void assign (initializer_list<value_type> il);
The following two functions are actually similar to insert
Template <class ... Args>
Iterator Emplace (const_iterator position, args&& .... Args);
Template <class ... Args>
void Emplace_back (Args&& .... Args);
Qvector has a function that the vector does not have:
int Qvector::count (const T & value) const
Returns the number of values in Qvector that are value.
BOOL Qvector::contains (const T & value) const
Determine if the qvector contains element value, requiring that the type in the Qvector must support the comparison operation = =
BOOL Qvector::endswith (const T & value) const
Determines whether the qvector ends with value.
int Qvector::indexof (const T & value, int from = 0) const
int Qvector::lastindexof (const T & value, int from =-1) const
qvector<t> qvector::mid (int pos, int length =-1) const
void Qvector::squeeze ()
This function frees unused memory, similar to the vector of void Shrink_to_fit ()
BOOL Qvector::startswith (const T & value) const
Qlist<t> qvector::tolist () const
Std::vector<t> Qvector::tostdvector () const
Qvector<t> qvector::fromlist (const qlist<t> & list) [Static]
Qvector<t> qvector::fromstdvector (const std::vector<t> & vector) [Static]
BOOL Qvector::operator!= (const qvector<t> & other) const
Qvector<t> qvector::operator+ (const qvector<t> & other) const
Qvector<t> & qvector::operator+= (const qvector<t> & Other)
Qvector<t> & qvector::operator+= (const T & value)
Qvector<t> & qvector::operator<< (const T & value)
Qvector<t> & qvector::operator<< (const qvector<t> & Other)
Qvector<t> & qvector::operator= (const qvector<t> & Other)
BOOL qvector::operator== (const qvector<t> & other) const

http://blog.csdn.net/hai200501019/article/details/11713519

Comparison of Qvector and vectors (Qvector is implicitly shared by default, and there are more functions available)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.