C++ Code Support
Support for C++ is currently limited and continues to improve. This guide explains the current supported features and limitations.
Please adhere to the following rules to avoid using incompatible syntax features.
Common issues
Common issues
Supported Features
1. Basic Data Types
The following basic types are supported for declaration and usage:
char, string, bool, int, float, double
Example:
int num = 42;
string name = "Alice";
double pi = 3.1415;
Generating interactive preview...
2. Standard Containers
The following containers and their nested forms are supported:
| Container Type | Example |
|---|
vector<T> | vector<int> nums3 |
vector<vector<T>> | 2D dynamic array |
map<T1, T2> | map<int, string> |
unordered_map<T1,T2> | Hash table |
queue<T> | Queue |
set<T> | Set |
unordered_set<T> | Set |
stack<T> | Stack |
Constraints:
- Keys in
map/unordered_map must be basic types (char/string/bool/int/float/double).
- Container nesting depth is limited to 2 levels (e.g.,
vector<vector<int>>).
3. Built-in Data Structures
The following predefined structures can be directly used:
template <typename T>
struct ListNode {
int val;
ListNode* next;
ListNode(T x, ListNode *next = nullptr) : val(x), next(next) {}
};
template <typename T>
struct TreeNode {
T val;
TreeNode* left;
TreeNode* right;
TreeNode(T x, TreeNode *left = nullptr, TreeNode *right = nullptr) : val(x), left(left), right(right);
};
Generating interactive preview...
4. Language Features
- Code must include a
main function with an int return type and explicitly return an int.
// Includes `main` function and explicitly returns `0`
int main() {
// code
return 0;
}
// Return type is not `int`
void main() {
// code
}
// No explicit return
int main() {
// code
}
Generating interactive preview...
- Supported: Functions and class methods (excluding static class methods).
- Supported: Lambda expressions, but variables must be explicitly captured (e.g.,
[&]{}).
- Common headers (e.g.,
<vector>, <algorithm>) are pre-included; no manual inclusion is needed.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <utility>
Limitations and Notes
Unsupported Syntax
| Category | Example | Remarks |
|---|
| Header Inclusion | #include <sstream> | Common libraries are pre-included |
| Static Methods | MyClass::staticFunc() | Static class methods are unsupported |
Anonymous Lambdas (without &) | [](){ ... } | Must use explicit capture like [&](){ ... } |
stringstream | ss << "text" | Use alternatives like to_string |
| In-function Structs | void func() { struct X } | Structs must be defined globally or within a class |
Other Restrictions
- Incomplete class support; prioritize standalone functions or class methods.
- Avoid complex templates; custom template classes may fail to parse.
- Non-basic types as map keys are unsupported. Example of invalid code:
map<vector<int>, int> mp; // Key must be a basic type
Generating interactive preview...
Code Examples
// Using containers and algorithms
vector<vector<int>> matrix = {{1,2}, {3,4}};
sort(matrix[0].begin(), matrix[0].end());
// Manipulating linked lists
ListNode<int>* head = new ListNode(0);
head->next = new ListNode(1);
#include <unordered_set> // Manual header inclusion is unsupported
struct MyStruct {
static void helper() { ... } // Static methods are unsupported
};
void badExample() {
stringstream ss; // stringstream is unsupported
map<set<int>, int> mp; // Container as map key is invalid
}
Generating interactive preview...
Adjust your code according to the above rules. Contact platform support if issues arise.