Skip to content

Instantly share code, notes, and snippets.

@andrit
Last active July 7, 2018 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrit/d466cfc9b981bd91e5a4f0f03280ca56 to your computer and use it in GitHub Desktop.
Save andrit/d466cfc9b981bd91e5a4f0f03280ca56 to your computer and use it in GitHub Desktop.
Learning C++ using the youtube vids & a book C++11

C++ Notes

CyrilStachniss-Youtube CPP-01

link

Declaring Variables

Variable Declaration always follows the pattern of: [ = ];

  • Every Variable has a type
  • Static Type
  • Better to initialize variables when they’re declared
  • name must start with letter
  • don’t include type in name
  • Google-Style uses underscores with all lowercase, in snake_case
  • names (C++ is…) are case sensitive

Examples: int uninitialized_var; int initialized_var = 0;

Built In Types

Out of the Box Types in C++ advanced

- bool this_is_fun = false;  // Boolean: True or False
- char carret_return = ‘\n'  // Single Character
- int  meaning_of_life = 42; // Integer number
- short smaller_int = 42; // Short Number
- long bigger_int = 42; // Long Number
- float fraction = 0.01f;  // Single Precision Float
- double precise_num = 0.01  //Double Precision float
- auto some_int = 13;  //Automatic Type [int]

Operations on arithmetic types all character, integer and floating point types are arithmetic operators: +, -, *, / comparisons return bool +=, -=, *=, /= Avoid == for floats bc they are imprecise

Logical operations ||, &&, ! % modulo increment and decrement a++ === ++a === a += 1

Strings #include to use std::string concatenate with + check if empty str.empty()

#include <iostream>
#include <string>
int main() {
  std::string hello="Hello";
std::cout << "Type your name:" << std::endl;
std::string name = ""; 
std::cin >> name;
std::cout << hello + ", " + name + "!" << std::endl;
return 0;

Array

  • #include to use std::array
  • store collection of items of same type
  • Create from Data:
    • array<float, 3> arr = {1.0f, 2.0f, 3.0f};
  • access item with arr[i]
  • number of stored items arr.size()
  • remove all elements arr.clear()
  • useful access alias:
    • first item: arr.front() == arr[0]
    • last item: arr.back() == arr[arr.size() - 1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment