C++ Quick Reference
C++ Quick Reference
<< Cheat-Sheets.org
PREPROCESSOR
// Comment to end of line
/* Multi-line comment */
#define X \
LITERALS
'\n', '\\', '\'', '\"' // Newline, backslash, single quote, double quote
DECLARATIONS
int x; // Declare x to be an integer (value undefined)
enum weekend {SAT,SUN}; // weekend is a type with values SAT and SUN
const int* const p=a; // Both p and its contents are constant
www.cheat-sheets.org/saved-copy/cppqref.20210603.html 1/7
10/10/21, 7:21 PM C++ QUICK REFERENCE
STORAGE CLASSES
int x; // Auto (memory exists only while in scope)
STATEMENTS
x=y; // Every expression is a statement
; // Empty statement
try { a; }
FUNCTIONS
int f(int x, int); // f is a function taking 2 ints and returning int
www.cheat-sheets.org/saved-copy/cppqref.20210603.html 2/7
10/10/21, 7:21 PM C++ QUICK REFERENCE
EXPRESSIONS
typeid(x) // Type of x
~x // Bitwise complement of x
-x // Unary minus
&x // Address of x
x * y // Multiply
x + y // Add, or &x[y]
x == y // Equals
x != y // Not equals
x ^ y // Bitwise exclusive or (3 ^ 6 is 5)
www.cheat-sheets.org/saved-copy/cppqref.20210603.html 3/7
10/10/21, 7:21 PM C++ QUICK REFERENCE
x | y // Bitwise or (3 | 6 is 7)
CLASSES
class T { // A new type
};
class X: public virtual T {}; // Classes derived from X have base T directly
TEMPLATES
template <class T> T f(T t); // Overload f for all types
template <class T, class U=T, int n=0> // Template with default parameters
www.cheat-sheets.org/saved-copy/cppqref.20210603.html 4/7
10/10/21, 7:21 PM C++ QUICK REFERENCE
NAMESPACES
Only the most commonly used functions are listed. Header files without
.h are in namespace std. File names
are actually lower case.
// Mode may also be "w" (write) "a" append, "a+" update, "rb" binary
"%%" // %
getchar(); // getc(stdin);
fgets(s, n, f); // Read line into char s[n] from f. NULL if EOF
ferror(f); // Error in f?
Strings are type char[] with a '\0' in the last element used.
www.cheat-sheets.org/saved-copy/cppqref.20210603.html 5/7
10/10/21, 7:21 PM C++ QUICK REFERENCE
strcpy(dst, src); // Copy string. Not bounds checked
// mem... functions are for any pointer types (void*), length n bytes
sec, min, hour, mday, mon (0-11), year (-1900), wday, yday, isdst
c = cin.get(); // c = getchar();
www.cheat-sheets.org/saved-copy/cppqref.20210603.html 6/7
10/10/21, 7:21 PM C++ QUICK REFERENCE
f1 >> x; // Read object from file
s1[0]; // 'h'
a.back()=4; // a[10]=4;
a.front(); // a[0];
DEQUE (array/stack/queue)
UTILITY (Pair)
pair<string, int> a("hello", 3); // A 2-element struct
a.first; // "hello"
a.second; // 3
a.size(); // 1
www.cheat-sheets.org/saved-copy/cppqref.20210603.html 7/7