*p++ // same as *(p++): increment pointer, and dereference unincremented address *++p // same as *(++p): increment pointer, and dereference incremented address ++*p // same as ++(*p): dereference pointer, and increment the value it points to (*p)++ // dereference pointer, and post-increment the value it points to *p++ = *q++; // equivalent to *p = *q; ++p; ++q;
const pointer
c++
1 2
constint* a = &b; //the value is a const constint* const a = &b; // the value is a const, and the pointer is also a const
pointer and string literals
c++
1
constchar * foo = "hello"; // foo needs to be const otherwise it is not allowed to be assigned
void pointer cast
c++
1 2 3 4
char* pChar; void* pVoid; pChar = (char*)pVoid; //OK in both C and C++ pChar = pVoid; //OK in C, not OK in C++. Convertion is implicit in C
null pointer
c++
1 2 3 4
int * p = 0; int * q = nullptr; int * r = NULL; cout << *r<<endl; // won't be able to compile
int *foo; int i = 2; foo = newint[i]; // As we can see here, the size can be a variable(determined in run time), this is the difference between array and new foo = newint[i]; // if allocation fails, an exception is thrown
// example with new with initialization int *p = newint(25); float *q = newfloat(75.25); int *d = newint[5]{1,2,3,4,5};
//note: foo is NOT arrau size_t x = sizeof(foo); // x will not be the array length, but 8 bytes (which is the length of a pointer address). This is different from array //example: int *foo = newint[5]; size_t x = sizeof(foo); cout << x << endl; // output: 8 (pointer length)
int bar[5]= {}; size_t y = sizeof(bar); cout << y << endl; // output: 20 (array length)
exception when using new
c++
1 2 3 4 5 6 7
foo = newint [5]; // if allocation fails, an exception is thrown foo = new (nothrow) int [5]; // no exception is thrown if allocation fails //In this case, if the allocation of this block of memory fails, the failure can be detected by checking if foo is a null pointer: if (foo == nullptr) { // error assigning memory. Take measures. }
delete
c++
1 2 3 4 5
int *foo = newint; delete foo; int *foo = newint[10]; delete[] foo; // if new[] is used, delete[] must be used otherwise there is memory leak.
vector<int> vector_name; vector<int> (3,100); // it means vector<int> {100, 100, 100} vector<vector<int> vector>;
vector methods
c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
vector.size(); vector.at(x); vector.push_back(); vector.pop_back(); vector.front(); vector.back(); std::vector<int>::iterator it = myvector.begin() // This inserts 2 of 300 after the first three elements of the vector // new_it is a pointer to the first element of the new vector new_it = vector.insert (it + 3,2,300);
// if the arguments are pointers, the inserted elements will be the contents between the values these pointers pointed to int myarray [] = { 501,502,503 }; it = vector.insert (vector.begin(), myarray, myarray+3);
vector iteration
c++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
std::vector<int>::iterator it = myvector.begin() for (it=myvector.begin(); it<myvector.end(); it++){ cout << *it << endl;//example vector loop }
// below is an example of vector loop using iterator std::vector<float> myvector {1,2,3}; for (std::vector<float>::iterator it=myvector.begin(); it != myvector.end(); it++){ std::cout << *it << std::endl; }
for (auto member: vector{ cout << member << endl; //This is another way of iterate a vector }
classSampleClass { private: int a; public: SampleClass () {} SampleClass (int x) : a(x) {} // member operator overloading function example declaration SampleClass operator + (const SampleClass&); // member operator overloading function example declaration booloperator > (const SampleClass&); // this is a const member inline function constint& get_a_value()const{return a;} };
// A function to return a rvalue SampleClass instance SampleClass get_temp_sample(SampleClass sample){ return sample; }
intmain(){ SampleClass a; SampleClass b("okok"); cout << "a string value is: " << a.get_ptr_value() << endl; cout << "b string value is: " << b.get_ptr_value() << endl; SampleClass c; cout << "c string value is: " << c.get_ptr_value() << endl; c = b; cout << "c string value after c = b is: " << c.get_ptr_value() << endl; SampleClass d(b); cout << "d string value is: " << d.get_ptr_value() << endl; SampleClass e = get_temp_sample(SampleClass("369")); cout << "e string value is: " << e.get_ptr_value() << endl; e = get_temp_sample(SampleClass("okok")); cout << "e string value after move assignment is: " << e.get_ptr_value() << endl; return0; }