Giter Club home page Giter Club logo

cppcrash's Introduction

C++ Refresher

Quick Tips

  • Return type for functions probably best to be void as it doesn't expect a return type.

INCLUDE, REFERENCING

  • #include "../log.h" // (Best use for everything) Relative to current file
  • #include <log.h> // ()Does not need to be relative

if you include:

using namespace std;

then you don't need to do things like std::cout just cout

  • If your function is below main or in another folder you should specify it in the include section i.e.
#include <string>
using namespace std;
int addNumbers(int num1, int num2);

Alternatively you can include header files :

  • Add the function declaration (just function header no body in the .h file
  • Reference the .h file in your code i.e #include log.h
  • Ensure #pragma once is included as it will fail to compile where duplicate lines exist especially with structs etc
  • Alternatively in your logfile, you can check if something has been defined already
#ifndef _LOG_H
#define _LOG_H  

void initLog();


#endif

Quick functions

String functions

string mystring = "Adam";
mystring.len()
mystring.find("d",0);
mystring.substr(1,3);

GET INPUT VS STRING INPUT

cin >> age 
cout << age ;

// get string input 
getline(cin,name);
cout << "hello " << name;

Console out

cout << "My name is " << characterName << endl;

NOTES

VECTOR

A dynamic array you don't need to populate up front.

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{

	// VECTOR 

	vector<string> friends;

	friends.push_back("Oscar");
	friends.push_back("Angela");
	friends.push_back("Kevin");
	friends.insert(friends.begin() + 1, "Jim");

	// friends.erase(friends.begin() + 1);	

	cout << friends.at(0) << endl;
	cout << friends.at(1) << endl;
	cout << friends.at(2) << endl;
	cout << friends.size() << endl;

	return 0;


}


ARRAYS

int mynumbers[]  = {4,5,6,7};


// empty array 

int mynumbers[20];

Add number

mynumbers[4] = 8;

Threading

PROS

  • Fast to start
  • Shares mem
  • Unlike multiprocessing, all bloat isn't needed to prevent them stepping on each others toes
  • Low overhead

CONS

  • Difficult to implement
  • Can't run on distributed system
#include <thread>
#include <iostream>
using namespace std;


// FUNCTION WE WISH TO THREAD 

void function_1(){

	std::cout << " a random print statement" << std::endl;
}

int main(){
	std::thread t1(function_1); // t1 starts running
	

	// DEPENDENT 


	t1.join(); // main thread waits for t1 to finish

	// DEAOMON PROCESS 
	// DETACH means it runs freely (main wont wait)
	// If you ran this, it wouldn't print beause main finishes first
	t1.detach(); 

	return 0;
}

POINTERS

#include <iostream>
#include <string>
using namespace std;

/// -----------------USING POINTERS 
int main(){

	int num = 10;
	cout << &num << endl; // get address 

	int *pNum = &num;  // reference Pointer = address of num var

	cout << pNum  << endl; // Prins out the saved pointer 
	cout << *pNum << endl; // Actually prints out the physical pointed value 

	return 0;


	// POINTER IS JUST A VARIABLE THAT ALLOWS YOU TO STORE AN ADDRESS 


}

functions

// DECLARE THREAD 
std::thread t1(function_1);

// JOIN
t1.join();
  
// DETACH
t1.detach();


// Check detached
if (t1.joinable())
	t1.join();

NOTES

COMPARING PRIMATIVES

mutual exclusion lock

The most efficient mechanism in both memory use and execution time. The basic use of a mutual exclusion lock is to serialize access to a resource.

condition variable

The basic use of a condition variable is to block on a change of state; that is it provides a thread wait facility. Remember that a mutex lock must be acquired before blocking on a condition variable and must be unlocked after returning from pthread_cond_wait(). The mutex lock must also be held across the change of state that occurs before the corresponding call to pthread_cond_signal().

Sephamore

The semaphore uses more memory than the condition variable. It is easier to use in some circumstances because a semaphore variable functions on state rather than on control. Unlike a lock, a semaphore does not have an owner. Any thread can increment a semaphore that has blocked.

Read-Write lock

The read-write lock permits concurrent reads and exclusive writes to a protected resource. The read-write lock is a single entity that can be locked in read or write mode. To modify a resource, a thread must first acquire the exclusive write lock. An exclusive write lock is not permitted until all read locks have been released.

BASICS

Mac

Compile

make myprogram.cpp

run

./myprogram.cpp

Class definition

Declaration

    string characterName = "Adam";
    int characterAge = 35;

Print

cout << "My name is " << characterName << endl;
cout << "My name is " << characterName << "\n";

Strings

int main(){

	// S T R I N G S 

	string greeting = "hello";

	cout << greeting.length();
	cout << greeting[0];
	cout << greeting.find("llo") << endl;
	cout << greeting.substr(2) << endl;
	cout << greeting.substr(1,3) << endl; 
	

	return 0;

}

Functions string mystring = "Adam";
mystring.len()
mystring.find("d",0);
mystring.substr(1,3);

Variables

string name    = "Mike";
char testGrade = 'A';


// Numbers 

int age = 20;   //  16 bit signed 
short age = 10; // 16 bit at least signed 
long age = 44; // at least 32 bit  signed 
long long age 40; // at least 64 bit signed



bool isHungry;    // true/false 
isTall = true;

Casting

int volume = 10.2; // double 

cout << (int)volume 


	

cppcrash's People

Contributors

murchie85 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.