codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › All commands

All commands

Every command of this course: name, detailed description, syntax and example code.

Every command used in this course, with its name, a detailed description, the syntax and an example.

#include <iostream>

Definition: Loads the input/output library that provides cout and cin.

Syntax:

#include <iostream>

Example:

#include <iostream>

using namespace std;

Definition: Lets you write cout instead of std::cout by using names from the std namespace directly.

Syntax:

using namespace std;

Example:

using namespace std;

cout

Definition: Sends output to the console. Chain several values with << and use endl for a new line.

Syntax:

cout << value << endl;

Example:

cout << "Hello " << 5 << endl;

cin

Definition: Reads a value typed by the user into a variable using the >> operator.

Syntax:

cin >> variable;

Example:

int n;
cin >> n;

string

Definition: A type for text. It can be joined with + and has helpers such as length().

Syntax:

string name = "text";

Example:

string s = "Hi";
cout << s.length();

vector

Definition: A resizable array. push_back adds an item at the end and size() gives the count.

Syntax:

vector<type> name;

Example:

vector<int> v;
v.push_back(4);

class

Definition: Defines your own type that bundles data (members) with functions (methods) that work on it.

Syntax:

class Name {
public:
  members;
};

Example:

class Car {
public:
  int speed;
};

for (auto x : list)

Definition: Range-based loop that visits every item of a container; auto lets C++ pick the type.

Syntax:

for (auto x : container) { ... }

Example:

for (auto x : v) {
  cout << x;
}

#include

Definition: #include loads a header, main() starts the program, statements end with ; .

Syntax:

#include

Example:

#include <iostream>

↗ C++ syntax and comments

main()

Definition: #include loads a header, main() starts the program, statements end with ; .

Syntax:

main()

Example:

int main() {

↗ C++ syntax and comments

endl

Definition: endl or "\n" starts a new line.

Syntax:

endl

Example:

cout << "Age: " << 20 << endl;

↗ C++ output

const

Definition: Use const for fixed values and auto to let the compiler infer the type.

Syntax:

const

Example:

const char grade = 'A';

↗ C++ variables

auto

Definition: Use const for fixed values and auto to let the compiler infer the type.

Syntax:

auto

Example:

auto n = 7;

↗ C++ variables

getline(cin, s)

Definition: Use getline(cin, s) to read a whole line with spaces.

Syntax:

getline(cin, s)

Example:

cout << "Name: "; getline(cin, name);

↗ C++ user input

int

Definition: int , double , float , char , bool , string .

Syntax:

int

Example:

int main() {
cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;

↗ C++ data types

double

Definition: int , double , float , char , bool , string .

Syntax:

double

Example:

cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;

↗ C++ data types

char

Definition: int , double , float , char , bool , string .

Syntax:

char

Example:

cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;

↗ C++ data types

bool

Definition: int , double , float , char , bool , string .

Syntax:

bool

Example:

cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;

↗ C++ data types

sizeof

Definition: Check sizes with sizeof .

Syntax:

sizeof

Example:

cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;

↗ C++ data types

boolalpha

Definition: bool prints as 1 or 0 unless you use boolalpha .

Syntax:

boolalpha

Example:

cout << boolalpha << (a > b && b > 0) << endl;

↗ C++ operators and booleans

length()

Definition: Join with + , use length() , substr , find , append and index with [] .

Syntax:

length()

Example:

cout << s << " " << s.length() << " " << s.substr(0, 5) << " " << s[1] << " " << s.find("World") << endl;

↗ C++ strings

substr

Definition: Join with + , use length() , substr , find , append and index with [] .

Syntax:

substr

Example:

cout << s << " " << s.length() << " " << s.substr(0, 5) << " " << s[1] << " " << s.find("World") << endl;

↗ C++ strings

find

Definition: Join with + , use length() , substr , find , append and index with [] .

Syntax:

find

Example:

cout << s << " " << s.length() << " " << s.substr(0, 5) << " " << s[1] << " " << s.find("World") << endl;

↗ C++ strings

cmath

Definition: Include cmath for sqrt , pow , round , ceil , floor , abs .

Syntax:

cmath

Example:

#include <cmath>

↗ C++ math

sqrt

Definition: Include cmath for sqrt , pow , round , ceil , floor , abs .

Syntax:

sqrt

Example:

cout << sqrt(64) << " " << pow(2, 10) << " " << round(2.6) << " " << max(3, 8) << endl;

↗ C++ math

pow

Definition: Include cmath for sqrt , pow , round , ceil , floor , abs .

Syntax:

pow

Example:

cout << sqrt(64) << " " << pow(2, 10) << " " << round(2.6) << " " << max(3, 8) << endl;

↗ C++ math

round

Definition: Include cmath for sqrt , pow , round , ceil , floor , abs .

Syntax:

round

Example:

cout << sqrt(64) << " " << pow(2, 10) << " " << round(2.6) << " " << max(3, 8) << endl;

↗ C++ math

max

Definition: max and min need no header.

Syntax:

max

Example:

cout << sqrt(64) << " " << pow(2, 10) << " " << round(2.6) << " " << max(3, 8) << endl;

↗ C++ math

if

Definition: Use if , else if , else , the ternary ?: and switch with break .

Syntax:

if

Example:

if (t < 12) cout << "Morning" << endl;

↗ C++ if...else and switch

else

Definition: Use if , else if , else , the ternary ?: and switch with break .

Syntax:

else

Example:

else cout << "Later" << endl;

↗ C++ if...else and switch

switch

Definition: Use if , else if , else , the ternary ?: and switch with break .

Syntax:

switch

Example:

switch (d) {

↗ C++ if...else and switch

break

Definition: Use if , else if , else , the ternary ?: and switch with break .

Syntax:

break

Example:

case 1: cout << "Mon" << endl; break;
case 3: cout << "Wed" << endl; break;

↗ C++ if...else and switch

while

Definition: while checks first; do...while runs at least once.

Syntax:

while

Example:

while (i <= 3) { cout << i << " "; i++; }

↗ C++ while loop

for

Definition: Classic for counts.

Syntax:

for

Example:

for (int i = 0; i < 3; i++) cout << i << " ";
for (int x : a) cout << x << " ";

↗ C++ for loop

continue

Definition: break leaves the loop, continue jumps to the next round.

Syntax:

continue

Example:

if (i == 2) continue;

↗ C++ break and continue

struct

Definition: A struct bundles fields.

Syntax:

struct

Example:

struct Car { string brand; int year; };

↗ C++ structures and enums

enum

Definition: enum names integer constants; enum class keeps names scoped and safer.

Syntax:

enum

Example:

enum class Level { Low, High };

↗ C++ structures and enums

nullptr

Definition: A pointer can change targets and can be nullptr .

Syntax:

nullptr

Example:

cout << x << " " << *p << " " << (p != nullptr) << endl;

↗ C++ pointers

new

Definition: new allocates on the heap and delete frees it ( delete[] for arrays).

Syntax:

new

Example:

int *p = new int(42);

↗ C++ memory management

delete

Definition: new allocates on the heap and delete frees it ( delete[] for arrays).

Syntax:

delete

Example:

delete p;

↗ C++ memory management

void

Definition: Use void when nothing is returned.

Syntax:

void

Example:

void inc(int &n) { n++; }

↗ C++ functions and parameters

private

Definition: Make data private and expose it through getters and setters, so the class controls how it changes.

Syntax:

private

Example:

private:

↗ C++ access specifiers and encapsulation

friend

Definition: A friend function is not a member but may read private data.

Syntax:

friend

Example:

friend void show(Box b);

↗ C++ friend functions

virtual

Definition: Mark a base method virtual and override it.

Syntax:

virtual

Example:

class Animal { public: virtual void sound() { cout << "..." << endl; } virtual ~Animal() {} };

↗ C++ polymorphism

override

Definition: Mark a base method virtual and override it.

Syntax:

override

Example:

class Dog : public Animal { public: void sound() override { cout << "Woof" << endl; } };

↗ C++ polymorphism

fstream

Definition: Include fstream .

Syntax:

fstream

Example:

#include <fstream>

↗ C++ files

ofstream

Definition: ofstream writes, ifstream reads.

Syntax:

ofstream

Example:

ofstream out("data.txt");

↗ C++ files

ifstream

Definition: ofstream writes, ifstream reads.

Syntax:

ifstream

Example:

ifstream in("data.txt");

↗ C++ files

time(0)

Definition: time(0) gives seconds since 1970.

Syntax:

time(0)

Example:

time_t now = time(0);

↗ C++ date and time

ctime

Definition: ctime converts it to readable text.

Syntax:

ctime

Example:

#include <ctime>
cout << ctime(&now);

↗ C++ date and time

exception

Definition: Catch standard errors with exception and what() .

Syntax:

exception

Example:

} catch (const exception &e) {

↗ C++ exceptions

what()

Definition: Catch standard errors with exception and what() .

Syntax:

what()

Example:

cout << e.what() << endl;

↗ C++ exceptions

push_back

Definition: A vector grows automatically: push_back , pop_back , size , at .

Syntax:

push_back

Example:

v.push_back(9);

↗ C++ STL and vectors

size

Definition: A vector grows automatically: push_back , pop_back , size , at .

Syntax:

size

Example:

cout << "size " << v.size() << endl;

↗ C++ STL and vectors

list

Definition: A list inserts and removes anywhere in constant time, but has no [] access.

Syntax:

list

Example:

#include <list>
list<int> l = {2, 3};

↗ C++ list

push_front

Definition: Use push_front and push_back .

Syntax:

push_front

Example:

l.push_front(1); l.push_back(4);

↗ C++ list

stack

Definition: A stack uses push , top , pop .

Syntax:

stack

Example:

#include <stack>
stack<int> s; s.push(1); s.push(2);

↗ C++ stacks and queues

push

Definition: A stack uses push , top , pop .

Syntax:

push

Example:

stack<int> s; s.push(1); s.push(2);
queue<int> q; q.push(1); q.push(2);

↗ C++ stacks and queues

top

Definition: A stack uses push , top , pop .

Syntax:

top

Example:

cout << s.top() << " " << q.front() << endl;

↗ C++ stacks and queues

queue

Definition: A queue uses push , front , pop .

Syntax:

queue

Example:

#include <queue>
queue<int> q; q.push(1); q.push(2);

↗ C++ stacks and queues

front

Definition: A queue uses push , front , pop .

Syntax:

front

Example:

cout << s.top() << " " << q.front() << endl;

↗ C++ stacks and queues

deque

Definition: A deque supports push_front , push_back and [] access.

Syntax:

deque

Example:

#include <deque>
deque<int> d = {2, 3};

↗ C++ deque

set

Definition: A set keeps unique items in order.

Syntax:

set

Example:

#include <set>
set<int> s = {5, 1, 5, 3};

↗ C++ sets

insert

Definition: Use insert , count , erase .

Syntax:

insert

Example:

s.insert(2);

↗ C++ sets

count

Definition: Use insert , count , erase .

Syntax:

count

Example:

cout << s.count(3) << endl;

↗ C++ sets

map

Definition: A map stores sorted key-value pairs.

Syntax:

map

Example:

#include <map>
map<string, int> age = {{"Ravi", 30}};

↗ C++ maps

begin()

Definition: begin() and end() give iterators.

Syntax:

begin()

Example:

for (auto it = v.begin(); it != v.end(); ++it) cout << *it << " ";

↗ C++ iterators

end()

Definition: begin() and end() give iterators.

Syntax:

end()

Example:

for (auto it = v.begin(); it != v.end(); ++it) cout << *it << " ";

↗ C++ iterators

algorithm

Definition: Include algorithm .

Syntax:

algorithm

Example:

#include <algorithm>

↗ C++ algorithms

sort

Definition: They work on iterator ranges: sort , reverse , find , count , max_element .

Syntax:

sort

Example:

sort(v.begin(), v.end());

↗ C++ algorithms

max_element

Definition: They work on iterator ranges: sort , reverse , find , count , max_element .

Syntax:

max_element

Example:

cout << *max_element(v.begin(), v.end()) << endl;

↗ C++ algorithms

namespace

Definition: A namespace groups names.

Syntax:

namespace

Example:

namespace Math { int twice(int x) { return x * 2; } }

↗ C++ namespaces

mt19937

Definition: Modern way: a mt19937 engine seeded by random_device and a distribution.

Syntax:

mt19937

Example:

mt19937 gen(rd());

↗ C++ how to: random numbers

random_device

Definition: Modern way: a mt19937 engine seeded by random_device and a distribution.

Syntax:

random_device

Example:

random_device rd;

↗ C++ how to: random numbers

Example code

#include <iostream>
using namespace std;

int main() {
  cout << "Hello, C++!" << endl;
  return 0;
}

Run this code in the online editor

← Welcome to the C++ courseC++ introduction and get started →