Essential C ++ chap.
Article Directory
1. How to write a C ++ program
Input and output statement
#include<iostream>
int main()
{
int a;
cin>>a;
cout<<a<<endl;
}
2. object initialization
There are two ways to initialize the object
(1) the direct assignment method
int a = 3;
(2) Method initializer
int a(3);
The method appears initialization list because some variables are not single-valued, for example, a complex that contains the real and imaginary parts, we must use two variables are initialized, so there will be two input parameters of the problem, and therefore the initialization list It is very valuable.
3. Operator
Operators include arithmetic operators, relational operators, and logical operators
Wherein there is a more useful feature, the scope of the arithmetic operators, the method is to use the data branch of the conditional expression, like for example the output, five sub-row can be written as
cout<<data
<<(num%5?' ':'\n');
4.array and Vector
The main initialization vector write about, there are two ways, one is using the initialization list of methods, one is an array of data interception.
(1) initialization list
Esential C ++, said vector does not support such an approach, but VS is used.
#include<vector>
vector<int> vc[5] = {1,2,3,4,5}
Do not write in brackets is also OK
(2) Method of the array taken
#include<vector>
vector<int> VC(array,array+size);
This is achieved by the method of address, and address in parentheses are headed end address. vector with the original array configuration using this method is independent relationship, the modification will not change one another.
5. Pointer
5.1 Pointer
(1) If there is no pointer is initialized when bind variables should be initialized to a null pointer.
int *p = 0;
(2) for determining a pointer, a null pointer should be avoided, and therefore should be used for short-circuit operation
if(p&&*p!=1024)
{
}
}
When p is a null pointer is that its value is 0, due to the short circuit calculation behind that expression will no longer be calculated, so the expression pointer, should first rule out that he is not a null pointer
(3) a pointer twofold. Not only contains address information, but also be able to access variable address points. This capability is known withdraw effect.
5.2 pointer to the vector
defined as
vector<int> vec = {1,2,3,4,5}
vector<int> *p= &vec;
When there exist a plurality of vector, we want to call pointer way, the index can be used for the embodiment, as shown in the above direct calls between, will lose transparency, specifically refers to an object may not be easy to see, the use of the index can be a good way to see it.
//01定义一些vector
vector<int> Fibonacci = {1,1,2,3,5,8,13,21};
vector<int> Lucas = { 1,3,4,7,11,18,29,47 };
vector<int> Pell = { 1,2,5,12,29,70,169,408 };
//02 将这些类的指针地址组合在vector中
vector<int>* p[3] = { &Fibonacci,&Lucas,&Pell };
//03使用随机数去寻找这些vector
srand(time(NULL));
int index = rand() % 3;
vector<int>* q = p[index];
//04 打印选到的
for (int i = 0; i < q->size(); i++)
{
cout << (*q)[i] << endl;
}
Pointer 5.3 points to vector data call
We suppose to do such a thing, to determine whether a vector is empty, if not empty, the second element is zero.
Program is
vector<int>a = {1,0,2,3};
if(!a.empty()&&a[1]===0)
{
}
}
If the vector is a pointer to how to do this thing?
This judgment mainly do two things, namely access to the member functions and member variables of the class. Using a dot (member variables operator selection) and brackets (index symbol)
vector<int> a = {1,0,2,3};
//01定义指针
vector<int> *p = &a;
//02调用成员函数
//成员函数的调用使用->
//写作:p->empty();
//03 调用成员变量
//因为索引优先级比指针运算符高,要加括号
//写作:(*p)[1]
//04 要注意判断指针不能为空指针的问题
//05 综上所述,上面的那句判断应该写作
if(p&&!p->empty()&&(*p)[1]==0)
{
}
6. File Operations
Need the header file fstream
Including both read and write