C + + supplements (ii)--First glimpse of standard library types

Source: Internet
Author: User

At the beginning of this blog post, we first introduce a book on the puzzle: there are 20 bottles of pills, of which 19 bottles have 1 grams/tablets of pills, the remaining one bottle has 1.1 grams/pills. There is a weighing precision balance, but how to find the heavier bottle of pills with one balance?

All right, just publish the answer. Take a pill from bottle # # # Two, take out three pills from the bottle # # #, and so on. If each pellet weighs 1 grams, the total weight is 210 grams (1 + 2 + ... + 20 = 20 * 21/2 = 210), and the "extra" weight must come from a pill of more than 0.1 grams per grain. The number of the vial can be derived from the formula (weight-210 grams)/0.1 grams. Therefore, if the pellet weighs 211.3 grams, the vial #13 is loaded with heavier pills.

Have you come up with such a method (I am not, haha)?

Then go straight to the topic, this article focuses on the standard library type, but not exhaustive, only two of the most common types of string and vector for example, the first glimpse of the standard library, the rest of the content see follow-up blog.

  Standard library String type

    • A useful string IO operation: getline. The function accepts two parameters: an input stream object and a string object. The Getline function reads from the next line of the input stream and saves the read content to a string, but does not include a newline character. It does not ignore the beginning of the newline character.
    • The size operation of string returns a value of type String::size_type. It is a matching type defined by the string-tired type (companion type) to achieve the use of the library type as machine-independent (machine-independent), which is defined as having the same meaning as the unsigned type. and can store the length of a string object that is large enough. Note that the domain 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 a mixed join operation is performed on string objects and string literals, at least one of the left and right operands of the + operator must be of type string.
    • The string type accesses a single character in a string object by using the subscript operator ([]), which is of type Size_type.

  Standard library vector type

    • Vector is not a data type, but just a class template that can be used to define any number of data types.
    • The member function of the vector size () returns the value of the size_type 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, and the subscript can only be used to get an existing element. To add an element to the vector, use push_back ().

Introduction to Iterators

    • All standard library containers define the appropriate iterator types, and only a few containers support subscript operations, and modern C + + programs are more inclined to use iterators.
    • Each container defines the begin and end functions for returning iterators. Begin returns the first element pointed to by the iterator, if one exists, and the end operation returns the next "end element of the container", that is, "beyond the End iterator (off-the-end-iterator)", used as a sentry.

For the sake of image understanding as described above, take a simple example: reading a set of integers to a vector object, calculating the first and last paired elements and outputting them.

1vector<int>Ivec;2     intival;3 4cout <<"Enter Numbers:"<<Endl;5      while(Cin >>ival)6 Ivec.push_back (ival);7 8     if(ivec.size () = =0)9     {Tencout <<"NO elemnts!"<<Endl; One         return-1; A     } -  -cout <<"Sum of each pair of counterpart elements in the vector:"<<Endl; the      -vector<int>::size_type cnt =0; -vector<int>:: iterator first, last; -      for(first = Ivec.begin, last = Ivec.end ()-1; First < last; ++first,--Last ) +     { -cout << *first + *last <<" "; +++CNT; A         if(CNT%6==0) atcout <<Endl; -     } -  -     if(First = =Last ) -cout <<Endl -<<"The center element is:" in<< Ivec[first] << Endl;

C + + supplements (ii)--First glimpse of standard library types

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.