C ++ standard library study notes 4-Chapter 6

Source: Internet
Author: User
  • 1. (P145)

Common operation functions of the container class (only a few of them are listed ):

C. max_size ()
Returns the maximum number of elements possible

C1.swap (c2)/Swap (c1, c2)
Swaps the dataC1AndC2

C. begin ()/C. end ()
Returns an iterator for the first element/the position after the last element

C. rbegin ()
Returns a reverse iterator for the first element of a reverse iteration

C. rend ()
Returns a reverse iterator for the position after the last element of a reverse iteration

C. insert (pos, elem)
Inserts a copyElem(Return value and the meaningPosDiffer)

C. erase (beg, end)
Removes all elements of the range[Beg, end)(Some containers return next element not removed)

C. clar ()
Removes all elements (makes the container empty)

  • 2. (P149)

Vector size and capacity)

Size indicates the number of elements in the current container.

Capacity indicates the number of elements that a vector can hold. If this value is exceeded, the internal memory is reconfigured.

Use resize (num) or resize (num, elem) to change the number of elements to num.

Use reserve (num) to change the number of vectors to num.

Capacity> = size

The vector container cannot assign values to a non-existent space. That is to say, the assign () value of the vector can be [begin (), end ). Note that this is the range of size, not the range of capacity.

  • 3. (P158)

Class vector <bool>

It seems that this is not very useful, but it is definitely not good.

Effective C ++: Avoid using vector <bool>

I also found a discussion post on the Internet:

Http://topic.csdn.net/t/20040511/15/3054586.html

  • 4. (P163)

Deque

Most deque and vector are similar. The following are some differences:

1. deque does not provide capacity operations (capacity () and reserve ()).

2. deque directly inserts and deletes header elements (push_front () and pop_front ())

Deque also has a feature. The insertion or deletion of elements may lead to memory reallocation. Therefore, any insertion or deletion will invalidate all pointers, references, and iterator pointing to the deque element. The only exception is to insert an element in the header or tail.

  • 5. (P169)

List

The Special versions of remove () and remove_if () are added in list as their member functions. Therefore, in the face of list, we should call the member function remove () instead of the STL Algorithm for calling like vector <> or deque <>.

Special Modifying Operations)

For the Splice function, pay attention to how to transfer through the linked list, for example:

After the c1.splice (pos, c2) operation, the original c2 is empty.

Take the following example and pay attention to the result.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
// Author: Tanky Woo// Blog:    www.wutianqi.com#include <cstddef> #include <iostream> #include <vector> #include <list> #include <deque> #include <set> #include <algorithm> #include <iterator> using namespace std;  void printLists (const list<int>& l1, const list<int>& l2) {     cout << "list1: ";     copy (l1.begin(), l1.end(), ostream_iterator<int>(cout," "));     cout << endl << "list2: ";     copy (l2.begin(), l2.end(), ostream_iterator<int>(cout," "));     cout << endl << endl; }  int main() {     // create two empty lists     list<int> list1, list2;      // fill both lists with elements     for (int i=0; i<6; ++i) {         list1.push_back(i);         list2.push_front(i);     }     printLists(list1, list2);      // insert all elements of list1 before the first element with value 3 of list2     // – find() returns an iterator to the first element with value 3     list2.splice(find(list2.begin(),list2.end(),  // destination position                       3),                  list1);                          // source list     printLists(list1, list2);     // move first element to the end     list2.splice(list2.end(),        // destination position                  list2,              // source list                  list2.begin());     // source position     printLists(list1, list2);      // sort second list, assign to list1 and remove duplicates     list2.sort();     list1 = list2;     list2.unique();     printLists(list1, list2);      // merge both sorted lists into the first list     list1.merge(list2);     printLists(list1, list2);     system("PAUSE");     return EXIT_SUCCESS;  }

Result:

List1: 0 1 2 3 4 5
List2: 5 4 3 2 1 0

List1:
List2: 5 4 0 1 2 3 4 5 3 2 1 0

List1:
List2: 4 0 1 2 3 4 5 3 2 1 0 5

List1: 0 0 1 1 2 2 3 3 4 5 5
List2: 0 1 2 3 4 5

List1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 5 5 5
List2:

  • 6. (P180)

Set/multiset special search functions

Just as list provides special versions of remove () and remove_if (), set/multiset also provides special search functions, they can execute jobs faster than STL functions with the same name.

Count (elem)
Returns the number of elements with valueElem

Find (elem)
Returns the position of the first element with valueElemOrEnd ()

Lower_bound (elem)
Returns the first position, whereElemWocould get inserted (the first element> = Elem)

Upper_bound (elem)
Returns the last position, whereElemWocould getInserted(The first element> Elem)

Performance_range (elem)
Returns the first and last position, whereElemWocould get inserted (the range of elements= Elem)

  • 7. (P181)

For assign or swap of set/multiset, if the criterion is different, the criterion itself will also be assigned or exchanged.

  • 8. (P183)

Set/multiset does not provide the remove () operation. However, c. erase (elem) can be used to delete the "equal to elem" element.

The insert function of set/multiset can insert only one element at a time.

The Return Value of the insert function of set is a pair type. first indicates the position of the new element or the position of the same element already contained. second indicates whether the insert operation is successful.

1234567891011121314
std::pair<std::set<float>::iterator,bool> status;  //insert value and assign return value status = c.insert(value);  //process return value if (status.second) {     std::cout << value << " inserted as element " } else {     std::cout << value << " already exists as element " } std::cout << std::distance(c.begin().status.first) + 1           << std::endl;
  • 9. (198)

Set/multiset can be considered as a special map/multimap, but the value and key of the set element point to the same object. Therefore, map/multimap has all set/multiset capabilities and all operation functions.

Similar to set/multimap, map/multimap provides special search functions. Note that the parameter of the member function find () Here is the key. That is to say, you cannot use find () to search for elements with a specific value, but you can use find_if (). Count (key), find (key), lower_bound (key), upper_bound (key), 1__range (key) and other parameters are keys.

In map/multimap, the keys of all elements are regarded as constants. Therefore, the actual element type is pair <const key, T>. This restriction is used to ensure that you do not change the order of sorted elements because of changing the key of the elements.

You must be familiar with the subscript operations of map, and the subscript operations get the value directly.

Related Article

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.