Built-in Types
-
boolean
return ture or false
-
character
-
integer
intergal type, include char and bool 20 024 0x14
-
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
- Addition operator ( + )
- Subtraction operator ( - )
- multiplication operator(*)
- division operatpr(/)
- Modulo operator ( % )
- Increament operator ( ++ )
- 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
&, | , ^, <<, >>
oprators | meaning | Description |
---|---|---|
& | AND | bitwise AND (1-1=1, 0-0=0) |
| | OR | bitwise inclusive OR ( if 1 then 1) |
~ | NOT | unary complement(1=0, 0=1) |
^ | XOR | bitwise exclusive OR (0-0=0, 1-1=0, 1-0=1, if same then 0, else 1) |
<< | SHL | shift bits left |
>> | SHR | shift 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){}
- code sequence
- return type
- parameter list
- 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