WebsiteTemplate/quickref/cpp.html
2026-01-25 11:33:37 -04:00

397 lines
12 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
<meta http-equiv="X-Content-Type-Options" content="nosniff">
<link rel="stylesheet" href="../assets/css/style.css">
<link rel="icon" type="image/x-icon" href="../favicon.ico">
<script>
// Apply theme immediately to prevent flash
(function() {
const theme = localStorage.getItem('theme') ||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', theme);
})();
</script>
<title>C++ Quickstart Reference - Quick Reference - Launch Pad</title>
</head>
<body>
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
</button>
<br/><br/>
<div class="name">
__ _______________________ _________._________________________
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
\/ \/ \/ \/ \/ \/ \/
</div>
<div class="blog-page-header">
<div class="blog-header-content">
<a href="/quickref" class="back-link" title="Back to Quick Reference">
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
</a>
<h1 class="blog-page-title">C++ Quickstart Reference</h1>
</div>
</div>
<div class="blog-post-container">
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
<div class="blog-post">
<div class="blog-post-content">
<p><a href="index.html">← Back to quick reference</a></p>
<p><a href="../index.html">← Home</a></p>
<hr>
<p>Quick reference guide for C++. Essential syntax, memory management, STL containers, and common patterns for systems programming.</p>
<hr>
<h2>Hello World</h2>
<pre><code>#include &lt;iostream&gt;
int main() {
std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl; // Standard output
return 0;
}</code></pre>
<hr>
<h2>Variables</h2>
<pre><code>int x = 42; // integer
double pi = 3.14; // floating point
std::string name = "Alice"; // string class
bool flag = true; // boolean
char c = 'A'; // character
// Type modifiers
unsigned int u = 100; // Unsigned
long long big = 1000000; // Long integer
float f = 3.14f; // Single precision
// Auto type deduction (C++11)
auto y = 42; // Compiler infers int
auto text = "Hello"; // Compiler infers const char*
</code></pre>
<hr>
<h2>Control Flow</h2>
<h3>If/Else</h3>
<pre><code>if (x > 10) {
std::cout &lt;&lt; "x is big\n";
} else {
std::cout &lt;&lt; "x is small\n";
}
if (x > 10) {
std::cout &lt;&lt; "big\n";
} else if (x > 5) {
std::cout &lt;&lt; "medium\n";
} else {
std::cout &lt;&lt; "small\n";
}
// Ternary operator
int result = (x > 10) ? 100 : 0;
</code></pre>
<h3>Switch</h3>
<pre><code>switch (x) {
case 10:
std::cout &lt;&lt; "ten\n";
break;
case 20:
std::cout &lt;&lt; "twenty\n";
break;
default:
std::cout &lt;&lt; "other\n";
}
</code></pre>
<h3>For Loops</h3>
<pre><code>// Traditional for loop
for (int i = 0; i &lt; 5; i++) {
std::cout &lt;&lt; i &lt;&lt; "\n";
}
// Range-based for loop (C++11)
int nums[] = {1, 2, 3};
for (int n : nums) {
std::cout &lt;&lt; n &lt;&lt; "\n";
}
std::vector&lt;int&gt; vec = {4, 5, 6};
for (int n : vec) {
std::cout &lt;&lt; n &lt;&lt; "\n";
}
// With auto
for (auto n : vec) {
std::cout &lt;&lt; n &lt;&lt; "\n";
}
</code></pre>
<h3>While Loops</h3>
<pre><code>while (x > 0) {
x--;
std::cout &lt;&lt; x &lt;&lt; "\n";
}
// Do...while
do {
x--;
} while (x > 0);
</code></pre>
<hr>
<h2>Functions</h2>
<pre><code>int add(int a, int b) {
return a + b;
}
std::cout &lt;&lt; add(2, 3) &lt;&lt; "\n";
// Function overloading
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
// Default parameters
void greet(std::string name = "World") {
std::cout &lt;&lt; "Hello, " &lt;&lt; name &lt;&lt; "\n";
}
// Pass by reference
void increment(int&amp; x) {
x++;
}
// Pass by pointer
void setValue(int* ptr, int value) {
*ptr = value;
}
</code></pre>
<hr>
<h2>Arrays / Vectors</h2>
<h3>Arrays</h3>
<pre><code>int nums[] = {1, 2, 3}; // Fixed-size array
int arr[5] = {1, 2, 3, 4, 5}; // Explicit size
for (int n : nums) {
std::cout &lt;&lt; n &lt;&lt; "\n";
}
// Array size
int size = sizeof(nums) / sizeof(nums[0]);
</code></pre>
<h3>Vectors</h3>
<pre><code>#include &lt;vector&gt;
std::vector&lt;int&gt; vec = {4, 5, 6}; // Dynamic array
vec.push_back(7); // Add to end
vec.pop_back(); // Remove from end
vec.size(); // Size
vec[0]; // Access by index
vec.at(0); // Safe access (throws if out of bounds)
// Iterate
for (auto it = vec.begin(); it != vec.end(); ++it) {
std::cout &lt;&lt; *it &lt;&lt; "\n";
}
// Range-based for
for (int n : vec) {
std::cout &lt;&lt; n &lt;&lt; "\n";
}
</code></pre>
<h3>Other STL Containers</h3>
<pre><code>#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;set&gt;
#include &lt;unordered_map&gt;
// String
std::string str = "Hello";
str.length();
str += " World";
str.substr(0, 5);
// Map
std::map&lt;std::string, int&gt; person;
person["age"] = 30;
person["name"]; // Access (creates if doesn't exist)
// Set
std::set&lt;int&gt; numbers = {1, 2, 3};
numbers.insert(4);
numbers.find(2); // Returns iterator
// Unordered map (hash map)
std::unordered_map&lt;std::string, int&gt; hash_map;
hash_map["key"] = 42;
</code></pre>
<hr>
<h2>Input</h2>
<pre><code>std::string input_name;
std::cout &lt;&lt; "Enter your name: ";
std::cin &gt;&gt; input_name; // Whitespace-delimited
std::cout &lt;&lt; "Hello " &lt;&lt; input_name &lt;&lt; "\n";
// Full line input
std::string full_line;
std::cout &lt;&lt; "Enter a line: ";
std::getline(std::cin, full_line); // Reads entire line
std::cout &lt;&lt; "You entered: " &lt;&lt; full_line &lt;&lt; "\n";
// Number input
int number;
std::cout &lt;&lt; "Enter a number: ";
std::cin &gt;&gt; number;
std::cout &lt;&lt; "You entered: " &lt;&lt; number &lt;&lt; "\n";
</code></pre>
<hr>
<h2>Pointers &amp; References</h2>
<pre><code>int x = 42;
int* ptr = &amp;x; // Pointer to x
int&amp; ref = x; // Reference to x
*ptr = 100; // Dereference pointer
ref = 200; // Modify through reference
// Null pointer
int* null_ptr = nullptr; // C++11 (prefer over NULL)
// Dynamic allocation (avoid if possible)
int* dynamic = new int(42);
delete dynamic; // Must delete what you new
// Smart pointers (preferred)
#include &lt;memory&gt;
std::unique_ptr&lt;int&gt; smart = std::make_unique&lt;int&gt;(42);
// Automatically deleted when out of scope
</code></pre>
<hr>
<h2>Classes</h2>
<pre><code>class Person {
private:
std::string name;
int age;
public:
// Constructor
Person(std::string n, int a) : name(n), age(a) {}
// Getter
std::string getName() const {
return name;
}
// Setter
void setAge(int a) {
age = a;
}
// Method
void greet() const {
std::cout &lt;&lt; "Hello, I'm " &lt;&lt; name &lt;&lt; "\n";
}
};
Person person("Alice", 30);
person.greet();
</code></pre>
<hr>
<h2>File Operations</h2>
<pre><code>#include &lt;fstream&gt;
// Reading
std::ifstream file("file.txt");
std::string line;
while (std::getline(file, line)) {
std::cout &lt;&lt; line &lt;&lt; "\n";
}
file.close();
// Writing
std::ofstream out("file.txt");
out &lt;&lt; "Hello, World!\n";
out.close();
// Check if file opened
if (file.is_open()) {
// File operations
} else {
std::cerr &lt;&lt; "Failed to open file\n";
}
</code></pre>
<hr>
<h2>Error Handling</h2>
<pre><code>#include &lt;stdexcept&gt;
// Throwing exceptions
void divide(int a, int b) {
if (b == 0) {
throw std::runtime_error("Division by zero");
}
return a / b;
}
// Catching exceptions
try {
int result = divide(10, 0);
} catch (const std::exception&amp; e) {
std::cerr &lt;&lt; "Error: " &lt;&lt; e.what() &lt;&lt; "\n";
}
</code></pre>
<hr>
<h2>Tips</h2>
<ul>
<li>Always prefer <strong>stack allocation</strong> (<code>int x;</code>) over <code>new</code>/<code>delete</code> unless necessary.</li>
</ul>
<ul>
<li>If you must allocate dynamically, use <strong>smart pointers</strong> (<code>std::unique_ptr</code>, <code>std::shared_ptr</code>) to avoid leaks.</li>
</ul>
<ul>
<li>Remember to <code>#include</code> necessary headers (<code>&lt;iostream&gt;</code>, <code>&lt;vector&gt;</code>, <code>&lt;string&gt;</code>).</li>
</ul>
<ul>
<li>Use RAII (Resource Acquisition Is Initialization) to manage resources safely.</li>
</ul>
<ul>
<li><code>std::cin</code> input is whitespace-delimited; use <code>std::getline()</code> for full lines.</li>
</ul>
<ul>
<li>Watch for buffer overflows with arrays; prefer <code>std::vector</code> or <code>std::string</code>.</li>
</ul>
<ul>
<li>C++ variables are strongly typed; type conversions are explicit.</li>
</ul>
<ul>
<li>Use <code>const</code> for values that shouldn't change; <code>const</code> correctness is important.</li>
</ul>
<ul>
<li>Prefer range-based for loops (<code>for (auto x : container)</code>) when possible.</li>
</ul>
<ul>
<li>Use <code>auto</code> for type deduction when the type is obvious from context.</li>
</ul>
<ul>
<li>Initialize variables; uninitialized variables contain garbage values.</li>
</ul>
<ul>
<li>Use references (<code>&amp;</code>) instead of pointers when you don't need null or reassignment.</li>
</ul>
<ul>
<li>Prefer standard library containers (<code>std::vector</code>, <code>std::string</code>) over C-style arrays.</li>
</ul>
<ul>
<li>Use <code>nullptr</code> instead of <code>NULL</code> or <code>0</code> for null pointers (C++11).</li>
</ul>
</div>
</div>
</div>
</div>
<script async type="text/javascript" src="../blog/analytics.js"></script>
<script src="../theme.js"></script>
</body>
</html>