C ++ Miscellaneous Memo

Source: Internet
Author: User

<From C ++ Primer>

Const

Modify the variable. The variable is assigned a value during initialization and cannot be modified during the program running.

The default value of a non-const variable is extern. To make the const variable accessible in other files, it must be explicitly specified as extern.

A const reference is a reference to a const object. It can only reference the value of this object, but cannot change its value.

<From Sun Xin VC ++ for further explanation>

Const char * and char * const

Many functions related to file operations have two parameter types: const char * (pointer to constant) and char * const.

Const before *: indicates a pointer to a constant. This pointer cannot modify the content in the memory to which it points, but can modify the memory address to which it points.

For example, char ch [5] = "lisi ";

Const char * pStr = ch

* PStr = 'W' // error

PStr = "wangwu" // OK

Char * const: A pointer constant in the table after *. For a pointer constant, it must be assigned a value in the defined colleague. A pointer Constant indicates that the pointer itself is a constant.

Char * const pStr = ch;

PStr = "zhangsan" // error

* PStR = 'W' // OK

<From Xu Huimin C ++>

Common member functions

Syntax format:

Return Value Type description function name (parameter table) const

Features: const is an integral part of the function type. The const keyword must also be included in function definition, declaration, and implementation.

A common member function cannot update the data member of an object or call a non-member function in this class.

A common object can only call a common member function, but a common member function can also be called by a common object.

The const keyword can be used to differentiate overload functions.

  

From C ++ Reference

Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace std;
Int main ()
{
Int end;
Int myints [] = {10, 20, 30, 40 };
Int * p;
P = find (myints, myints + 4, 30 );
P ++;
Cout <"The element following 30 is" <* p <endl;
Vector <int> myvector (myints, myints + 4 );
Vector <int>: iterator it;
It = find (myvector. begin (), myvector. end (), 30 );
It ++;
Cout <"The element following 30 is" <* it <endl;

Cin> end;
Return 0;
}

This code indicates the relationship between iterator and pointer, and the relationship between arrays and corresponding class vector.

Constructor initialization type: it can only be specified in the constructor definition but not in the Declaration. The class type members without the default constructor, And the const or reference type members, regardless of the type, must be initialized in the constructor initialization list. The initialization order of members in the constructor initialization List defines the order of members.

 

A vector cannot be assigned a value through a subscript. It can only be assigned through push_back (). However, a map or a two-dimensional vector contains a vector and can be assigned a value through a subscript.

The following code is valid:

 

Vector <int> myintegers;
Myintegers. resize (3 );
For (int I = 0; I <3; I ++)
{
Myintegers [I]. resize (4 );
}
For (int I = 0; I <3; I ++)
{
For (int j = 0; j <4; j ++)
{
Myintegers [I] [j] = numeric_limits <int >:: min ();
}

}
For (int I = 0; I <3; I ++)
{
For (int j = 0; j <4; j ++)
{
Cout <myintegers [I] [j];
}
Cout <endl;

}

 

 

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.