C++ Online Compiler - รัน, ทดสอบ, และดีบั๊กได้ทันที

เขียน, คอมไพล์, และรันโค้ด C++ ได้ทันทีในเบราว์เซอร์ของคุณด้วย C++ compiler ออนไลน์ฟรีของเรา เหมาะสำหรับการเรียนรู้, การทดสอบอย่างรวดเร็ว, และการเตรียมตัวสัมภาษณ์ — ไม่ต้องตั้งค่าหรือดาวน์โหลดโปรแกรมใดๆ

💡 คู่มือพื้นฐาน C++ สำหรับผู้เริ่มต้น

1. การประกาศตัวแปรและค่าคงที่

C++ ต้องการให้คุณประกาศประเภทของตัวแปรแต่ละตัว ใช้ const เพื่อกำหนดค่าที่อ่านได้เท่านั้นและไม่สามารถเปลี่ยนแปลงหลังจากการเริ่มต้นได้

int age = 30;
double pi = 3.14159;
char grade = 'A';
std::string name = "Alice";
bool isActive = true;

// ค่าคงที่
const int MAX_USERS = 100;
const std::string COMPANY = "CodeUtility";

// การพยายามแก้ไข const จะทำให้เกิดข้อผิดพลาดในขณะคอมไพล์
// MAX_USERS = 200; // ❌ error

2. เงื่อนไข (if / switch)

ใช้ if, else if, else สำหรับการแยกสาขา ใช้ switch สำหรับหลายค่า

int x = 2;
if (x == 1) {
    std::cout << "One\n";
} else if (x == 2) {
    std::cout << "Two\n";
} else {
    std::cout << "Other\n";
}

// Switch-case
switch (x) {
    case 1:
        std::cout << "One\n";
        break;
    case 2:
        std::cout << "Two\n";
        break;
    default:
        std::cout << "Other\n";
}

3. ลูป

ใช้ for, while, และ do-while เพื่อทำซ้ำบล็อกโค้ด

for (int i = 0; i < 3; i++) {
    std::cout << i << "\n";
}

int n = 3;
while (n > 0) {
    std::cout << n << "\n";
    n--;
}

4. อาเรย์

อาเรย์เก็บหลายองค์ประกอบที่มีประเภทเดียวกัน

int numbers[3] = {10, 20, 30};
std::cout << numbers[1] << "\n";

5. การจัดการอาเรย์/เวกเตอร์

ใช้ std::vector สำหรับอาเรย์แบบไดนามิก

#include <vector>

std::vector nums = {1, 2, 3};
nums.push_back(4);
nums.pop_back();

for (int n : nums) {
    std::cout << n << " ";
}

6. การรับ/ส่งข้อมูลทางคอนโซล

ใช้ std::cin สำหรับการรับข้อมูลและ std::cout สำหรับการส่งข้อมูล

std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "\n";

7. ฟังก์ชัน

ฟังก์ชันรวมกลุ่มตรรกะที่ใช้ซ้ำได้ ใช้พารามิเตอร์และประเภทการคืนค่า

int add(int a, int b) {
    return a + b;
}

std::cout << add(3, 4);

8. แผนที่

std::map เก็บคู่คีย์-ค่าเหมือนพจนานุกรม

#include <map>

std::map ages;
ages["Alice"] = 30;
std::cout << ages["Alice"];

9. การจัดการข้อยกเว้น

ใช้ try และ catch เพื่อจัดการข้อผิดพลาดในขณะรันไทม์

try {
    throw std::runtime_error("Error occurred");
} catch (const std::exception& e) {
    std::cout << e.what();
}

10. การรับ/ส่งข้อมูลจากไฟล์

ใช้ fstream เพื่ออ่านจากและเขียนไปยังไฟล์

#include <fstream>

std::ofstream out("file.txt");
out << "Hello File";
out.close();

std::ifstream in("file.txt");
std::string line;
getline(in, line);
std::cout << line;
in.close();

11. การจัดการสตริง

สตริงใน C++ รองรับเมธอดต่างๆ เช่น length(), substr(), และ find()

std::string text = "Hello World";
std::cout << text.length();
std::cout << text.substr(0, 5);
std::cout << text.find("World");

12. คลาสและออบเจ็กต์

C++ รองรับการเขียนโปรแกรมเชิงวัตถุโดยใช้คลาส

class Person {
  public:
    std::string name;
    Person(std::string n) : name(n) {}
    void greet() { std::cout << "Hi, I'm " << name; }
};

Person p("Alice");
p.greet();

13. พอยน์เตอร์

พอยน์เตอร์เก็บที่อยู่หน่วยความจำของตัวแปรอื่น ใช้ & เพื่อรับที่อยู่ และ * เพื่อเข้าถึงค่าในที่อยู่นั้น

int x = 10;
int* ptr = &x;

std::cout << "Value of x: " << x << "\n";
std::cout << "Address of x: " << ptr << "\n";
std::cout << "Value from pointer: " << *ptr << "\n";

// แก้ไข x ผ่านพอยน์เตอร์
*ptr = 20;
std::cout << "Updated x: " << x << "\n";