C ++ collection (2)-standard library type for beginners

Source: Internet
Author: User

C ++ collection (2)-standard library type for beginners

At the beginning of this blog post, I will first introduce the wisdom question in the book: there are 20 bottles of pills, 19 of which have 1g/tablet of pills, and the remaining one has 1.1G/tablet of pills. There is a precise Weighing Balance, just how to find the heavy bottle of pills with a balance?

Okay. Publish the answer directly. Take one pill from the medicine bottle #1, two pills from the medicine bottle #2, three pills from the medicine bottle #3, and so on. If each pill weighs 1 gram, the total weight is 210 grams (1 + 2 +... + 20 = 20*21/2 = 210), the "extra" weight must come from 0.1 grams of pills per tablet. The number of the medicine bottle can be obtained by formula (weight-210 grams)/0.1 grams. Therefore, if the heap pills weigh 211.3 grams, the pill bottle #13 carries a heavier pill.

Have you come up with this method (I don't have it anyway, haha )?

Next, let's go straight to the topic. This article focuses on the standard library type, but it is not comprehensive. Taking the two most common types of string and vector as examples, we will first look at the standard library, for more information, see the subsequent blog.

  Standard Library String type

  • A useful string IO operation: getline. This function accepts two parameters: An input stream object and a string object. The getline function reads data from the next row of the input stream and saves the read content to the string, excluding line breaks. It does not ignore line breaks at the beginning.
  • The string size operation returns a value of the string: size_type type. It is a companion type defined by the string tired type, to make the use of the library type irrelevant to the machine (machine-independent), it is defined as have the same meaning with the unsigned type, and can store the length of a string object that is large enough. Note: The upper-field operator must be added to indicate that the type is defined by the string class. That is, the size () operation returns the string: size_type type, not the int type.
  • When the string object and the string literal value are joined together, at least one of the left and right operands of the + operator must be of the string type.
  • The string type uses the subscript operator ([]) to access a single character in the string object. Its type is size_type.

  Standard Library vector type

  • Vector is not a data type, but a class template, which can be used to define any number of data types.
  • The size () function of the vector member function returns the size_type value defined by the corresponding vector class, and must indicate where the type is defined. The vector type always includes the element type of the vector, such as: vector <int>: size_type.
  • The subscript operation does not add elements. The subscript can only be used to obtain existing elements. Add an element to the vector using push_back ().

Introduction to iterator

  • All the standard library containers define the corresponding iterator type. Only a few containers support subscript operations. Modern C ++ programs prefer to use the iterator.
  • Each container defines the begin and end functions for returning the iterator. Begin returns the first element (if any) pointed to by the iterator. The end operation returns the "next to the end element" of the container ", that is, the "off-the-end-iterator" is used as the Sentinel.

To better understand the image, let's take a simple example: Read a set of Integers to a vector object, calculate the first and last matched elements and output them.

 1     vector<int> ivec; 2     int ival; 3  4     cout << "Enter numbers: " << endl; 5     while (cin >> ival) 6         ivec.push_back(ival); 7  8     if (ivec.size() == 0) 9     {10         cout << "NO Elemnts!" << endl;11         return -1;12     }13 14     cout << "Sum of each pair of counterpart elements in the vector: " << endl;15     16     vector<int>::size_type cnt = 0;17     vector<int>::iterator first, last;18     for (first = ivec.begin, last = ivec.end() - 1; first < last; ++first, --last)19     {20         cout << *first + *last << " ";21         ++cnt;22         if (cnt % 6 == 0)23             cout << endl;24     }25 26     if (first == last)27         cout << endl28         << "The center element is: "29         << ivec[first] << endl;    

 

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.