Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------demonstrate I/O---------------
- //INPUT: cin or scanf (scanf from <cstudio>)
- //OUTPUT: cout or printf (printf from <cstudio>)
- /*printf/scanf are from C, while cout/cin are from C++. There's tradeoffs for both,
- but printf is usually better for production. */
- //---------show common data types-----------
- /*
- Int ("%d"): 32 Bit integer
- Long ("%ld"): 64 bit integer
- Char ("%c"): Character type
- Float ("%f"): 32 bit real value
- Double ("%lf"): 64 bit real value
- */
- //----------------random notes---------------
- • Vectors vs. C Arrays: C arrays lower-level but fast. vectors allow for easy manipulation at the expense of memory usage.
- • map<string, int> m;
- m.insert(make_pair("hello", 17));
- • vector<int> v = {1, 2, 3, 4}; //binary search O(logn)
- cout << (binary_search(v.begin(), v.end(), 3) ? : "Found" ? "Not found") << endl;
- • vector<int> v = {1, 2, 3, 4};
- 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.
- int index = distance(v.begin(), iter); //upper_bound() is same thing but NOT EQUAL TO only returns first value greater than.
- cout <<
- //---------demonstrate templates------------
- // inside of numbers.h
- template <typename T>
- T get_smallest(T num1, T num2)
- {
- return num2 < num1? num2 : num1;
- }
- //----------demonstrate classes----------------
- // inside of music.cpp
- #include <iostream>
- #include "song.h"
- int main()
- {
- Song my_song("Ignorance", "Paramore");
- std::cout << my_song.get_title() << std::endl;
- return 0;
- }
- // inside of song.h
- #include <string>
- class Song {
- private:
- std::string title;
- std::string artist;
- public:
- Song(std::string new_title, std::string new_artist);
- ~Song();
- std::string get_title();
- std::string get_artist();
- };
- // inside of song.cpp
- #include "song.h"
- Song::Song(std::string new_title, std::string new_artist)
- {
- title = new_title;
- artist = new_artist;
- }
- Song::~Song()
- {
- /* destructors normally aren't needed bc automatically garbage collected when the object moves out of scope, is explicitly deleted, and when the program ends*/
- }
- std::string Song::get_title() {
- return title;
- }
- std::string Song::get_artist() {
- return artist;
- }
- //----------demonstrate references---------------
- int a = 23;
- int &b = a;
- b++;
- //----------demonstrate pass-by-reference---------------
- void swap_num(int &i, int &j)
- {
- int temp = i;
- i = j;
- j = temp;
- }
- int main()
- {
- int a = 100;
- int b = 200;
- swap_num(a, b);
- std::cout << a; //200
- std::cout << b; //100
- }
- //-----demonstrate pass-by-reference with const-----------
- void triple(int const &i)
- {
- return i * 3;
- }
- int main()
- {
- std::cout << triple(10) << std::endl; //30
- }
- //----------demonstrate pointers----------------------
- //purpose of a pointer is to share a memory address among other contexts (usually functions).
- //& before variable means address of
- //* before variable means contents of
- int var = 17;
- int *pointer = &var;
- std::cout << ptr << std::endl; // "0x7fff640295ac"
- std::cout << *ptr << std::endl; // dereference operator. "17"
- int *pointer; //pointer pointing to... nothing... this is dangerous! Make it "int *pointer = nullptr;" as of C++11.
- //--------------demonstrate structs------------------
- //combine multiple fields in a single data structure.
- //Keep details (name, height, weight) in a single package in the theoretical "Person" class.
- struct Student
- {
- string first_name;
- string last_name;
- int age;
- double gpa;
- };
- int main()
- {
- Student st;
- st.first_name = "Mika";
- std::cout << st.first_name << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement