#include<bits/stdc++.h> classcomplex{ public: complex(int ri = 0, int ii = 0){ r = ri; i = ii; } int r; int i; friendcomplexoperator+(constcomplex &b) ;//友元函数 //complex operator+(const complex &b) const; 成员函数 };
Use unsigned to state an unsigned variable type, an unsigned variable do not have a sign digit.
Type conversion
1 2 3 4
bool a =42;// 0 refers true and the others refer false. int i =3.14;// the value of i is 3,the fractional digit has been dropped. double pi =i;//the value of pi is 3 unsignedchar c = -1; //the value of c is 255
WARNING: DO NOT USE UNSIGNED TYPE AND SIGNED TYPE VARIABLE AT THE SAME TIME !!!
Reference
1 2 3
int a=3; int &num = a;// num is a reference of a int &num2;//ERROR: reference must be initialized
Pointer
Pointers point to a pointer:
1 2 3 4
int a=1024; int *ptr1 = &a; int **ptr2 = &ptr1; cout<<**ptr2<<endl;// output 1024
C++ Standard Library :: begin, end
1 2 3
int ia[]={1,2,3,4,5,6,7,8,9}; int *beg = begin(ia);//refer a[0] int *end = end(ia);//refer the next position after the last element of an array
Pointer with custom size:
1 2 3 4 5
int a[2][2]; int *p[2];//NOT THIS: It created a pointer array int (*p2)[2];//a pointer which can point to an array with 2 ints. //use decltype or auto: decltype *p3 = a;
Const
You can use const to make a variable unchangeable.
Top-level-const and Low-level-const:
1 2 3 4
int i=0; int *const p1 = &i;//the value of pointer can not be changed,top-level-const constint ci =42;//the value of ci can not be changed. constint *p2 = &ci;//the value of pointer can not be changed
Typedef
1
typedefdouble wages;
Decltype
Use decltype to get a type that a function returns.
1
decltype(f()) sum =0;//the type of sum is just the type that f() returns.
Write your own head file
You’d better define your struct and class in a head file .
intmain() { vector<int> vec1;//Statement for (int i = 0; i < 5; i++) { vec1.push_back(i); } auto it = vec1.begin();//Auto Statement vector<int>:: iterator it = vec1.begin();//Standard statement
cout << *it << endl; for (; it != vec1.end(); it++) { cout << *it << endl; }
return0;
}
Operations:
1 2 3 4 5
it = v1.begin();//set v1 refer to the first *it;//return value it++;//point to the next *it = 1234;//change value it2 = it + 2;//send the address + 2 to another
Stack: a LIFO(Last in First out) data structure
Statement in C++:
1
stack<int> stack_name;
Basic operations:
1 2 3
stack_name.push(data);//push a data into the stack cout<<stack_name.top();//read the data at top stack_name.pop();//pop out the data at top
Queue: a FIFO(First in First out) data structure
Statement in C++:
1
queue<int> q;
Basic operations:
1 2 3 4
q.front();//read the data at the front q.push();//push a data at the end of the queue q.pop();//pop out the data at the front q = queue<int>();//empty the queue
int sum = 0; queue<int> children; for (int i = 1; i <= qty_children; i++) { children.push(i); } //statement queue while (sum < qty_children-1) {
for (int i = 0; i < k-1; i++) { children.push(children.front());//if we do not pull out the line, thus we let this wild kid get to the back. children.pop(); } children.pop();//when it comes to pull out the line, this wild kid is over. sum++; } cout << children.front();
return0; }
Using dynamic array with pointer
Code demo:
1 2
int *p = newint[10];//Create an array when running. delete[] p;//the realease of a dynamic array method is not same with a variable
bitset
A data structure which storages bit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bitset<4> bitset1;//construct without parameter:set space 4
bitset<8> bitset2(12);//the space is 8, storge 12 into binary system,use 0 to fill the blank bit.
string s = "100101"; bitset<10> bitset3(s);//the space is 10,use 0 to fill the blank bit,the string CAN ONLY CONTAINS 0 and 1.
char s2[] = "10101"; bitset<13> bitset4(s2);//the space is 13,use 0 to fill the blank bit
cout << bitset1 << endl;//0000 cout << bitset2 << endl;//00001100 cout << bitset3 << endl;//0000100101 cout << bitset4 << endl;//0000000010101 bitset3[0];//Simular as array,use subscript to access the elements.
There’s so many ways to use bitset, but let’s see struct first.
Struct
A struct is a data structure which contains serval variable type. The struct can also form an array!
C++ examples
1 2 3 4 5 6 7
structBooks { char title[50]; char author[50]; char subject[100]; int book_id; } book = {"C Language", "RUNOOB", "Programing Language", 123456};//define a struct and initialize
//Of course, you can use it as below: No initialize.
This is a significant data structure in C++ standard library , equals an array that can change memory size at will,and you can also CRUD in it in will.
Basic operations:
1 2
vector<int> c; c.push_back(1);//place the element at the end of the vector.
1 2
vector<Sales_item> sales_vec;//vector can also store class or struct vector<vector<string>>;//the elements in this vector are also vector objects.
Other operations:
1 2 3 4 5 6
vector<T> v1{1,2,3,4,5}; vector<T> v2(5,5);//{5,5,5,5,5} vector<string> v3={"Hello","World","!"}; vector<int> v4(10); //Create a vector have 10 elements, initialized by 0. v4.push_back(1);//put an elemnet to the back of the vector; v4.size();
Use array to initialize a vector
1 2
int a[]={1,2,3,4,5}; vector<int> v1(begin(a),end(arr));//send the begin and end to construct.
String
String is a char sequence whose size is changeable .
Initialize:
1 2
string s1;//an empty string has been created strings2(10,'c');//"cccccccccc"
#include<stdio.h> #include<math.h> intmain() { int m, n; int s = 0; intmin = 0; scanf("%d%d", &m, &n); //最大公约数 if (m > n) { s = m; min = n; } else { s = n; min = m; } while (min != 0) { int t = s%min; s = min; min = t; } printf("%d\n",s); //重置两个数 if (m > n) { s = m; min = n; } else { s = n; min = m; } //公倍数 for (int i = min;; i += min) { double resR = (double)i / s; intfloor = i / s;
if ((double)floor == resR) { printf("%d\n", i); break; } }
return0; }
筛法判断质数
上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
boolisPrime(int a) { if (a == 2) returntrue; if (a % 2 == 0) returnfalse; int x = sqrt(a); for (int i = 2; i <= x; ++i) { if (a % i == 0) returnfalse; } returntrue; }
intmain() { int number_list[1000] = {0}; //we can get primes below 1000 bool break_flag = false; number_list[0] = 1; int pri = 2; while (!break_flag) { // the non-prime will be mark 1 int count = pri; while (count <= 1000) { count += pri; if (count >= 1000) break; number_list[count] = 1; } int temp_pri = pri; while (true) { temp_pri++; if (temp_pri >= 999) { break_flag = true; break; } if (number_list[temp_pri] == 0) { pri = temp_pri; break; } } } for (int i = 0; i < 1000; i++) { if (number_list[i] == 0) { cout << i << " "; } }