Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. ความแตกต่างระหว่าง #include <filename> และ #include "filename" ใน C/C++

    ความแตกต่างอยู่ที่ ตำแหน่งที่ preprocessor ค้นหาไฟล์ #include <filename> ค้นหาใน system include directories (ที่ compiler กำหนดไว้) ใช้สำหรับ standard library headers #include filename ค้นหาใน same directory กับไฟล์ที่มี directive นี้ก่อน หาไม่เจอค่อยค้นหาแบบ <filename> ใช้สำหรับ user-d

  2. วิธีประกาศ 2D array แบบ dynamic ด้วย new ใน C++

    2D array แบบ dynamic = array of pointers to arrays ต้อง allocate 2 ขั้นตอน: Allocate array ของ pointers (int**) Loop allocate แต่ละ row (1D array) ตัวอย่าง #include <iostream> using namespace std; int main() { int rows = 3, cols = 4; int** arr = new int*[rows]; // array ของ po

  3. ทำไม Variable-Length Arrays (VLA) ไม่อยู่ในมาตรฐาน C++

    VLA (Variable-Length Arrays)** = array ที่ขนาดกำหนดได้ตอน runtime (ไม่ใช่ compile-time constant) เหตุผลที่ไม่อยู่ในมาตรฐาน C++: สร้าง array ใหญ่บน stack ได้ (stack มีพื้นที่จำกัด) → stack overflow risk ต้องการการเปลี่ยนแปลงใหญ่โตใน type system ของ C++ C++ มีทางเลือกที่ดีกว่าและปลอดภัยกว่าแล้ว ทาง

  4. Rule of Three vs Rule of Five ใน C++

    Rule of Three (C++98/03) ถ้าคลาสต้องการ implement ตัวใดตัวหนึ่งใน 3 นี้ ควร implement ทั้ง 3: Copy Constructor Copy Assignment Operator Destructor เหตุผล: ถ้าจำเป็นต้องมีหนึ่งในนี้ แสดงว่าคลาสจัดการ dynamic resources ต้องการทั้งการ copy (ctor/assign) และการ cleanup (dtor) Rule of Five (C++11+) ขย

  5. Rule of Five ใน C++11 คืออะไร

    Rule of Five (กฎของ 5) ใช้สำหรับ resource management ใน C++11+ ขยายจาก Rule of Three โดยเพิ่ม move semantics หากคลาสต้องการจัดการ resource (memory, file handle, socket ฯลฯ) เอง ควร implement ทั้ง 5 ฟังก์ชันนี้: Destructor - ปล่อย resource เมื่อ object จบอายุ Copy Constructor - Deep copy resource C

  6. C++ vs C++0x vs C++11 vs C++98 - ความแตกต่างของมาตรฐาน

    C++98: มาตรฐานรุ่นแรก กำหนด language constructs, STL, standard library พื้นฐาน C++03: Bugfix release แก้ 92 core defects, 125 library defects เพิ่มแค่ value initialization C++0x: Working draft / codename คาดเสร็จ 2008-09 จริงเสร็จ 2011 C++11: มาตรฐานสมัยใหม่ 2011 เพิ่มฟีเจอร์ใหญ่ๆ: Initializer li

  7. Placement new ใน C++ ใช้ทำอะไร

    Placement new ให้สร้าง (construct) object บนหน่วยความจำที่ จัดสรรไว้แล้ว (pre-allocated) แทนที่จะ allocate ใหม่ รูปแบบ: new (address) Type(initializer) ประโยชน์: optimization - เร็วกว่า (ไม่ต้อง allocate ใหม่) reuse memory เหมาะสำหรับ memory pools, custom allocators, embedded systems ตัวอย่าง #incl

  8. ความแตกต่างระหว่าง C++0x และ C++11

    มาตรฐาน C/C++ มักตั้งชื่อตามปีที่เผยแพร่: C++98 (1998), C++03 (2003) รุ่นต่อไปคาดว่าจะเสร็จปี 2008-2009 แต่ไม่แน่ใจ จึงตั้งชื่อชั่วคราวว่า C++0x (x = 8 หรือ 9) ผลลัพธ์: เสร็จในปี 2011 จึงเปลี่ยนชื่อเป็น C++11 อย่างเป็นทางการ สรุป: C++0x = ชื่อรหัส (codename) ก่อนเผยแพร่ | C++11 = ชื่อมาตรฐานจริงหลั

  9. ความแตกต่างระหว่าง -std=c++11 และ -std=gnu++11 ใน GCC

    GNU C++ compiler (g++) มี extensions (ส่วนขยาย) ของภาษา C++ -std=c++11: ปิด GNU extensions ที่ขัดกับมาตรฐาน C++11 (strict standard compliance) -std=gnu++11: เปิด GNU extensions ทั้งหมด (default mode ของ g++) บาง extensions ที่ไม่ขัดกับมาตรฐานอาจยังทำงานใน -std=c++11 ได้ รายการ extensions: GCC C++

  10. C++ Standards Support ใน GCC

    GCC รองรับหลาย dialect ของ C++ ตามมาตรฐาน ISO เลือกด้วย option -std= C++98 / C++03: รองรับเต็มรูปแบบ (1998 + defect reports 2003) C++11: GCC 4.8.1 เป็นเวอร์ชันแรกที่ implement ครบ (ก่อนหน้านี้เรียก C++0x) C++14: รองรับเต็มรูปแบบ (มาตรฐาน 2014) C++17: รองรับ experimental (มาตรฐาน 2017) การใช้งาน $

  11. การเปลี่ยนแปลงสำคัญใน C++11 (The Biggest Changes)

    C++11 เผยแพร่ 2011 นำการปรับปรุงใหญ่ๆ มาหลายอย่าง (รายการเดียวกับ C++11 Overview) Initializer lists Automatic type deduction Rvalue references and move constructors constexpr – Generalized constant expressions Modification to the definition of plain old data Uniform initialization Range-based for

  12. C++11 Overview - ภาพรวมมาตรฐาน C++11

    C++11 เป็นมาตรฐาน C++ สมัยใหม่เผยแพร่ปี 2011 (ISO approved 12 Aug 2011) แทนที่ C++03 เรียกว่า C++0x ก่อนเผยแพร่ (x = 8 หรือ 9 คาดว่าจะเสร็จปี 2008-2009) การเปลี่ยนแปลง/เพิ่มเติมหลักใน C++11 Initializer lists Automatic type deduction (auto) Rvalue references & move constructors constexpr - gene

  13. POD Types ใน C++ คืออะไร

    POD (Plain Old Data) = class/struct ที่มี เฉพาะ member variables ไม่มี methods, constructors, destructors, virtual functions, base classes, private/protected non-static members ตัวอย่าง #include <iostream> using namespace std; struct MyStruct { // POD int key; string data; }; int m

  14. Lambda Expression ใน C++11 คืออะไร

    C++ STL มี generic algorithms อย่าง std::for_each แต่การสร้าง functor แบบเดิม煩瑣 (verbose) C++11 เพิ่ม lambda expressions (anonymous functions) เพื่อแก้ปัญหานี้ ตัวอย่าง #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int&

  15. Aggregates และ PODs ใน C++ คืออะไร

    POD (Plain Old Data) = class/struct ที่มี เฉพาะ member variables ไม่มี methods, constructors, destructors, virtual functions, base classes, private/protected non-static members ตัวอย่าง POD #include <iostream> using namespace std; struct MyStruct { // POD int key; string data; //

  16. ขนาดของ int และ long ในมาตรฐาน C++

    มาตรฐาน C++ ไม่กำหนดขนาดเป็นไบต์ ของ integral types กำหนดแค่ minimum range ที่ต้องรองรับ ขนาดในบิตคำนวณจาก minimum range ได้ ขนาดที่พบบ่อยในปฏิบัติ (de facto standards) 32-bit systems (ILP32): int, long, pointer = 32-bit (4 bytes) 64-bit Unix/Linux (LP64): long, pointer = 64-bit (8 bytes), int = 3

  17. ทำไมการเข้าถึง array out of bounds ใน C++ ไม่ให้ error

    เพราะ C++ ไม่ทำ bounds checking (ตรวจสอบขอบเขต array) ภาษาอื่นอย่าง Java, Python ทำ bounds checking โยน exception/error แนวคิดการออกแบบ C++: ไม่ควรช้ากว่า C และ C ไม่ทำ array bounds checking หากเข้าถึง out-of-bounds: Undefined Behavior (UB) ตามมาตรฐาน C++ อะไรก็ได้: crash, freeze, ทำงานปกติ (เหมือน

  18. วิธีส่งออบเจ็กต์ไปยังฟังก์ชันใน C++ (Pass Objects to Functions)

    มี 5 วิธีส่งออบเจ็กต์ไปฟังก์ชัน (สมมติคลาส X, ฟังก์ชัน fun): 1. Pass by Value สร้าง shallow copy ใน scope ฟังก์ชัน การแก้ไขไม่ส่งผลต่อ object ต้นฉบับ void fun(X x); // Declaration X x; fun(x); // Calling 2. Pass by Reference ส่ง reference ไป ฟังก์ชันแก้ไข object ต้นฉบับได้ ไม่สร้าง copy void

  19. วิธีตรวจจับ Integer Overflow ใน C++

    วิธีที่ปลอดภัยที่สุดคือตรวจสอบ overflow ก่อนที่จะเกิดขึ้น มีวิธีการแบบ hacky สำหรับตรวจจับ integer overflow บ้าง ดังนั้น หากคุณต้องการตรวจจับ overflow ในการบวก unsigned int สามารถตรวจสอบว่าผลลัพธ์น้อยกว่าค่าใดค่าหนึ่งที่บวกเข้ามาได้ ตัวอย่างเช่นunsigned int x, y; unsigned int value = x + y; bool o

  20. Comma Operator ใน C++ ทำงานอย่างไร

    Comma operator (,) ใช้รวมหลาย expression เป็น expression เดียว ค่าของทั้ง expression คือค่าของ expression ตัวสุดท้าย (ขวาสุด) ค่าของ expression อื่นๆ ถูกทิ้ง (discarded) ตัวอย่าง #include <iostream> using namespace std; int main() { int i, j; j = 10; i = (j++, j + 100, 999 + j);

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