Advertisement
rasmonkey17

C++ Notes

Jun 2nd, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. //------------demonstrate I/O---------------
  2. //INPUT: cin or scanf (scanf from <cstudio>)
  3. //OUTPUT: cout or printf (printf from <cstudio>)
  4. /*printf/scanf are from C, while cout/cin are from C++. There's tradeoffs for both,
  5. but printf is usually better for production. */
  6.  
  7. //---------show common data types-----------
  8. /*
  9. Int ("%d"): 32 Bit integer
  10. Long ("%ld"): 64 bit integer
  11. Char ("%c"): Character type
  12. Float ("%f"): 32 bit real value
  13. Double ("%lf"): 64 bit real value
  14. */
  15.  
  16. //----------------random notes---------------
  17. • Vectors vs. C Arrays: C arrays lower-level but fast. vectors allow for easy manipulation at the expense of memory usage.
  18.  
  19. • map<string, int> m;
  20.   m.insert(make_pair("hello", 17));
  21.  
  22. • vector<int> v = {1, 2, 3, 4}; //binary search O(logn)
  23.   cout << (binary_search(v.begin(), v.end(), 3) ? : "Found" ? "Not found") << endl;
  24.  
  25. • vector<int> v = {1, 2, 3, 4};
  26.   auto iter = lower_bound(v.begin(), v.end(), 2); //lower_bound() is O(logn). Returns pointer to val in vector if found, next biggest value if not.
  27.   int index = distance(v.begin(), iter); //upper_bound() is same thing but NOT EQUAL TO only returns first value greater than.
  28.   cout <<
  29.  
  30.  
  31.  
  32. //---------demonstrate templates------------
  33. // inside of numbers.h
  34. template <typename T>
  35. T get_smallest(T num1, T num2)
  36. {
  37.     return num2 < num1? num2 : num1;
  38. }
  39.  
  40. //----------demonstrate classes----------------
  41. // inside of music.cpp
  42. #include <iostream>
  43. #include "song.h"
  44. int main()
  45. {
  46.     Song my_song("Ignorance", "Paramore");
  47.     std::cout << my_song.get_title() << std::endl;
  48.     return 0;
  49. }
  50.  
  51. // inside of song.h
  52. #include <string>
  53. class Song {
  54.     private:
  55.         std::string title;
  56.         std::string artist;
  57.  
  58.     public:
  59.         Song(std::string new_title, std::string new_artist);
  60.         ~Song();
  61.         std::string get_title();  
  62.         std::string get_artist();
  63. };
  64.  
  65. // inside of song.cpp
  66. #include "song.h"
  67. Song::Song(std::string new_title, std::string new_artist)
  68. {
  69.     title = new_title;
  70.     artist = new_artist;
  71. }
  72.  
  73. Song::~Song()
  74. {
  75.     /* destructors normally aren't needed bc automatically garbage collected when the object moves out of scope, is explicitly deleted, and when the program ends*/
  76. }
  77.  
  78. std::string Song::get_title() {
  79.     return title;
  80. }
  81.  
  82. std::string Song::get_artist() {
  83.     return artist;
  84. }
  85.  
  86. //----------demonstrate references---------------
  87. int a = 23;
  88. int &b = a;
  89. b++;
  90.  
  91. //----------demonstrate pass-by-reference---------------
  92. void swap_num(int &i, int &j)
  93. {
  94.     int temp = i;
  95.     i = j;
  96.     j = temp;
  97. }
  98.  
  99. int main()
  100. {
  101.     int a = 100;
  102.     int b = 200;
  103.     swap_num(a, b);
  104.     std::cout << a; //200
  105.     std::cout << b; //100
  106. }
  107.  
  108. //-----demonstrate pass-by-reference with const-----------
  109. void triple(int const &i)
  110. {
  111.     return i * 3;
  112. }
  113.  
  114. int main()
  115. {
  116.     std::cout << triple(10) << std::endl; //30
  117. }
  118.  
  119. //----------demonstrate pointers----------------------
  120. //purpose of a pointer is to share a memory address among other contexts (usually functions).
  121. //& before variable means address of
  122. //* before variable means contents of
  123. int var = 17;
  124. int *pointer = &var;
  125. std::cout << ptr << std::endl; // "0x7fff640295ac"
  126. std::cout << *ptr << std::endl; // dereference operator. "17"
  127. int *pointer; //pointer pointing to... nothing... this is dangerous! Make it "int *pointer = nullptr;" as of C++11.
  128.  
  129. //--------------demonstrate structs------------------
  130. //combine multiple fields in a single data structure.
  131. //Keep details (name, height, weight) in a single package in the theoretical "Person" class.
  132. struct Student
  133. {
  134.     string first_name;
  135.     string last_name;
  136.     int age;
  137.     double gpa;
  138. };
  139.  
  140. int main()
  141. {
  142.     Student st;
  143.     st.first_name = "Mika";
  144.     std::cout << st.first_name << std::endl;
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement