Přejít na menu

C++ cheatsheet

Správa článků

Vyhledávání Vyhledávání
9.10.2015 13:13
,
Počet přečtení: 626
Cheatsheet for C++ language.

Declarations

Declare static map (since C++11):

#include <map>
std::map<std::string, std::string> field_aliases_name = {
    {"key1", "value1t"},
    {"key2", "value2"}
};

Iterating

Using keyword auto (since C++11):

std::vector<int> numbers = {1, 2, 3};
for (auto item : numbers )
     std::cout << item << std::endl;

//std::vector<MyObject> vector_of_my_objects;
for (auto& item: vector_of_my_objects)
     std::cout << item << std::endl;

Casting

C++ reference

Static cast

  • when you know types of variables
  • checked in compile time
  • NewType new_variable = static_cast<NewType>(old_variable);

Dynamic cast

  • converting pointers and references
  • checked in run time
  • "children to parent" is OK, but "parent to children" is possible when 
  • NewType* new_variable = dynamic_cast<NewType*>(old_variable);

Reinterpret cast

  • starts to use the data in memory as another type data
  • no checking (=fast but dangerous)
  • NewType* new_variable = reinterpret_cast<NewType*>(old_variable);

std::string

Functions:

  • my_string.insert(position, string) - insert chars or strings into the string
  • my_string.compare("abc")

Constants:

  • std::string::npos - offset which means "doest not found" when searching in strings (size_t npos = -1)
Vytvořil 9. října 2015 ve 13:13:16 mira. Upravováno 26x, naposledy 22. října 2015 ve 22:11:57, mira


Diskuze ke článku

Vložení nového komentáře
*
*
*