C++

Built-in Types

byte:

word:

  1. arithmetic type

  2. void

    Arithmetic type

    1. integral type (char and bool)

      signed : negative number, positive number and zero

      unsigned: more than zero.

      unsigned u1 = 10, u2 = 42;
      int i = 42;
      cout << u1 + i <<endl; // if int is 32 bit, cout 4294967264
      cout << u2 - u1 << endl; // print 32
      cout << u1 - u2 << endl; // print undefined value.
      warning: unsigned type in loop
      
      

      char: char, signed char, unsigned char

    2. float type

    3. boolean**

      true and false

      all nonzero value yields true, and 0 yields false.

    4. literal

      20  // decimal
      024 // 
      0x14 // hex
      

void

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

variable

variable is almost same as object, which own memory and type

**type specifier , identifier **

​ initialize

  1. list initialization

separate compilation: declaration and definition

variable can be only defined once, but can be declare many times

scope

#include <iostream>
int sum = 0; // global scope
 int main()
 {
     int unique = 10; // block scope
     for (int i=0; i< 10; ++i){
         sum += i; 
     }
 }

const expression

const int max = 20;
#define int max 20
constexpr int mf = 20;

type alias

  1. typedef

  2. alias declaration

    typedef sales_item SI;
    using SI = Sales_item;
    

    auto

    auto variable must has initialized values. (owns only a build-in type in once statement)

    auto item = val1 + val2;
    

    decltype

    it meas chose and return type

    decltype (f()) sum = x ; // type of sum is function f return type
    **** decltype ((variable)) is always means reference.
    **** decltype (variable) // depends on the variable type
    

Operators

precedence, associativity, order of evaluation

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
cout << *iter++ << endl; more than cout<< *iter<<endl; ++iter;

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 (only same 1 return 1, else 0.(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;
}		

sequence


*,/,%,-,+

Array

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

not copy or assign.

#include <cstddef>

string

#include <string>
string s1;
string s2(s1);   // copy s1 to s2
string s2 = s1;  //  copy s1 to s2
string s3("value");  //  s3="value", direct initialize 
string s4(n,'c');  //  s4 inlcude n's c char, direct initialize
string s5 = "valued"; 
getline(is, s); 
s.empty(); s.size(); s1 +s2; s[n],s[s.size()-1], s1 == s2; >, <, >=,<=, sensitive upper and lower.
#include <cctype>
isalnum(c); 
isalpha(c);
isdigit(c);
toupper(c);
tolower(c);
ispunct(c); 
    while (getline(cin, line)){
        if (!line.empty()){
           	cout << line <<endl;
        }
    }
string::size_type : is a unsigned value. s.size return a unsigned value.
	auto len = line.size(); 
string s1 = "hello", s2 = "world";
string s3 = s1 + s2;
string s4 = s1 + "world";
string s5 = "hello" + s2; // wrong, 
string s6 = "hello" + " ," ; // wrong, cannot add two literal value.
	string str("some string");
	for (auto c : str){
        cout << c <<endl;} // c is char
for (decltype(s.size()) index =0; index != s.size() && !isspace(s[index]); ++index){
    s[index] = toupper(s[index]);
} // print SOME string

vector

vector is a class template.(container)

#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;
vector<Sales_item> Sales_vec;
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++;
	}
if(s.begin() != s.end()){
    auto it = s.begin();
    *it = toupper(*it);
}

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;
}

for ( decltype (s.size()) index = 0; index != s.size() && !isspaces(s[index]); ++index){
    s[index] = toupper(s[index]);
}

while (conditon)
statement;

while ( int i = get_num())
    cout << i << endl;
i = 0; //error, can not access the loop.

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

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

try

throw

throw expresson

  • task
  • abc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值