Chapter 3 Standard Library types

Source: Internet
Author: User
Tags bitset

Chapter 2

1. to be compatible with the C language, the compiler automatically adds an empty character to the end of all character strings in C ++.

2. Reference is a composite type, which is defined by adding the "&" symbol before the variable name. A composite type is a type defined by another type. The reference must be initialized with an object of the same type as the reference.

Because the reference is just another name of the object it binds, all operations on the reference actually work on the object it binds.

After the reference is initialized, as long as the reference exists, it is bound to the object pointed to during initialization. It is impossible to bind a reference to another object.

Initialization is the only method that specifies the object to which the reference is directed. A const reference is a reference to a const object. Const reference can be initialized to different types of objects or initialized to the right value.

A non-const reference can only be bound to an object of the same type as the reference. Const reference can be bound to different but related types of objects or bound to the right value.

 

Chapter 3

1.Warning: Standard Library string type and string literal value

For historical reasons and for compatibility with the C language, the string literal value is not of the same type as the string type in the standard library.

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.

Suggestion: Use the C ++ version of the header file of the C standard library.

In addition to defining some facilities specific to C ++, the C ++ standard library also includes the C standard library. The header file cctype of C ++ Zhong is actually the C standard library functions used. These library functions are defined in the ctype. h header file of the C standard library.

The header file of the C standard library is named name. h, while the C ++ version is named cname. C is added before the header file with the suffix. h missing. C indicates that the header file is from the C standard library. Therefore, the content of cctype is the same as that of ctype. h, but it adopts a form that is more suitable for C ++ programs. In particular, the names defined in the cname header file are all defined in the namespace STD, but not in the. h version.

2. vector is not a data type, but a class template. It can be used to define any number of data types. Each element of the vector type specifies the type of elements it saves. Therefore, both vector <int> and vector <string> are data types.

An important attribute of a vector object (and other standard library container objects) is that it can efficiently add elements at runtime. Because the growth efficiency of vector is high, when the element value is known, it is best to dynamically add elements to it to let it grow.

The vector type always includes the element type of the vector. Vector <int >:: size_type (true); vector: size_type (false)

Only an existing element can be referenced by the subscript operator. When values are assigned through subscript operations, no elements are added.

3. The iterator is a data type that checks elements in a container and traverses elements.

All the standard library containers define the corresponding iterator type, and only a few containers support subscript operations.

Another pair of operations that can be executed on the iterator is comparison: Use = or! = Operator to compare two iterators. If the two iterator objects point to the same element, they are equal. Otherwise, they are not equal.

Each container type also defines a type named const_iterator, which can only be used to read elements in the container, but cannot change its value. When the const_iterator type is unreferenced, a const value is returned. Const_iterator is not allowed to assign values.

Do not confuse the const_iterator object with the const iterator object. When declaring a const iterator, The iterator must be initialized. Once initialized, its value cannot be changed.

Iterator arithmetic operation: the result after addition or subtraction must point to an element in the vector indicated by ITER, or the last element at the end of the vector.

Any operation to change the length of a vector will fail because an existing iterator is invalid. For example, after push_back is called, you can no longer trust the value of the iterator pointing to the vector.

4. The bitset constructor is similar to a vector. The bitset class is a type template. Unlike a vector, the bitset type object only differs in its length rather than in its type.

The bit string starting with 0 is a low-level bit.

When a bitset object is initialized with a string object, the string object is expressed as a bit. The order in which the bitset is read from the string object is from right to left.

Chapter 4

1. Generally, a C-style string has the same data type as a string literal and ends with a null character,Therefore, the C-style string can be used in any place where the string literal value can be used:

* You can use a C-style string to initialize or assign values to a string object.

* The string type addition operation requires two operands. You can use a C-style string as one of the operands, or use a C-style string as the right-hungry operand for the composite value assignment operation.

Otherwise, the string type object of the standard library cannot be directly used where the C-style string is required. Char * STR = st2; (false)


String type provides a member function named c_str to meet our requirements: char * STR = st2.c _ STR (); (true)

The c_str function returns a C-style string, which literally means: "returns the expression of a C-style string", that is, returns a pointer to the first address of the character array, this array stores the same content as the string object and ends with the end character null. The pointer returned by c_str points to an array of the const char type.

2. Use arrays to initialize vector Elements

C ++ uses arrays to initialize a vector object, although this initialization form looks a little unfamiliar at first. When initializing a vector object using an array, you must specify the first element used for initialization and the address of the next position of the last element of the array:

Const size_t arr_size = 6;

Int int_arr [arr_size] = {0, 1, 2, 3, 4, 5 };

Vector <int> ivec (int_arr, int_arr + arr_size );

The two pointers passed to ivec indicate the range of the initial values of the vector. The second Pointer Points to the address space after the last copied element.

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.