Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. Bitap Algorithm (Shift-And) สำหรับ String Matching ใน C++

    Bitap Algorithm (หรือ Shift-And) ใช้ bitwise operations ทำ string matching รวดเร็วสำหรับ pattern สั้น (≤ 64 chars) และ exact matching หลักการทำงาน Precompute bitmask สำหรับแต่ละตัวอักษรใน pattern ใช้ bit vector R ติดตามสถานะการ match Shift และ AND ด้วย bitmask สำหรับแต่ละตัวอักษรใน text ถ้าบิตที่

  2. String Search Algorithm สำหรับข้อความสั้นใน C++ (Naive Search)

    Naive String Search (Brute Force) เหมาะสำหรับข้อความสั้นและ pattern สั้น ทำงานโดยเปรียบเทียบตัวอักษรทีละตัว อัลกอริทึม เลื่อน pattern ไปทีละตัวบน text ที่แต่ละตำแหน่ง เปรียบเทียบตัวอักษรทีละตัว ถ้าตรงทุกตัว → พบ match ถ้าไม่ตรง → ข้ามไปตำแหน่งถัดไป Time Complexity: O(n×m) โดย n = text length, m =

  3. Implement Queue ด้วย Two Stacks ใน C++

    ใช้ Stack 2 ตัว (LIFO) จำลอง Queue (FIFO) โดยรักษา invariant ว่า stack หนึ่งเก็บข้อมูลเรียง FIFO วิธีการ: Input/Output Stacks inStack: รับข้อมูลใหม่ (push) outStack: ให้ข้อมูลออก (pop/front) เมื่อ outStack ว่าง → ย้ายทั้งหมดจาก inStack → outStack (กลับลำดับ) Operations OperationAlgorithmAmorti

  4. cout << endl vs cout << "\n" ใน C++: ความแตกต่างด้านประสิทธิภาพ

    หลายคนใช้ std::endl และ \n สลับกันได้ แต่จริงๆ แล้วมีความแตกต่างสำคัญด้านประสิทธิภาพ ความแตกต่างพื้นฐาน คุณสมบัติstd::endl\n ขึ้นบรรทัดใหม่✅✅ Flush output buffer✅ (บังคับทุกครั้ง)❌ (ไม่บังคับ) Performanceช้า (system call ทุกบรรทัด)เร็ว (buffered) TypeManipulator (function)Character literal ท

  5. Implement Stack ด้วย Two Queues ใน C++

    ใช้ Queue 2 ตัว (FIFO) จำลอง Stack (LIFO) โดยรักษา invariant ว่าองค์ประกอบล่าสุดอยู่ที่ front ของ queue หลัก วิธีการ: Active/Standby Queues q1 = active queue (เก็บข้อมูลเรียง LIFO) q2 = standby queue (ช่วยย้ายข้อมูล) Operations OperationAlgorithmTime push(x)enqueue x เข้า q1O(1) pop()ย้ายทั้

  6. Implement Deque (Double-Ended Queue) ใน C++

    Deque (Double-Ended Queue) เป็นคิวที่เพิ่ม/ลบได้ทั้งสองข้าง รวมความสามารถของ Stack และ Queue Operations OperationคำอธิบายComplexity push_front(x)เพิ่มหน้าคิวO(1) push_back(x)เพิ่มหลังคิวO(1) pop_front()ลบหน้าคิวO(1) pop_back()ลบหลังคิวO(1) front()/back()ดูค่าหน้า/หลังO(1) size()/empty()ขนาด/ว่า

  7. ฟังก์ชัน fread() ใน C++: อ่านข้อมูลจากไฟล์แบบ Binary

    fread() เป็นฟังก์ชันมาตรฐาน C สำหรับอ่านข้อมูลแบบ binary จากไฟล์ (stream) ลงใน buffer ใช้กับ FILE* จาก <cstdio> Prototype size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream); พารามิเตอร์ พารามิเตอร์คำอธิบาย ptrPointer ไปยัง buffer ที่จะเก็บข้อมูล (ต้องมีขนาด ≥ size × nmemb b

  8. Implement Queue ใน C++ ด้วย Array และ Linked List

    Queue เป็นโครงสร้างข้อมูลแบบ FIFO (First-In-First-Out) การเข้า (Enqueue) ที่ rear การออก (Dequeue) ที่ front Interface มาตรฐาน OperationคำอธิบายComplexity enqueue(x)เพิ่ม x ที่ rearO(1) dequeue()ลบและคืนค่าที่ frontO(1) front()ดูค่าที่ front (ไม่ลบ)O(1) empty()ตรวจสอบว่าว่างหรือไม่O(1) size()จำ

  9. Warshall's Algorithm สร้าง Transitive Closure ของกราฟใน C++

    Transitive Closure ของ directed graph คือเมทริกซ์ที่บอกว่า vertex j ไปถึงจาก vertex i ได้หรือไม่ (มี path หรือไม่) Warshalls Algorithm คำนวณได้ใน O(n³) หลักการ ใช้ Dynamic Programming: พิจารณาว่า path จาก i ไป j สามารถผ่าน vertex k ได้หรือไม่ T[i][j] = T[i][j] OR (T[i][k] AND T[k][j]) โดย T คือ adj

  10. คำนวณพื้นที่รูปไข่ (Ellipse) ใน C++

    รูปไข่ (Ellipse) มีพื้นที่คำนวณจากสูตรง่าย: A = π × a × b โดยที่ a = กึ่งแกนใหญ่ (semi-major axis), b = กึ่งแกนเล็ก (semi-minor axis) ส่วนประกอบของรูปไข่ ชื่อคำอธิบาย Center (ศูนย์กลาง)จุดกึ่งกลางของรูปไข่ และกึ่งกลางของเส้นเชื่อมจุดโฟกัสสองจุด Major Axis (แกนใหญ่)เส้นผ่านศูนย์กลางที่ยาวที่สุด

  11. Nearest Neighbour Algorithm ใน C++: การประยุกต์กับ TSP

    Nearest Neighbour Algorithm เป็น heuristic แบบ Greedy สำหรับหาเส้นทางประมาณการใน Traveling Salesman Problem (TSP) โดยเลือกเมืองที่ใกล้ที่สุดซ้ำๆ อัลกอริทึม เริ่มจากเมืองหนึ่ง (start city) หาเมืองที่ยังไม่ได้เยี่ยมชมและมีระยะทางสั้นที่สุดจากเมืองปัจจุบัน ย้ายไปเมืองนั้น ทำเครื่องหมายว่าเยี่ยมชมแล้ว

  12. Traveling Salesman Problem (TSP) ด้วย Nearest Neighbour Algorithm ใน C++

    Traveling Salesman Problem (TSP) เป็นปัญหา optimization หาเส้นทางสั้นที่สุดที่ผ่านทุกเมืองครั้งละครั้งและกลับมาที่เริ่มต้น Nearest Neighbour เป็น heuristic แบบ greedy ให้คำตอบประมาณการรวดเร็ว อัลกอริทึม Nearest Neighbour เริ่มจากเมืองใดเมืองหนึ่ง หาเมืองที่ใกล้ที่สุดที่ยังไม่ได้ไป ไปที่เมืองนั้น ท

  13. Pure Virtual Functions และ Abstract Classes ใน C++

    Pure virtual functions และ Abstract classes เป็นหัวใจของ **Interface-based Programming** และ **Polymorphism** ใน C++ Pure Virtual Function class Base { public: virtual void doSomething() = 0; // Pure virtual // ^^^^^^ ไม่มี body, ต้อง override ใน derived }; Abstract Class ค

  14. Virtual Base Class ใน C++: แก้ปัญหา Diamond Inheritance

    Virtual base class ใช้แก้ปัญหา **Diamond Problem** (Diamond Inheritance) ที่ derived class ได้รับ base class มาหลายคอผ่านการสืบทอดหลายเส้นทาง ปัญหา Diamond Inheritance Base / \ D1 D2 \ / Derived โดยไม่มี virtual inheritance: Derived จะมี **สองคอ** ของ Base (หนึ่

  15. ทำไม Pure Virtual Function ใน C++ ถูก Initialize ด้วย 0?

    การกำหนด = 0 หลัง declaration ของ virtual function เป็น **เพียงแค่ syntax** (ไวยากรณ์) เพื่อบอก compiler ว่าฟังก์ชันนี้เป็น **pure virtual function** ไม่มีความหมายทางคณิตศาสตร์หรือการกำหนดค่าใดๆ Syntax class Base { public: virtual void foo() = 0; // Pure virtual function // ^^^^^^^

  16. ทำไมต้องมี Pure Virtual Destructor ใน C++?

    Pure virtual destructor เป็นกลไกสำคัญในการทำให้คลาสกลายเป็น Abstract Class พร้อมทั้งรับประกันการทำลายออบเจกต์อย่างถูกต้อง ทำไมต้องมี Body (Definition)? ใน C++ Destructor ถูกเรียกแบบ **chain** จาก derived ไป base: ~Base() แม้ Base destructor จะเป็น pure virtual (= 0) ก็ยังถูกเรียก ดังนั้น **ต้องมี

  17. Default Virtual Behavior ใน C++ vs Java: ความแตกต่างพื้นฐาน

    ความแตกต่างพื้นฐานระหว่าง C++ และ Java เกี่ยวกับ virtual functions ส่งผลต่อการออกแบบ class hierarchy และประสิทธิภาพ ความแตกต่างหลัก ด้านC++Java DefaultNon-virtual (static binding)Virtual (dynamic binding) Keyword เพื่อ virtualvirtualไม่ต้องใส่ (default) Keyword เพื่อ non-virtualไม่ใส่ virtual /

  18. Variadic Function Templates ใน C++: ฟังก์ชันรับอาร์กิวเมนต์ไม่จำกัด

    Variadic templates (C++11) ช่วยให้เขียนฟังก์ชัน/คลาสที่รับจำนวนอาร์กิวเมนต์ไม่จำกัดได้ แทนที่ ellipsis (...) แบบ C-style ที่ไม่ปลอดภัย Syntax พื้นฐาน template <typename T, typename... Args> return_type function_name(T first, Args... args); typename... Args: parameter pack (แพ็คของชนิด) Args.

  19. Template Metaprogramming ใน C++: การคำนวณที่ Compile-time

    Template Metaprogramming (TMP) คือเทคนิคการเขียนโปรแกรมที่ทำการคำนวณระหว่าง **compile-time** โดยใช้ template mechanism ของ C++ แทนที่จะคำนวณตอน runtime หลักการทำงาน Template instantiation เกิดขึ้นที่ compile-time Compiler ทำ recursive template expansion ผลลัพธ์เป็นค่าคงที่ (constant) ที่พร้อมใช้งา

  20. Above-Below-On Test: ตรวจสอบตำแหน่งจุดเทียบเส้นใน C++

    Above-Below-On Test ใช้กำหนดตำแหน่งของจุดเทียบกับเส้นตรง โดยใช้ค่า sign ของฟังก์ชันเส้น ทฤษฎี เส้นตรงผ่านจุด m(x₁,y₁) และ n(x₂,y₂) มีสมการ: F(x, y) = (y₂ - y₁)x + (x₁ - x₂)y + (x₂y₁ - x₁y₂) = 0 สำหรับจุดทดสอบ t(xₜ, yₜ) คำนวณ s = F(xₜ, yₜ): s < 0 → จุดอยู่ด้าน **clockwise** (below/left) ของเส้น 0

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:44/301  20-คอมพิวเตอร์/Page Goto:1 38 39 40 41 42 43 44 45 46 47 48 49 50