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>
main()
Definition: #include loads a header, main() starts the program, statements end with ; .
Syntax:
main()
Example:
int main() {endl
Definition: endl or "\n" starts a new line.
Syntax:
endl
Example:
cout << "Age: " << 20 << endl;
const
Definition: Use const for fixed values and auto to let the compiler infer the type.
Syntax:
const
Example:
const char grade = 'A';
auto
Definition: Use const for fixed values and auto to let the compiler infer the type.
Syntax:
auto
Example:
auto n = 7;
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);
int
Definition: int , double , float , char , bool , string .
Syntax:
int
Example:
int main() {
cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;double
Definition: int , double , float , char , bool , string .
Syntax:
double
Example:
cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;
char
Definition: int , double , float , char , bool , string .
Syntax:
char
Example:
cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;
bool
Definition: int , double , float , char , bool , string .
Syntax:
bool
Example:
cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;
sizeof
Definition: Check sizes with sizeof .
Syntax:
sizeof
Example:
cout << sizeof(int) << " " << sizeof(double) << " " << sizeof(char) << " " << sizeof(bool) << endl;
boolalpha
Definition: bool prints as 1 or 0 unless you use boolalpha .
Syntax:
boolalpha
Example:
cout << boolalpha << (a > b && b > 0) << endl;
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;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;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;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;
else
Definition: Use if , else if , else , the ternary ?: and switch with break .
Syntax:
else
Example:
else cout << "Later" << endl;
switch
Definition: Use if , else if , else , the ternary ?: and switch with break .
Syntax:
switch
Example:
switch (d) {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;
while
Definition: while checks first; do...while runs at least once.
Syntax:
while
Example:
while (i <= 3) { cout << i << " "; i++; }for
Definition: Classic for counts.
Syntax:
for
Example:
for (int i = 0; i < 3; i++) cout << i << " "; for (int x : a) cout << x << " ";
continue
Definition: break leaves the loop, continue jumps to the next round.
Syntax:
continue
Example:
if (i == 2) continue;
struct
Definition: A struct bundles fields.
Syntax:
struct
Example:
struct Car { string brand; int year; };enum
Definition: enum names integer constants; enum class keeps names scoped and safer.
Syntax:
enum
Example:
enum class Level { Low, High };nullptr
Definition: A pointer can change targets and can be nullptr .
Syntax:
nullptr
Example:
cout << x << " " << *p << " " << (p != nullptr) << endl;
new
Definition: new allocates on the heap and delete frees it ( delete[] for arrays).
Syntax:
new
Example:
int *p = new int(42);
delete
Definition: new allocates on the heap and delete frees it ( delete[] for arrays).
Syntax:
delete
Example:
delete p;
void
Definition: Use void when nothing is returned.
Syntax:
void
Example:
void inc(int &n) { n++; }private
Definition: Make data private and expose it through getters and setters, so the class controls how it changes.
Syntax:
private
Example:
private:
friend
Definition: A friend function is not a member but may read private data.
Syntax:
friend
Example:
friend void show(Box b);
virtual
Definition: Mark a base method virtual and override it.
Syntax:
virtual
Example:
class Animal { public: virtual void sound() { cout << "..." << endl; } virtual ~Animal() {} };override
Definition: Mark a base method virtual and override it.
Syntax:
override
Example:
class Dog : public Animal { public: void sound() override { cout << "Woof" << endl; } };ofstream
Definition: ofstream writes, ifstream reads.
Syntax:
ofstream
Example:
ofstream out("data.txt");ifstream
Definition: ofstream writes, ifstream reads.
Syntax:
ifstream
Example:
ifstream in("data.txt");time(0)
Definition: time(0) gives seconds since 1970.
Syntax:
time(0)
Example:
time_t now = time(0);
ctime
Definition: ctime converts it to readable text.
Syntax:
ctime
Example:
#include <ctime> cout << ctime(&now);
exception
Definition: Catch standard errors with exception and what() .
Syntax:
exception
Example:
} catch (const exception &e) {what()
Definition: Catch standard errors with exception and what() .
Syntax:
what()
Example:
cout << e.what() << endl;
push_back
Definition: A vector grows automatically: push_back , pop_back , size , at .
Syntax:
push_back
Example:
v.push_back(9);
size
Definition: A vector grows automatically: push_back , pop_back , size , at .
Syntax:
size
Example:
cout << "size " << v.size() << endl;
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);
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);
top
Definition: A stack uses push , top , pop .
Syntax:
top
Example:
cout << s.top() << " " << q.front() << endl;
queue
Definition: A queue uses push , front , pop .
Syntax:
queue
Example:
#include <queue> queue<int> q; q.push(1); q.push(2);
front
Definition: A queue uses push , front , pop .
Syntax:
front
Example:
cout << s.top() << " " << q.front() << endl;
deque
Definition: A deque supports push_front , push_back and [] access.
Syntax:
deque
Example:
#include <deque>
deque<int> d = {2, 3};set
Definition: A set keeps unique items in order.
Syntax:
set
Example:
#include <set>
set<int> s = {5, 1, 5, 3};↗ 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 << " ";
end()
Definition: begin() and end() give iterators.
Syntax:
end()
Example:
for (auto it = v.begin(); it != v.end(); ++it) cout << *it << " ";
algorithm
Definition: Include algorithm .
Syntax:
algorithm
Example:
#include <algorithm>
sort
Definition: They work on iterator ranges: sort , reverse , find , count , max_element .
Syntax:
sort
Example:
sort(v.begin(), v.end());
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;
namespace
Definition: A namespace groups names.
Syntax:
namespace
Example:
namespace Math { int twice(int x) { return x * 2; } }mt19937
Definition: Modern way: a mt19937 engine seeded by random_device and a distribution.
Syntax:
mt19937
Example:
mt19937 gen(rd());
random_device
Definition: Modern way: a mt19937 engine seeded by random_device and a distribution.
Syntax:
random_device
Example:
random_device rd;
Example code
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++!" << endl;
return 0;
}