You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A self-contained C++17 interpreter that extends the C interpreter with classes, templates, namespaces, STL containers, RAII, and modern C++ features. Lexes, parses, and interprets C++ code at runtime. No JIT, no code generation, no external compiler needed.
Edits .cpp / .cc / .cxx / .hpp / .h files in the shared Monaco-powered editor with C++-mode syntax highlighting and auto-save (debounced ~600 ms; also flushed on run, tab switch, view disappear, and app backgrounding).
// Stack allocation
Point p(3.0, 4.0);
// Heap allocation
Point* ptr = new Point(5.0, 6.0);
cout << ptr->getX() << endl;
delete ptr;
// Array allocationint* arr = newint[10];
for (int i = 0; i < 10; i++) arr[i] = i * i;
delete[] arr;
Operator
Description
new T(args)
Allocate single object on heap, call constructor
new T[n]
Allocate array of n objects
delete ptr
Destroy object and free memory
delete[] arr
Destroy array and free memory
References
int x = 42;
int& ref = x; // ref is an alias for x
ref = 100;
cout << x << endl; // 100// Pass by referencevoidswap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
// Const referencevoidprint(const string& s) {
cout << s << endl;
}
Find substring (returns string::npos if not found)
s.rfind(str)
Reverse find
s.replace(pos, len, str)
Replace substring
s.append(str) / s += str
Append
s.insert(pos, str)
Insert at position
s.erase(pos, len)
Erase characters
s.c_str()
C-string pointer
s[i] / s.at(i)
Character access
s.front() / s.back()
First/last character
s.push_back(c)
Append character
s.pop_back()
Remove last character
s.compare(str)
Compare strings
to_string(n)
Convert number to string
stoi(s) / stod(s)
Convert string to int/double
std::vector<T>
Method
Description
vector<int> v
Default construction
vector<int> v(n, val)
N elements with value
vector<int> v = {1,2,3}
Initializer list
v.push_back(x)
Add element to end
v.pop_back()
Remove last element
v.size()
Number of elements
v.empty()
Check if empty
v[i] / v.at(i)
Element access
v.front() / v.back()
First/last element
v.begin() / v.end()
Iterator range
v.insert(it, val)
Insert at iterator position
v.erase(it)
Erase at iterator position
v.clear()
Remove all elements
v.resize(n)
Resize container
v.reserve(n)
Reserve capacity
v.capacity()
Current capacity
std::map<K, V>
Method
Description
map<string, int> m
Default construction
m[key] = value
Insert or update
m.at(key)
Access with bounds check
m.find(key)
Find element (returns iterator)
m.count(key)
Check existence (0 or 1)
m.erase(key)
Remove by key
m.size()
Number of elements
m.empty()
Check if empty
m.begin() / m.end()
Iterator range
m.insert({key, value})
Insert pair
m.clear()
Remove all
std::pair<A, B>
pair<string, int> p = {"hello", 42};
cout << p.first << "" << p.second << endl;
auto p2 = make_pair(3.14, "pi");
std::set<T>
Method
Description
set<int> s
Default construction
s.insert(x)
Insert element
s.erase(x)
Remove element
s.count(x)
Check existence (0 or 1)
s.find(x)
Find element (iterator)
s.size()
Number of elements
s.empty()
Check if empty
s.begin() / s.end()
Iterator range
s.clear()
Remove all
STL Algorithms (<algorithm>)
Function
Description
sort(begin, end)
Sort range in ascending order
sort(begin, end, comp)
Sort with custom comparator
reverse(begin, end)
Reverse range
find(begin, end, value)
Find first occurrence
count(begin, end, value)
Count occurrences
min_element(begin, end)
Iterator to minimum
max_element(begin, end)
Iterator to maximum
accumulate(begin, end, init)
Sum elements (from <numeric>)
binary_search(begin, end, value)
Binary search (sorted range)
lower_bound(begin, end, value)
First element >= value
upper_bound(begin, end, value)
First element > value
unique(begin, end)
Remove consecutive duplicates
fill(begin, end, value)
Fill range with value
copy(begin, end, dest)
Copy range
swap(a, b)
Swap two values
next_permutation(begin, end)
Next lexicographic permutation
for_each(begin, end, func)
Apply function to each element
transform(begin, end, dest, func)
Transform each element
remove_if(begin, end, pred)
Remove elements matching predicate
I/O Streams
std::cout / std::cin
#include<iostream>usingnamespacestd;// Output
cout << "Hello, " << "World!" << endl;
cout << "x = " << 42 << ", pi = " << 3.14 << endl;
// Input (returns default values on iOS -- no stdin)int n;
cin >> n; // n = 0 (no stdin on iOS)
string line;
getline(cin, line); // line = "" (no stdin)
I/O Manipulators
Manipulator
Description
endl
Newline + flush
setw(n)
Set field width
setprecision(n)
Set decimal precision
fixed
Fixed-point notation
scientific
Scientific notation
left / right
Alignment
setfill(c)
Fill character
hex / oct / dec
Integer base
boolalpha
Print bool as "true"/"false"
showpoint
Always show decimal point
Modern C++ Features
auto Type Deduction
auto x = 42; // intauto pi = 3.14; // doubleauto s = string("hi"); // stringauto v = vector<int>{1, 2, 3}; // vector<int>
Range-Based For Loop
vector<int> nums = {1, 2, 3, 4, 5};
for (auto n : nums) {
cout << n << "";
}
for (auto& n : nums) { // By reference (modifiable)
n *= 2;
}
map<string, int> m = {{"a", 1}, {"b", 2}};
for (auto& [key, value] : m) { // Structured bindings (C++17)
cout << key << "=" << value << endl;
}
Lambda Expressions
// Basic lambdaauto add = [](int a, int b) { return a + b; };
cout << add(3, 4) << endl; // 7// Capture by valueint multiplier = 3;
auto mul = [multiplier](int x) { return x * multiplier; };
cout << mul(5) << endl; // 15// Capture by referenceint total = 0;
auto accumulate = [&total](int x) { total += x; };
accumulate(10);
accumulate(20);
cout << total << endl; // 30// Capture all by value [=] or reference [&]auto f = [=]() { return multiplier * 2; };
auto g = [&]() { total += 100; };
// Lambda as sort comparator
vector<int> v = {5, 2, 8, 1, 9};
sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
// v = {9, 8, 5, 2, 1}
Templates
// Function templatetemplate<typename T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}
cout << maximum(3, 7) << endl; // 7
cout << maximum(3.14, 2.71) << endl; // 3.14// Class templatetemplate<typename T>
classStack {
vector<T> data;
public:voidpush(T val) { data.push_back(val); }
T pop() {
T val = data.back();
data.pop_back();
return val;
}
boolempty() const { return data.empty(); }
intsize() const { return data.size(); }
};
Stack<int> intStack;
intStack.push(1);
intStack.push(2);
cout << intStack.pop() << endl; // 2
Stack<string> strStack;
strStack.push("hello");
Exception Handling
#include<stdexcept>try {
int x = 10, y = 0;
if (y == 0) throwruntime_error("Division by zero!");
cout << x / y << endl;
} catch (const runtime_error& e) {
cout << "Error: " << e.what() << endl;
} catch (...) {
cout << "Unknown error" << endl;
}
// Custom exceptionclassMyError : publicexception {
string msg;
public:MyError(string m) : msg(m) {}
constchar* what() constnoexceptoverride { return msg.c_str(); }
};
Supported C Features (inherited from C interpreter)
All C89/C99/C23 features from the C interpreter are available:
All C operators (48), control flow, functions, pointers, arrays (1D/2D)