C++ study notes collection
Static
// Example 1: Normal functionint getNumber() { int count = 0; return count++; // count always starts at 0}
// Example 2: Static variable in functionint getNumber() { static int count = 0; // Only initialized once return count++; // count remembers its value between calls}
int main() { cout << getNumber(); // prints 0 cout << getNumber(); // prints 1 cout << getNumber(); // prints 2}understand below:
static LveGameObject createGameObject()static: can be called without an instanceLveGameObject: data typecreateGameObject(): function name
Template
< > are used for templates. and template can actually accept multiple template parameters, not just one.
For example:
std::pair<int, std::string> person(42, "John");example: std::pair
int main() { std::pair<int , std::string>person(22, "kk"); std::cout<<person.second<<std::endl; return 0; }output: kk
‘
Class
class AA{ public: struct A_struct{ int xx = 10; int yy = 20; } float member1{0.f}; float member2{1.f};}when instance AA , for example AA AA_object{} , It initialize, but nothing to do with struct, only initialize members
If auto-initialize the struct, the class would have a struct-initialize-member inside
class AA{ public: struct A_struct{ int xx = 10; int yy = 20; } float member1{0.f}; float member2{1.f}; A_struct BB{}; //after adding this, when instance, struct will be initialized}Unordered Map
#include <unordered_map> std::unordered_map<string, int> scores; // Key type is string, value type is int
scores["Alice"] = 100;scores["Bob"] = 95;scores["Charlie"] = 88;`Unordered_map is a container that stores key-value pairs
- It uses hash table internally
- Elements are not stored in any particular order
- O(1) compared to
mapwhich is O(log n) - But use more memory than
mapdue to hash table structure - needs to handle collision
Preprocessor
The preprocessor is a tool that runs before your source code is compiled. Its main functions include:
- Processing Directives: It handles instructions that start with
#, such as#include,#define, and#ifdef.
-These directives tell the preprocessor to include other files, define macros, or conditionally compile parts of your code. - Macro Expansion: It replaces macros with their defined values or code snippets.
- File Inclusion: It inserts the content of header files into your source code when you use
#include.