data type
integer
// 32 bit or 4 Bytes
int Max = 0;
// short int 16 bit or long 32 bit
short a;
long b;
// size of type
cout << sizeof(short int)<<endl;
// character
char ch = "A";
print hex or oct value
Convert every 4 digits for hex, 3 for oct.
cout << hex << a << endl;
// dec for 10-
//input hex number, starting with 0x; use 0 for oct
int a = 0xFFF85;
max and min value
// 4.2 billion
unsigned int a= 0xFFFFFFFF;
cout << dec << a << endl;
// 2.1 billion
signed int a= 0x7FFFFFFF;
cout << dec << a << endl;
float
- float, 32 bit, 7-digit precision
- double, 64 bit, 15-digit precision
- long double, 64 bit, 15-digit precision
cout
will print 6-digit.
// set precision
cout << setprecision(100) << a << endl;
// input
float a = 0;
a = 1234.56e5;
Avoid add a large number with a small number.
Character
char a = 64;
int b = 'z';
//
cout << '\a' << '\\' << '\n';
Boolean
//True, non-zero
bool b1 = true;
b1 = -100;
b1 = 7 > 3;
//False, zero
constant
const double PI = 3.14;
n = 10000L; // long int const
x = 3.14L;
y = 3.14F;
k = 1000U; // unsigned
variable name
input & output
// to use setprecision()
#include<iomanip>
cout << fixed << setprecision(2) << f;
operator
sizeof
- subscript
[]
- logic:
! && ||
- digit
>> ~ | ^ &
assign value =
- different type, auto-conversion
- long to short, trim the low digit to short
- short to long, keep the same
- sign bit, assign value no matter sign or digit bit
a += 3
x *= y+8
x %= 3
a = b = c = 5;
arithemic
++i; i++
comma, if, conversion
// calculate 1 then 2 ..., value of last
a = 3*5, a*4; // last value is 60
max = (a>b) ? a:b;
(double)a // convert value of a and pass, a doesn't change
bit operation
- and :
&
, do and independently for every bit - or :
|
- xor :
^
- not :
~
- left move :
a<<2
, move every bit to left by a bit, discard overflow high bit, give 0 to new low bit. like *2 while move 1 bit if nothing discarded - right move :
>>
exchange two values
a = a^b;
b = b^a;
a = a^b;
control flow
// switch
switch (expression) {
default: expr0;
case value: expr1;
case ...
}
// for
for (int i = 0; i < 10; i++) {
break;
continue;
}
// while
while ( expr) {
}
// do while
do
{
} while (expr);
array
definition
float sheep[10];
// must use constant for size of array
const int i = 4;
int a[i];
// or
#define N 4
int main() {
int a[N];
}
// subscript starts from 0
// pre-allocation
int a[ ] = {1,2,3};
// other value will be 0
int a[4] = {1,2};
int a[4] = {0};
2-D array
int a[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12}};
int a[ ][4] = {1,2,3,4,5,6,7,8}; // 2 rows, 4 columns
int a[ ][4] = { {1},{0,6},{0,0,1} }; // fill with zeros
3-D array
int a[3][4][5];
using variable as array size
#include<vector>
vector<int> a(length,value);
string array
// other value will be initialized as '\0\'
char a[10] = {'a','b','c'};
char c[] = {'a','b','c'};
// double quote, has one extra value '\0'
char c[] = "China"
char c[5] = "China"; // invalid
// cannot assign value like this
str1[] = "China";
str1 = "China";
str2 = str1;
// use function to assign values
char str1[] = "C++ lang", str2[20];
int i = 0;
while (str1[i] != '\0') {
str2[i] = str1[i];
i++;
}
str2[i] = '\0';
2-D array
char weekday[7][11] = {"Sunday",...};
cin
// space and return will be treated as separator
while (cin >> grade) {
}
// but following will receive space and return
while ((c=cin.get()) != EOF) {
}
// equivalent to
while (cin.get(c))
// don't skip any char
while (c = getchar())
a sequence of chars
char a[10] = "Computer";
cout <<a; \\ end with '\0'
// 2d
char weekday[7][11] = {...};
for (int i = 0, i < 7; i++)
cout << weekday[i]<<endl;
cin
char str[10];
cout << "enter a sentence:" << endl;
while (cin >> str)
cout <<str << endl;
\\ cin.get(variable_name, number to read, stop sign)
\\ return 1 if success, 0 while stopped
\\ if 'o' is stop sign
(cin.get(ch,10,'\n'); \\ pointer stopped at exactly before stop sign 'we are g~ood'
cin.getline(ch,10,'\n'); \\ pointer stopped at one pos after stop sign, 'we are go~od`
\\
cin >> n;
cin.get() \\ read extra return
\\
while (cin.getline(str,200))
{
}
assign value
#include<string>
strcpy(str2,str1);