Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. main() ใน C และ C++ ควร return ค่าอะไร

    Return 0 (หรือ EXIT_SUCCESS): โปรแกรมจบปกติ (success) Return non-zero (หรือ EXIT_FAILURE): โปรแกรมจบผิดปกติ (error, invalid input, crash) Non-zero codes ไม่มีมาตรฐาน แต่อาจกำหนดเองแทน error types ต่างๆ GCC แนะนำใช้ EXIT_SUCCESS/EXIT_FAILURE จาก <cstdlib> (stdlib.h) C++ เท่านั้น: int main() ส

  2. ความแตกต่างระหว่าง Undefined, Unspecified, และ Implementation-defined Behavior ใน C/C++

    Undefined Behavior (UB) พฤติกรรมที่มาตรฐาน ไม่ได้กำหนดเลย Compiler/OS ทำอะไรก็ได้ (crash, ผลผิด, ทำงานได้, security hole) ตัวอย่าง: i++ + ++i, u = (u++), dereference null pointer, buffer overflow Implementation-defined Behavior มาตรฐานไม่กำหนด แต่ implementation (compiler) ต้องเลือกหนึ่งทางและ docu

  3. ทำไม sorted array เร็วกว่า unsorted array ใน C++

    เพราะ Branch Prediction (การคาดการณ์分支) ใน CPU architecture Branch predictor พยายามทายว่า conditional branch (if/else) จะถูกทำหรือไม่ เพื่อโหลด instruction ล่วงหน้า (pipeline) ตัวอย่าง if (arr[i] > 50) { do_B(); } else { do_A(); } Sorted array: 1, 2, 3... 50, 51... 100 A, A, A, A... A, B, B, B..

  4. วิธีใช้ Arrays ใน C++

    Array = ชุดข้อมูลชนิดเดียวกัน ติดกันในหน่วยความจำ เข้าถึงด้วย index (เริ่ม 0) การประกาศและใช้งาน int arr[10]; // ประกาศ array int ขนาด 10 (index 0-9) เข้าถึง element arr[2] // element ที่ 3 (index 2) ตัวอย่าง: รับค่าและแสดงค่าทั้งหมด #include <iostream> using namespace std; int main() {

  5. ความแตกต่างระหว่าง Pointer และ Reference ใน C++

    Reference (อ้างอิง) ชื่อเรียกทางเลือก (alias) สำหรับตัวแปรที่มีอยู่แล้ว Type &newName = existingVar; // ต้อง initialize ทันที Pointer (ตัวชี้) เก็บ address ของตัวแปร Type *ptr; // ประกาศ ptr = &variable; // กำหนด address ความแตกต่างหลัก Reference เป็น alias ของตัวแปรเดิม | Pointer

  6. Copy Elision และ Return Value Optimization (RVO) ใน C++

    Copy Elision = การเพิ่มประสิทธิภาพของคอมไพเลอร์ที่ข้ามการ copy/move object ที่ไม่จำเป็น (ไม่มี side effects) ตัวอย่าง struct MyStruct { MyStruct() {} MyStruct(const MyStruct&) { std::cout << Copied\n; } }; MyStruct f() { return MyStruct(); } int main() { std::cout << Mai

  7. ทำไม sizeof(struct) ไม่เท่ากับผลรวม sizeof ของแต่ละ member

    เพราะ padding และ alignment (การจัดแนวหน่วยความจำ) Processor แต่ละสถาปัตยกรรมมี word size (32-bit = 4 bytes, 64-bit = 8 bytes) ตัวแปรแต่ละชนิดมี alignment requirement ตัวอย่าง struct X { char b[3]; // 3 bytes int c; // 4 bytes, ต้อง align ที่ 4-byte boundary }; // sizeof(b) + sizeof(c

  8. Array Decaying ใน C/C++ คืออะไร

    Array และ pointer ทำงานคล้ายกัน แต่ sizeof ทำงานต่างกัน เมื่อ array decay กลายเป็น pointer (เช่น ส่งไปฟังก์ชัน) จะสูญเสียข้อมูลขนาด ตัวอย่าง #include <iostream> int main() { const int a[] = { 2, 3, 5, 7, 11 }; const int* p = a; // Array decay to pointer std::cout << ( sizeo

  9. เมื่อไหร่ควรใช้ Forward Declaration ใน C/C++

    Forward declaration บอกคอมไพเลอร์ว่ามีคลาสชื่อนี้อยู่ (จะนิยามภายหลัง) ใช้ได้เมื่อ: ต้องการใช้ชื่อคลาสก่อน definition (circular dependencies) ลด header dependencies (compile time) - forward declare ใน header, include จริงใน .cpp Pimpl idiom (Pointer to Implementation) ตัวอย่าง class Person; // F

  10. Smart Pointer ใน C++ คืออะไร และเมื่อไหร่ควรใช้

    Smart Pointer = คลาสที่ wrap raw pointer จัดการ resource (หน่วยความจำ) ให้อัตโนมัติ ทำหน้าที่คล้าย garbage collector ประเภทหลัก (C++11+) std::unique_ptr - exclusive ownership, ไม่ copy ได้ (move only) ใช้เป็น default choice std::shared_ptr - shared ownership, reference counting, copy ได้ std::weak

  11. หาเอกสารมาตรฐาน C และ C++ ล่าสุดที่ไหน

    C Standard: ซื้อได้จาก ANSI Webstore (ISO/IEC 9899) C++ Standard: ซื้อได้จาก ISO Website (ISO/IEC 14882) Working Draft (ฟรี): isocpp.org/std/the-standard - draft ล่าสุดของมาตรฐาน C++ หมายเหตุ: มาตรฐานอย่างเป็นทางการเป็นเอกสารลิขสิทธิ์ ต้องซื้อ แต่ working drafts อ่านฟรีได้ และใกล้เคียงมาตรฐานจริง

  12. เมื่อไหร่ควรใช้ static_cast, dynamic_cast, const_cast, reinterpret_cast ใน C++

    const_cast เพิ่ม/ลบ const qualifier จาก variable ใช้เมื่อจำเป็นต้องแก้ constness (เช่น API เก่าที่ไม่ const-correct) static_cast Type conversion ปกติ (ordinary) เช่น float→int, char→int, upcasting (derived→base) รับผิดชอบ implicit conversions dynamic_cast ใช้กับ polymorphism เท่านั้น: downcasting (

  13. C++ Singleton Design Pattern อธิบายและตัวอย่าง

    Singleton Pattern จำกัดการสร้าง instance ของคลาสให้มีเพียง หนึ่งเดียว ตลอดชีวิตของโปรแกรม ใช้เมื่อต้องการ object เดียวควบคุมการทำงานร่วมกัน (เช่น logger, config manager, database connection pool) การ implement แบบคลาสสิก #include <iostream> using namespace std; class Singleton { static S

  14. วิธีเข้าถึง local variable จากฟังก์ชันอื่นด้วย pointers ใน C++

    ปกติแล้วทำไม่ได้: local variable หายไปเมื่อออก scope (ฟังก์ชันจบ) การเข้าถึงผ่าน pointer ที่ชี้ไปที่ memory ที่ free แล้ว = Undefined Behavior ตัวอย่างที่อาจทำงาน (แต่ไม่ควรทำ) #include <iostream> int* foo() { int x = 3; return &x; // คืน address ของ local variable! } int main()

  15. วิธีเปรียบเทียบ float และ double ใน C++

    การเปรียบเทียบ floating-point ด้วย == ไม่เชื่อถือได้ เพราะ precision error ควรใช้ epsilon comparison ฟังก์ชันเปรียบเทียบแบบง่าย #include <iostream> #include <cmath> using namespace std; #define EPSILON 0.000001 bool areSame(double a, double b) { return fabs(a - b) < EPSILON; }

  16. วิธี set, clear, toggle bit ใน C/C++

    ใช้ bitwise operators พร้อม bitshift เพื่อเลื่อน bit ไปตำแหน่งที่ต้องการ 1. Set bit (ตั้งค่าเป็น 1) ใช้ bitwise OR (|) i |= (1 << n); // ตั้ง bit ที่ n เป็น 1 ตัวอย่าง: n=4 → 1<<4 = 16 (binary 10000) 2. Clear bit (ล้างค่าเป็น 0) ใช้ bitwise AND (&) กับ NOT (~) i &= ~(1 <<

  17. วิธีอ่านและ parse CSV ไฟล์ใน C++

    ควรใช้ library แทนการ parse เอง เพราะ CSV มีกรณีขอบมาก (quotes, escaped commas, newlines ใน field) วิธีที่ 1: ใช้ Boost.Tokenizer (แนะนำ) #include <iostream> #include <boost/tokenizer.hpp> #include <vector> #include <string> using namespace std; using namespace boost; vecto

  18. Tokenize String ใน C++ (แยกคำในสตริง)

    วิธีที่ 1: stringstream (แยกด้วย whitespace) #include <vector> #include <string> #include <sstream> using namespace std; int main() { string str(Hello from the dark side); string tmp; stringstream str_strm(str); vector<string> words; while (str_strm >

  19. เมื่อไหร่ควรใช้ virtual destructors ใน C++

    Scott Meyers (Effective C++) แนะนำ: ถ้าคลาสมี virtual function ใดๆ ควรมี virtual destructor ด้วย และคลาสที่ไม่ได้ออกแบบมาเป็น base class หรือใช้ polymorphically ไม่ควรมี virtual destructor เหตุผล: หากสร้าง object ด้วย derived constructor แต่ชี้ด้วย base pointer: Base *b = new Derived(); delete b;

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:12/301  20-คอมพิวเตอร์/Page Goto:1 6 7 8 9 10 11 12 13 14 15 16 17 18