C++

Built-in Types

  1. boolean

     return ture or false
    
  2. character

  3. integer

     intergal type, include char and bool
     
     20
     024
     0x14
    
  4. floating point

Operators

Assignment operator (=)

the assignment operator assigns a value to a variable (right-to-left).

int x =5;  x=y=z=5;

Compound assignment

+=, -=,*=, /=, >>=, <<=, &=, ^=, |=

Arithmetic operators

  1. Addition operator ( + )
  2. Subtraction operator ( - )
  3. multiplication operator(*)
  4. division operatpr(/)
  5. Modulo operator ( % )
  6. Increament operator ( ++ )
  7. Decreament operator ( – )
    notice division operator: data type
    x=5/2 is 2; and x=5.0/2.0 is 2.5 , x=5%3 is 2.
    prefix(++x), suffix(x++)
int x = 2, y= 3; int num =(y + n++) * 6 ; num is (2+3) * 6 = 30
int x = 2, y= 3; int num =(y + ++n) * 6 ; num is (2+4) * 6 = 36

Relational and comparison opreators

== , !=, >, < , >=, <=

Logical operators

!, &&, ||

Conditional ternary operator

condition ? result1 : result2

(a>b) ? a:b ; //if (a>b) is ture, return a, else return b.

comma operator

int x = ( b=2, b+3);  x = 5

bitwise opertors

&, | , ^, <<, >>

opratorsmeaningDescription
&ANDbitwise AND (1-1=1, 0-0=0)
|ORbitwise inclusive OR ( if 1 then 1)
~NOTunary complement(1=0, 0=1)
^XORbitwise exclusive OR (0-0=0, 1-1=0, 1-0=1, if same then 0, else 1)
<<SHLshift bits left
>>SHRshift bits right
#include <iostream>
#include <list>
#include <bitset>
using namespace std;
void BinaryBitset(int n)
{
	cout<< bitset<sizeof(int)*8> (n)<<endl;
} 
int main()
{
	int a =4;
	int b =3;
	int c_and = a & b;
	cout << a <<" a = ";
	BinaryBitset(a);	
	cout << b << " b = ";	
	system("pause");
	return 0;
}
number << n means number * 2^n
number >> n means number / 2^n (number >=0)

sizeof, new, delete

x = sizeof(char) is 1

two-dimensiong array (p[m][n]) assign memory

int main()
{
	int **p; //p[m][n]
	p = new int *[m];
	for(int i =0 ; i < m; ++i)
	{
		p[i] = new int[n];
	}
	for (i = 0; i<m; i++)
	{
		delete [] p[i];
	}
	delete [] p;

Three-Dimenssion array

#inlcude <iostream>
int mian()
{
	int ***p; //p[m][n][k]
	p = new int **[m]
	for(int i=0; i<m;++i)
	{
		p[i] = new *p[n];
		for(int j=0; j<n;++j)
		{
		p[i][j] = new int[k];
		}
	}
	for(int i=0; i<m; ++i)
		{
			for(int j=0; j<n; ++j)
				{
					delete [] p[i][j];
				}
			delete [] p[i];
		}
		delete [] p;

		return 0;
}		

Explicit type casting operators

int i; float f=3.14; i=int (f);  i=3.  // truncated
bool b=42; // all nonzero values yield ture, 0 yields false;
double result = static_cast<double>(firstNumber)/secondNumber;
const_cast
dynamic_cast
reinterpret_cast

sequence


*,/,%,-,+

Function

typename name(parameter, parameter){ statement;}
typename name(void){}

  1. code sequence
  2. return type
  3. parameter list
  4. function body

(by reference or by value)

#include "graphics.h"

// global function 
void Function()
{
}

/*class funciton definition*/
void Box::Draw()
{
}
var foo = 'bar';

Pointer to Function

const vector<int> *fibon_seq(int size); // pointer to int* 
const vector<int> (*fibon_seq)(int size); // pointer to function

overloading


Array

array has a fixed size, has a better run-time performance with losing flexibility in some specialized applications.

String

#include <string>
string s1;
string s2(s1);   // copy s1 to s2
string s2 = s1;  //  copy s1 to s2
string s3("value");  //  s3="value"
string s4(n,'c');  //  s4 inlcude n's c char.
s.empty(); s.size(); s1 +s2; s[n], s1 == s2; >, <, >=,<=, sensitive upper and lower.
#include <cctype>
isalnum(c);
isalpha(c);

vector

vector is a class template.

#include <vector>
vector<int> vec;
vector<int> vec(Size);
vector<int> vec(Size, value); //vec= {value,...,Size}.
vector<int> vec{Size,Value}; // vec={Size, Value}.
vector<double> *pv =0;
int nums[5] = {1,2,3,4,5};
vector<int> vec(nums,nums+5); // vec == nums.
	if(vec.empty())
	{
		for (i = 0; i < 5; i++)
			vec.push_back(i); //vec[i] = i; is wrong, does not add an element.
	}
	/*using iterator counter*/
	vector<int>::iterator v = vec.begin();
	auto v= vec.begin();
	while (v != vec.end())
	{
		cout << "value of v = " << *v << endl;
		v++;
	}

for

for( init-statement; codition; expression)
{
	 statement;
}	
vector<int> vec = {1,2,3,4,5};
for(auto &r : vec) // r must be a reference so we can change the element
	r *= 2; // double every element in vec.
vector<int> ::const_iterator t = vec.begin(); // t can only read not write
for (auto beg = vec.begin() , end = vec.end(); beg != end; ++beg)
{
	auto &r = beg;
	r *=2;
}

while (conditon)
statement;

if (true)
statement;
else if (true)
statement_;
else
statement_t_;

switch (value)
case (one):
statement; break;
case (two):
statement_; break;

  • task
  • abc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值