Download PDF: C++ Programming Basics Cheat Sheet.pdf
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
int number = 10; // Integer
double pi = 3.14; // Double
char letter = 'A'; // Character
std::string greeting = "Hello"; // String (requires <string> header)
bool isTrue = true; // Boolean
int i = 100; // Integer
float f = 10.5f; // Floating point
double d = 10.5; // Double floating point
char c = 'A'; // Character
bool b = true; // Boolean
std::string s = "Hello"; // String (requires <string> header)
if (condition) {
// code block
} else if (anotherCondition) {
// another code block
} else {
// another code block
}
int day = 2;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
// other cases
default:
std::cout << "Invalid day" << std::endl;
}
for (int i = 0; i < 5; i++) {
std::cout << i << std::endl;
}
int i = 0;
while (i < 5) {
std::cout << i << std::endl;
i++;
}
int i = 0;
do {
std::cout << i << std::endl;
i++;
} while (i < 5);
#include <iostream>
#include <string>
int main() {
std::string s = "Hello, World!";
std::cout << s.length() << std::endl; // Output: 13
std::cout << s.substr(7, 5) << std::endl; // Output: World
std::cout << s.find("World") << std::endl; // Output: 7
std::string s2 = s.replace(7, 5, "C++");
std::cout << s2 << std::endl; // Output: Hello, C++!
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream file("filename.txt");
std::string line;
if (file.is_open()) {
while (getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
} else {
std::cout << "Unable to open file";
}
return 0;
}
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("filename.txt");
if (file.is_open()) {
file << "Hello, file!\n";
file.close();
} else {
std::cout << "Unable to open file";
}
return 0;
}
// This is a single-line comment
int x = 10; // This is an inline comment
/*
This is a multi-line comment
spanning multiple lines.
*/
int y = 20;
#include <iostream>
#include <stdexcept>
int main() {
try {
int result = 10 / 0;
} catch (const std::exception &e) {
std::cerr << "Exception: " << e.what() << std::endl;
} catch (...) {
std::cerr << "Unknown exception!" << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <utility>
+ // Addition
- // Subtraction
* // Multiplication
/ // Division
% // Modulus
++ // Increment
-- // Decrement
== // Equal to
!= // Not equal to
> // Greater than
< // Less than
>= // Greater than or equal to
<= // Less than or equal to
&& // Logical AND
|| // Logical OR
! // Logical NOT
#include <iostream>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
// Function call
int result = add(5, 3);
std::cout << result << std::endl; // Output: 8
return 0;
}
#include <iostream>
// Function declaration
int add(int, int);
int main() {
int result = add(5, 3);
std::cout << result << std::endl; // Output: 8
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
#include <iostream>
// Overloaded functions
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int main() {
std::cout << add(5, 3) << std::endl; // Output: 8
std::cout << add(5.5, 3.3) << std::endl; // Output: 8.8
return 0;
}
int numbers[5] = {1, 2, 3, 4, 5};
// Access elements
std::cout << numbers[0] << std::endl; // Output: 1
// Loop through array
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << std::endl;
}
// Using range-based for loop (C++11 and later)
for (int number : numbers) {
std::cout << number << std::endl;
}
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
// Access elements
std::cout << vec[0] << std::endl; // Output: 1
// Add elements
vec.push_back(6);
// Loop through vector
for (int i = 0; i < vec.size(); i++) {
std::cout << vec[i] << std::endl;
}
// Using range-based for loop (C++11 and later)
for (int val : vec) {
std::cout << val << std::endl;
}
return 0;
}
#include <iostream>
#include <map>
int main() {
std::map<std::string, int> ageMap;
ageMap["Alice"] = 30;
ageMap["Bob"] = 25;
// Access elements
std::cout << "Alice's age: " << ageMap["Alice"] << std::endl;
// Loop through map
for (const auto &pair : ageMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {1, 2, 3, 4, 5};
// Add element
mySet.insert(6);
// Check if element exists
if (mySet.find(3) != mySet.end()) {
std::cout << "Element found" << std::endl;
} else {
std::cout << "Element not found" << std::endl;
}
// Loop through set
for (int val : mySet) {
std::cout << val << std::endl;
}
return 0;
}
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 2, 3, 4, 5};
// Add elements
myList.push_back(6);
myList.push_front(0);
// Loop through list
for (int val : myList) {
std::cout << val << std::endl;
}
// Remove elements
myList.pop_front();
myList.pop_back();
return 0;
}