List vector deque differences in STL

Source: Internet
Author: User

STL provides three of the most basic containers: Vector,list,deque.

The vector is similar to the built-in array, which has a contiguous memory space and the starting address is the same, so
It can be very well supported immediately, that is, the [] operator, but because its memory space is continuous, so in the middle
Inserting and deleting will result in a copy of the memory block, and when the memory space behind the array is not enough, you need to re-
Request a piece of memory that is large enough and make a copy of the memory. These have greatly affected the efficiency of vectors.

A list is a doubly linked list in a data structure (according to the SGI STL source code), so its memory space can be discontinuous
Access to data through pointers, which makes it very inefficient to access it immediately, so it
No overloads are provided for the [] operator. But due to the characteristics of the list, it can be very efficient to support the deletion of any place
and insert.

Deque is a double-ended queue, its concrete implementation is not very clear, but know that it has the following two features:
It supports the [] operator, which is supported immediately, and is similar to the efficiency of the vector, which supports both the
Operation: Push_back,push_front,pop_back,pop_front, and the efficiency of the list on both sides of the operation
also similar.

Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following
Principle of:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list
3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.

capacity is the amount of the total number of elements that can be added to a container before the next time the container needs to grow itself.
Containers for continuous storage are related such as vector deque or string. List does not require a capacity capacity () operation

length size refers to the number of elements currently owned by the container in order to get the current length of the container we call its size () operation

The more frequent the dynamic self-growth of vectors, the greater the overhead of element insertion. One solution is to convert vectors to lists when the overhead of vectors becomes very large. Another frequently used scheme is to store complex class objects indirectly through pointers.

The reserve () operation allows the programmer to set the container's capacity to an explicitly specified value, but often results in degraded performance

Empty () operator to check if the container is null

Push_back () It inserts the element at the end of the container

The list and Deque containers also support Push_front (), which inserts new elements into the front of the list. So if the main behavior of the container is to insert elements at the front end deque is more efficient than vector, so we should give preference to deque

Vector IList (list_size); List_size is the container size, each element is initialized to the type default value

Vector IList (list_size,-1); Initialized to List_size int, each of which is-1

In addition to giving the initial length, we can reset the container's length by resize () operation.

Vector IList (list_size);

Vector Ilist2 (IList); Initializes another container object with one container object

Each container supports a set of relational operators that we can use to compare two containers where these relational operators are equal to not equal to less than or greater than or equal to and greater than or equal to

Begin () returns a iterator it points to the first element of a container
End () returns a iterator that points to the next position of the end element of the container

Iterator arithmetic operations apply only to vectors or deque and not to list because the elements of the list are not stored continuously in memory

Vector Svec;
Vector svec2 (Svec.begin (), Svec.end ()); Initializes the SVEC2 with all elements of the Svec
Vector SVEC3 (Svec.begin (), Svec.begin () + svec.size ()/2);//Initialize Svec with the first half of SVEC3

vector< string > vwords (words, words+4); initializes the first element pointer and the pointer to the next position of the last element.

Slist.insert (ITER, spouse);

The first parameter of insert () is a position that points to a position in the container iterator the second parameter is the value to be inserted into the front of the position pointed to by iterator.

Svec.insert (Svec.begin (),//vector, Anna), 10 Anna inserted at the beginning of

Svec.insert (Svec.begin () + svec.size ()/2,sarray+2, sarray+4);//Insert some elements

Svec_two.insert (Svec_two.begin () + svec_two.size ()/2,svec.begin (), Svec.end ());//Insert the elements contained in the Svec, starting from the middle of the svec_two

The general form of removing elements within a container is a pair of erase () methods one removes a single element and another removes the short method of removing the end element of the container by a pair of iterator tags in a range of elements supported by the Pop_back () method

Slist1 = slist2;//Slist1 contains 10 elements, Slist2 contains 24 elements, and after assignment contains 24 elements

Slist1.swap (SLIST2);//With the above difference is that Slist2 now contains a copy of the 10 elements contained in the Slist1 Central Plains

String::npos return value not found by using find ()

String::size_type using Find () to return a value type

Find_first_of () finds the first occurrence that matches any character in the searched string, returns the Size_type type index

Find_first_of (Numerics, POS) find matches any character in the searched string starting from the POS position

SUBSTR () operation generates a copy of a substring of an existing string object its first parameter indicates where to start the second optional parameter indicates the length of the substring

RFind () finds the occurrence of the last specified substring of the right

Find_first_not_of () finds the first character that does not match any character to search for a string

Find_last_of () finds the last character in a string that matches any element of the search string

Find_last_not_of () finds the last character in a string that does not match any word embox the search string

Word.erase (POS, 1); The first parameter of this version of the erase () operation represents the beginning of the character in the string to be deleted the second argument is an optional representation of the number of characters to be deleted

ToLower ((*iter) [POS]); is a standard C library function it accepts an uppercase character and returns the lowercase letter that is equivalent in order to use it I
Must include the header file ToLower ((*iter) [POS]);

The second form of erase () uses a pair of iterators iterator as parameters to mark the range of characters to be deleted, the character pointed by the second iterator does not belong to the range of characters to be deleted, the third form takes only one iterator as a parameter, it marks the character to be deleted.

Assign () and append () string operations they allow us to copy or concatenate parts of a string object sequentially to another string object

Swap () operation to Exchange two string values

The at () operation provides a range check of the index value at run time if the index is valid then at () returns the relevant character element in the same manner as the subscript operator, but if the index is invalid then the at () throws a Out_of_range exception

The Compare () string operation provides a dictionary order of two strings compared to a given s1.compare (S2);
Then compare () returns one of three possible values
1 if S1 is greater than S2, compare () returns a positive value
2 Compare () returns a negative value if S1 is less than S2
3 if S1 equals S2 then compare () returns 0

The replace () operation has two basic formats, which are mainly about how to mark the character set to be replaced in the first two parameters give the index that points to the beginning of the character set and the number of characters to be replaced in the second format passed a pair of iterator respectively
Marks the beginning of the character set and the next position of the last character to be replaced

In general, when we want to know if a value exists, set is the most useful when you want the store to be able to modify a related value when the map is most helpful in both cases, the elements are stored in an ordered relationship to support efficient storage and retrieval

List vector deque differences in STL

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.