C++ Tutorial

C++ Examples

118 complete C++17 programs, including STEM labs from maths, physics, and chemistry. Compile them at /cpp/try with g++.

118 copy-and-run snippets. Open one in Try C++ at /cpp/try, change a value, and run it again. Each language has its own shelf and its own editor.

Basics

Hello, C++

#include <iostream>
using namespace std;

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

Print a name

#include <iostream>
using namespace std;

int main() {
  cout << "Ada in " << 2026 << endl;
  return 0;
}
Related lesson →

Variables

#include <iostream>
using namespace std;

int main() {
  int a = 8;
  int b = 11;
  cout << a + b << endl;
  return 0;
}
Related lesson →

Const

#include <iostream>
using namespace std;

int main() {
  const int seats = 12;
  cout << seats << endl;
  return 0;
}
Related lesson →

Auto type

#include <iostream>
using namespace std;

int main() {
  auto n = 7;
  cout << n << endl;
  return 0;
}
Related lesson →

Arithmetic

#include <iostream>
using namespace std;

int main() {
  cout << 7 + 3 << " " << 7 * 3 << endl;
  return 0;
}
Related lesson →

Division remainder

#include <iostream>
using namespace std;

int main() {
  cout << 17 / 5 << " " << 17 % 5 << endl;
  return 0;
}
Related lesson →

Float average

#include <iostream>
using namespace std;

int main() {
  int sum = 8 + 11 + 5;
  cout << sum / 3.0 << endl;
  return 0;
}
Related lesson →

Boolean

#include <iostream>
using namespace std;

int main() {
  bool ready = true;
  cout << boolalpha << ready << endl;
  return 0;
}
Related lesson →

Sizeof

#include <iostream>
using namespace std;

int main() {
  cout << sizeof(int) << " " << sizeof(double) << endl;
  return 0;
}
Related lesson →

Enum class

#include <iostream>
using namespace std;

int main() {
  enum class Day { Mon, Tue, Wed };
  Day d = Day::Tue;
  cout << static_cast<int>(d) << endl;
  return 0;
}
Related lesson →

Reference

#include <iostream>
using namespace std;

int main() {
  int n = 7;
  int &alias = n;
  alias = 12;
  cout << n << endl;
  return 0;
}
Related lesson →

Cast

#include <iostream>
using namespace std;

int main() {
  cout << static_cast<int>(9.8) << endl;
  return 0;
}
Related lesson →

Hex output

#include <iostream>
using namespace std;

int main() {
  cout << hex << 255 << endl;
  return 0;
}
Related lesson →

String concat

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

int main() {
  string a = "Try ";
  string b = "C++";
  cout << a + b << endl;
  return 0;
}
Related lesson →

String length

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

int main() {
  string word = "compiler";
  cout << word.size() << endl;
  return 0;
}
Related lesson →

String index

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

int main() {
  string word = "python";
  cout << word[0] << word.back() << endl;
  return 0;
}
Related lesson →

Substring

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

int main() {
  string word = "StudyGrid";
  cout << word.substr(0, 5) << endl;
  return 0;
}
Related lesson →

Find in string

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

int main() {
  string word = "banana";
  cout << word.find("na") << endl;
  return 0;
}
Related lesson →

Reverse string

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

int main() {
  string word = "compiler";
  string flipped;
  for (int i = (int)word.size() - 1; i >= 0; i--) flipped += word[i];
  cout << flipped << endl;
  return 0;
}
Related lesson →

Control

If else

#include <iostream>
using namespace std;

int main() {
  int n = 7;
  if (n % 2 == 0) cout << "even" << endl;
  else cout << "odd" << endl;
  return 0;
}
Related lesson →

Elif chain

#include <iostream>
using namespace std;

int main() {
  int score = 82;
  if (score >= 90) cout << "A" << endl;
  else if (score >= 80) cout << "B" << endl;
  else cout << "C" << endl;
  return 0;
}
Related lesson →

Ternary

#include <iostream>
using namespace std;

int main() {
  int n = 4;
  cout << (n % 2 == 0 ? "even" : "odd") << endl;
  return 0;
}
Related lesson →

Switch

#include <iostream>
using namespace std;

int main() {
  int day = 3;
  switch (day) {
    case 1: cout << "Mon" << endl; break;
    default: cout << "later" << endl;
  }
  return 0;
}
Related lesson →

For loop

#include <iostream>
using namespace std;

int main() {
  for (int n = 1; n <= 5; n++) cout << n << endl;
  return 0;
}
Related lesson →

Range for

#include <iostream>
using namespace std;

int main() {
  int nums[] = {4, 17, 9};
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

While

#include <iostream>
using namespace std;

int main() {
  int n = 3;
  while (n > 0) {
    cout << n << endl;
    n--;
  }
  return 0;
}
Related lesson →

Break continue

#include <iostream>
using namespace std;

int main() {
  for (int n = 0; n < 6; n++) {
    if (n % 2 == 0) continue;
    cout << n << endl;
  }
  return 0;
}
Related lesson →

FizzBuzz

#include <iostream>
using namespace std;

int main() {
  for (int n = 1; n <= 15; n++) {
    if (n % 15 == 0) cout << "FizzBuzz" << endl;
    else if (n % 3 == 0) cout << "Fizz" << endl;
    else if (n % 5 == 0) cout << "Buzz" << endl;
    else cout << n << endl;
  }
  return 0;
}
Related lesson →

Nested loops

#include <iostream>
using namespace std;

int main() {
  for (int r = 1; r <= 3; r++) {
    for (int c = 1; c <= 3; c++) cout << r * c << " ";
    cout << endl;
  }
  return 0;
}
Related lesson →

Max of three

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

int main() {
  int a = 8, b = 11, c = 5;
  cout << max(a, max(b, c)) << endl;
  return 0;
}
Related lesson →

Leap year

#include <iostream>
using namespace std;

int main() {
  int year = 2024;
  bool leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
  cout << (leap ? "leap" : "common") << endl;
  return 0;
}
Related lesson →

Containers

C array sum

#include <iostream>
using namespace std;

int main() {
  int nums[] = {8, 11, 5};
  int sum = 0;
  for (int n : nums) sum += n;
  cout << sum << endl;
  return 0;
}
Related lesson →

Array max

#include <iostream>
using namespace std;

int main() {
  int nums[] = {4, 17, 9, 2, 13};
  int best = nums[0];
  for (int n : nums) if (n > best) best = n;
  cout << best << endl;
  return 0;
}
Related lesson →

2D array

#include <iostream>
using namespace std;

int main() {
  int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
  cout << grid[1][0] << endl;
  return 0;
}
Related lesson →

Vector push

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

int main() {
  vector<int> nums;
  nums.push_back(4);
  nums.push_back(17);
  cout << nums.size() << " " << nums[1] << endl;
  return 0;
}
Related lesson →

Vector range for

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

int main() {
  vector<int> nums = {1, 2, 3};
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Sort a vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {9, 2, 7, 1, 4};
  sort(nums.begin(), nums.end());
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Find in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {4, 17, 9};
  auto it = find(nums.begin(), nums.end(), 9);
  cout << (it != nums.end() ? *it : -1) << endl;
  return 0;
}
Related lesson →

Count in vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {3, 1, 3, 2, 3};
  cout << count(nums.begin(), nums.end(), 3) << endl;
  return 0;
}
Related lesson →

Map lookup

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

int main() {
  map<string, int> scores;
  scores["Mia"] = 95;
  scores["Kai"] = 88;
  cout << scores["Mia"] << endl;
  return 0;
}
Related lesson →

Map iterate

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

int main() {
  map<string, int> scores = {{"Ada", 99}, {"Kai", 88}};
  for (auto &pair : scores) cout << pair.first << " " << pair.second << endl;
  return 0;
}
Related lesson →

Set unique

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

int main() {
  set<int> nums = {3, 1, 3, 2};
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Set count

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

int main() {
  set<string> tags = {"html", "css"};
  cout << tags.count("html") << endl;
  return 0;
}
Related lesson →

Pair

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

int main() {
  pair<string, int> p = {"Ada", 2026};
  cout << p.first << " " << p.second << endl;
  return 0;
}
Related lesson →

Tuple

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

int main() {
  auto t = make_tuple("lat", 51.5);
  cout << get<0>(t) << " " << get<1>(t) << endl;
  return 0;
}
Related lesson →

Stack

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

int main() {
  stack<int> s;
  s.push(1);
  s.push(2);
  cout << s.top() << endl;
  s.pop();
  cout << s.top() << endl;
  return 0;
}
Related lesson →

Queue

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

int main() {
  queue<int> q;
  q.push(1);
  q.push(2);
  cout << q.front() << endl;
  q.pop();
  cout << q.front() << endl;
  return 0;
}
Related lesson →

Deque

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

int main() {
  deque<int> d = {1, 2};
  d.push_front(0);
  d.push_back(3);
  cout << d.front() << " " << d.back() << endl;
  return 0;
}
Related lesson →

Reverse vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {1, 2, 3};
  reverse(nums.begin(), nums.end());
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Accumulate

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main() {
  vector<int> nums = {8, 11, 5};
  cout << accumulate(nums.begin(), nums.end(), 0) << endl;
  return 0;
}
Related lesson →

Min max element

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {4, 17, 9};
  cout << *min_element(nums.begin(), nums.end()) << " ";
  cout << *max_element(nums.begin(), nums.end()) << endl;
  return 0;
}
Related lesson →

Functions and OOP

Add function

#include <iostream>
using namespace std;

int add(int a, int b) {
  return a + b;
}

int main() {
  cout << add(8, 11) << endl;
  return 0;
}
Related lesson →

Overload add

#include <iostream>
using namespace std;

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

int main() {
  cout << add(2, 3) << endl;
  cout << add(2.5, 3.5) << endl;
  return 0;
}
Related lesson →

Default argument

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

void greet(string name = "friend") {
  cout << "Hi, " << name << endl;
}

int main() {
  greet();
  greet("Kai");
  return 0;
}
Related lesson →

Factorial

#include <iostream>
using namespace std;

int factorial(int n) {
  int result = 1;
  for (int i = 2; i <= n; i++) result *= i;
  return result;
}

int main() {
  cout << factorial(5) << endl;
  return 0;
}
Related lesson →

Recursive fibonacci

#include <iostream>
using namespace std;

int fib(int n) {
  if (n < 2) return n;
  return fib(n - 1) + fib(n - 2);
}

int main() {
  for (int n = 0; n < 10; n++) cout << fib(n) << " ";
  cout << endl;
  return 0;
}
Related lesson →

Lambda

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {1, 2, 3, 4};
  for_each(nums.begin(), nums.end(), [](int n) { cout << n * n << " "; });
  cout << endl;
  return 0;
}
Related lesson →

Template max

#include <iostream>
using namespace std;

template <typename T>
T bigger(T a, T b) {
  return a > b ? a : b;
}

int main() {
  cout << bigger(3, 9) << endl;
  cout << bigger(2.5, 1.2) << endl;
  return 0;
}
Related lesson →

Class running total

#include <iostream>
using namespace std;

class Total {
  int sum;
public:
  Total() : sum(0) {}
  void add(int n) { sum += n; }
  int get() { return sum; }
};

int main() {
  Total t;
  t.add(4);
  t.add(7);
  cout << t.get() << endl;
  return 0;
}
Related lesson →

Constructor

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

class User {
  string name;
public:
  User(string n) : name(n) {}
  void hello() { cout << "Hi, " << name << endl; }
};

int main() {
  User u("Ada");
  u.hello();
  return 0;
}
Related lesson →

Inheritance

#include <iostream>
using namespace std;

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

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

int main() {
  Dog d;
  d.speak();
  return 0;
}
Related lesson →

Virtual polymorphism

#include <iostream>
using namespace std;

class Shape {
public:
  virtual int area() = 0;
  virtual ~Shape() {}
};

class Square : public Shape {
public:
  int area() override { return 9; }
};

int main() {
  Square s;
  Shape *p = &s;
  cout << p->area() << endl;
  return 0;
}
Related lesson →

Operator plus

#include <iostream>
using namespace std;

struct Point {
  int x, y;
  Point operator+(const Point &o) const { return {x + o.x, y + o.y}; }
};

int main() {
  Point a{1, 2}, b{3, 4};
  Point c = a + b;
  cout << c.x << " " << c.y << endl;
  return 0;
}
Related lesson →

Destructor note

#include <iostream>
using namespace std;

class Guard {
public:
  Guard() { cout << "acquire" << endl; }
  ~Guard() { cout << "release" << endl; }
};

int main() {
  Guard g;
  cout << "work" << endl;
  return 0;
}
Related lesson →

Static member

#include <iostream>
using namespace std;

class Counter {
public:
  static int count;
  Counter() { count++; }
};

int Counter::count = 0;

int main() {
  Counter a, b;
  cout << Counter::count << endl;
  return 0;
}
Related lesson →

Namespace

#include <iostream>
using namespace std;

namespace studio {
  int seats() { return 12; }
}

int main() {
  cout << studio::seats() << endl;
  return 0;
}
Related lesson →

Exception

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

int positive(int n) {
  if (n < 0) throw invalid_argument("need a positive number");
  return n;
}

int main() {
  try {
    cout << positive(-1) << endl;
  } catch (const exception &err) {
    cout << err.what() << endl;
  }
  return 0;
}
Related lesson →

Library

Sqrt

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

int main() {
  cout << sqrt(64.0) << endl;
  return 0;
}
Related lesson →

Pow

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

int main() {
  cout << pow(2.0, 10.0) << endl;
  return 0;
}
Related lesson →

Abs

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

int main() {
  cout << abs(-12) << endl;
  return 0;
}
Related lesson →

Chrono duration

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

int main() {
  using namespace chrono;
  auto start = steady_clock::now();
  auto end = start + milliseconds(5);
  cout << duration_cast<milliseconds>(end - start).count() << endl;
  return 0;
}
Related lesson →

Pointer new delete

#include <iostream>
using namespace std;

int main() {
  int *n = new int(7);
  cout << *n << endl;
  delete n;
  return 0;
}
Related lesson →

Nullptr

#include <iostream>
using namespace std;

int main() {
  int *p = nullptr;
  cout << (p == nullptr ? "null" : "set") << endl;
  return 0;
}
Related lesson →

String stream

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

int main() {
  stringstream ss;
  ss << "Ada " << 2026;
  cout << ss.str() << endl;
  return 0;
}
Related lesson →

To string

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

int main() {
  cout << to_string(42) + " seats" << endl;
  return 0;
}
Related lesson →

Stod

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

int main() {
  cout << stod("3.5") + 1 << endl;
  return 0;
}
Related lesson →

Ternary pass

#include <iostream>
using namespace std;

int main() {
  int score = 71;
  cout << (score >= 70 ? "pass" : "retry") << endl;
  return 0;
}
Related lesson →

Const method pattern

#include <iostream>
using namespace std;

int main() {
  const int n = 7;
  const int *p = &n;
  cout << *p << endl;
  return 0;
}
Related lesson →

Structured binding

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

int main() {
  pair<int, int> p{3, 4};
  auto [x, y] = p;
  cout << x << " " << y << endl;
  return 0;
}
Related lesson →

If init

#include <iostream>
using namespace std;

int main() {
  if (int n = 4; n % 2 == 0) cout << "even" << endl;
  return 0;
}
Related lesson →

String compare

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

int main() {
  cout << (string("ada") == string("ada")) << endl;
  return 0;
}
Related lesson →

Vector at

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

int main() {
  vector<int> nums = {1, 2, 3};
  cout << nums.at(1) << endl;
  return 0;
}
Related lesson →

Emplace

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

int main() {
  vector<string> names;
  names.emplace_back("Ada");
  cout << names[0] << endl;
  return 0;
}
Related lesson →

Lower bound

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {1, 4, 9, 16};
  cout << *lower_bound(nums.begin(), nums.end(), 9) << endl;
  return 0;
}
Related lesson →

Unique after sort

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {3, 1, 3, 2, 1};
  sort(nums.begin(), nums.end());
  nums.erase(unique(nums.begin(), nums.end()), nums.end());
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

All of

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {2, 4, 6};
  cout << boolalpha << all_of(nums.begin(), nums.end(), [](int n) { return n % 2 == 0; }) << endl;
  return 0;
}
Related lesson →

Any of

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {1, 2, 3};
  cout << boolalpha << any_of(nums.begin(), nums.end(), [](int n) { return n > 2; }) << endl;
  return 0;
}
Related lesson →

Transform

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums = {1, 2, 3};
  vector<int> out(3);
  transform(nums.begin(), nums.end(), out.begin(), [](int n) { return n * n; });
  for (int n : out) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Iota

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main() {
  vector<int> nums(5);
  iota(nums.begin(), nums.end(), 1);
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Fill

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> nums(4);
  fill(nums.begin(), nums.end(), 7);
  for (int n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Swap values

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

int main() {
  int a = 3, b = 9;
  swap(a, b);
  cout << a << " " << b << endl;
  return 0;
}
Related lesson →

Clamp

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

int main() {
  cout << clamp(12, 0, 10) << endl;
  return 0;
}
Related lesson →

GCD

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

int main() {
  cout << gcd(48, 18) << endl;
  return 0;
}
Related lesson →

LCM

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

int main() {
  cout << lcm(4, 6) << endl;
  return 0;
}
Related lesson →

Hypot

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

int main() {
  cout << hypot(3.0, 4.0) << endl;
  return 0;
}
Related lesson →

Floor ceil

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

int main() {
  cout << floor(4.7) << " " << ceil(4.1) << endl;
  return 0;
}
Related lesson →

Pointer arithmetic

#include <iostream>
using namespace std;

int main() {
  int nums[] = {1, 2, 3};
  int *p = nums;
  cout << *(p + 2) << endl;
  return 0;
}
Related lesson →

Const ref loop

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

int main() {
  vector<int> nums = {1, 2, 3};
  for (const int &n : nums) cout << n << " ";
  cout << endl;
  return 0;
}
Related lesson →

Optional-like pointer

#include <iostream>
using namespace std;

int main() {
  int n = 7;
  int *p = &n;
  if (p) cout << *p << endl;
  return 0;
}
Related lesson →

Stringstream parse

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

int main() {
  stringstream ss("8 11 5");
  int a, b, c;
  ss >> a >> b >> c;
  cout << a + b + c << endl;
  return 0;
}
Related lesson →

Map default insert

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

int main() {
  map<string, int> tally;
  tally["rye"]++;
  tally["rye"]++;
  cout << tally["rye"] << endl;
  return 0;
}
Related lesson →

Set insert

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

int main() {
  set<int> nums;
  nums.insert(3);
  nums.insert(1);
  nums.insert(3);
  cout << nums.size() << endl;
  return 0;
}
Related lesson →

Vector reserve

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

int main() {
  vector<int> nums;
  nums.reserve(3);
  nums.push_back(1);
  cout << nums.capacity() << endl;
  return 0;
}
Related lesson →

Boolean alpha off

#include <iostream>
using namespace std;

int main() {
  cout << true << " " << boolalpha << true << endl;
  return 0;
}
Related lesson →

Fixed precision

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

int main() {
  cout << fixed << setprecision(2) << 3.14159 << endl;
  return 0;
}
Related lesson →

Newline vs endl

#include <iostream>
using namespace std;

int main() {
  cout << "line\n";
  return 0;
}
Related lesson →

Comment reminder

#include <iostream>
using namespace std;

int main() {
  // change a value, compile again
  cout << 1 + 1 << endl;
  return 0;
}
Related lesson →

STEM

Resistor power

#include <iostream>
using namespace std;

int main() {
  double I = 0.050;
  double R = 100.0;
  cout << I * I * R << " W" << endl;
  return 0;
}
Related lesson →

Hypotenuse force

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

int main() {
  cout << hypot(5.0, 12.0) << " N" << endl;
  return 0;
}
Related lesson →

Gravitational PE

#include <iostream>
using namespace std;

double pe(double m, double h) {
  return m * 9.81 * h;
}

int main() {
  cout << pe(0.50, 2.0) << " J" << endl;
  return 0;
}
Related lesson →

Density

#include <iostream>
using namespace std;

int main() {
  double m = 54.0;
  double V = 20.0;
  cout << m / V << " g/cm^3" << endl;
  return 0;
}
Related lesson →

gcd Euclid

#include <iostream>
using namespace std;

int main() {
  int a = 48, b = 18;
  while (b) { int r = a % b; a = b; b = r; }
  cout << a << endl;
  return 0;
}
Related lesson →

RNA codon

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

int main() {
  string codon = "AUG";
  cout << codon << " " << codon.size() << endl;
  return 0;
}
Related lesson →

Element Z map

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

int main() {
  map<string, int> z;
  z["Fe"] = 26;
  cout << z["Fe"] << endl;
  return 0;
}
Related lesson →

Titre mean

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

int main() {
  vector<double> ml = {24.6, 24.7, 24.6};
  double s = 0;
  for (double v : ml) s += v;
  cout << s / ml.size() << endl;
  return 0;
}
Related lesson →

n = cV

#include <iostream>
using namespace std;

class Solution {
 public:
  double conc, vol;
  double moles() { return conc * vol; }
};

int main() {
  Solution sol;
  sol.conc = 0.100;
  sol.vol = 0.25;
  cout << sol.moles() << endl;
  return 0;
}
Related lesson →

Photon energy

#include <iostream>
using namespace std;

int main() {
  const double h = 6.626e-34;
  cout << h * 5.00e14 << " J" << endl;
  return 0;
}
Related lesson →

FAQ: C++ Examples

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ examples examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ examples in this C++ C++ lesson (C++ Examples).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.