Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. Generalized Lambda Expressions ใน C++14: Lambda แบบ Generic

    C++11 นำเสนอ Lambda Expressions แต่ C++14 ปรับปรุงให้รองรับ Generalized (Generic) Lambdas ที่สามารถรับพารามิเตอร์แบบ auto ได้ ทำให้ Lambda เดียวสามารถทำงานกับหลายชนิดข้อมูล (Polymorphic) Syntax [](auto x, auto y) { return x + y; } คีย์เวิร์ด auto ในพารามิเตอร์ทำให้ Compiler สร้าง Template แบบ Impli

  2. Self-Destructing Code ใน C: ลบ Executable ตัวเองหลังรัน

    Self-Destructing Code คือโปรแกรมที่หลังจากทำงานเสร็จจะลบไฟล์ Executable ของตัวเองออกจากดิสก์ ทำให้ไม่เหลือร่องรอยของโปรแกรมหลังรัน หลักการทำงาน รับชื่อไฟล์ Executable จาก argv[0] (Command Line Argument ตัวแรก) ใช้ฟังก์ชัน remove() จาก stdio.h ลบไฟล์ โค้ดที่เหลือยังทำงานต่อได้ เพราะโค้ดถูกโหลดลง Pr

  3. การใช้ POSIX Semaphores ในภาษา C สำหรับ Synchronization

    Semaphore เป็นกลไกสำหรับ Process/Thread Synchronization ใช้ควบคุมการเข้าถึง Shared Resource (Critical Section) เพื่อป้องกัน Race Condition ใน Linux ใช้ POSIX Semaphore Library (semaphore.h) การคอมไพล์ gcc program_name.c -lpthread -lrt -lpthread: Link pthread library -lrt: Link real-time library (ส

  4. วิธีสร้าง Header File ของตัวเองในภาษา C

    Header File (ไฟล์ส่วนหัว) ในภาษา C ช่วยให้เราสามารถนำโค้ดที่ใช้ซ้ำไปใช้ในหลายไฟล์ได้ โดยแยก Declaration (การประกาศ) ออกจาก Implementation (การนำไปใช้งาน) ทำให้โค้ดมีโครงสร้างดีขึ้น และง่ายต่อการบำรุงรักษา ขั้นตอนการสร้าง Header File สร้างไฟล์ใหม่ด้วยนามสกุล .h (เช่น my_header.h) วาง Function Proto

  5. การกลับสตริงใน C/C++ ด้วยโมเดล Client-Server

    บทความนี้สาธิตการสร้างระบบ Client-Server ที่ Client ส่งสตริงไปยัง Server และ Server จะกลับสตริงนั้นในลำดับย้อนกลับ (Reverse) แล้วส่งคืนให้ Client โดยใช้ Socket Programming ใน Linux แนวคิดพื้นฐาน ใช้ Socket Programming สำหรับการเชื่อมต่อระหว่าง Client และ Server ระบุ Port Number เหมือนกันทั้งสองฝั่

  6. Stringstream ใน C++: การใช้งานและตัวอย่างโค้ด

    Stringstream ใน C++ เป็นคลาสที่เชื่อมโยงออบเจ็กต์สตริงกับสตรีม ทำให้สามารถอ่านจากสตริงได้เหมือนกับการอ่านจาก cin ซึ่งเป็นเครื่องมือที่มีประโยชน์สำหรับการแยกคำ การแปลงชนิดข้อมูล และการจัดการข้อความแบบไดนามิก เมธอดหลักของ Stringstream clear(): ล้างสตรีมและรีเซ็ตสถานะข้อผิดพลาด str(): ดึงหรือตั้งค่าอ

  7. ซ่อนสตริงใน Binary Code ด้วย C++

    แปลงสตริงเป็น hexadecimal representation เพื่อ ซ่อน ข้อความใน binary (obfuscation เบื้องต้น) วิธีทำ อ่านแต่ละ character ของ string ดึงค่า ASCII (integer) แปลงเป็น hexadecimal ด้วย stringstream + std::hex โค้ดตัวอย่าง #include <iostream> #include <sstream> #include <string> using

  8. Null-terminated String ใน C/C++ คืออะไร?

    Null-terminated string (C-string) คือ array of characters ที่จบด้วย null character \0 (ASCII 0) ลักษณะสำคัญ ใน C: string คือ char[] หรือ char* ที่จบด้วย \0 ใน C++: std::string จัดการความยาวเอง (ไม่ต้องใช้ null terminator ภายใน) แต่ c_str() คืน null-terminated string Compiler แปลง string literal hel

  9. วิธี Trim std::string ใน C++ ที่ดีที่สุด

    Trim whitespace (space, tab, newline) ด้านซ้าย/ขวา/ทั้งสองข้างของ std::string ใช้ Boost.StringAlgo (แนะนำ) #include <boost/algorithm/string.hpp> using namespace std; string s = Hello World ; boost::trim_left(s); // Hello World boost::trim_right(s); // Hello World boost::trim(s);

  10. วิธีที่ดีที่สุดเปรียบเทียบ Float และ Double ใน C/C++

    เนื้อหาคล้าย ID 1004082269 แต่ครอบคลุม double ด้วย ใช้ epsilon comparison ฟังก์ชันสำหรับ Float bool compare_float(float x, float y, float epsilon = 0.01f) { return fabs(x - y) < epsilon; } ฟังก์ชันสำหรับ Double bool compare_double(double x, double y, double epsilon = 1e-7) { return fabs(x -

  11. ความแตกต่าง const int*, const int* const, int const*

    ใช้ Clockwise/Spiral Rule อ่านจากตัวแปรไปทางขวา (clockwise) เพื่อเข้าใจความหมาย 1. const int* (หรือ int const*) Pointer to const int - ชี้ไปที่ int ที่ไม่สามารถแก้ไขได้ ผ่าน pointer นี้ const int* p; // หรือ int const* p int x = 10; p = &x; // OK: เปลี่ยน address ได้ *p = 20; // Error: ไม่ส

  12. ผลของ extern "C" ใน C++

    extern C ระบุ linkage แบบ C ป้องกัน name mangling ของ C++ ชื่อฟังก์ชันจะคงเดิมใน object file ปัญหา Name Mangling C++ รองรับ overloading → compiler เข้ารหัสชื่อฟังก์ชัน (mangle) รวม parameter types เช่น func(int) → _Z4funci ต่างคอมไพเลอร์ต่างเข้ารหัส การใช้ extern C // เรียก C library จาก C++ extern

  13. Copy Initialization vs Direct Initialization ใน C++

    สองรูปแบบการ initialize object: Copy Initialization Type obj = value; // ใช้ copy/move constructor Type obj = other_obj; // copy initialization สร้าง temporary แล้ว copy/move (อาจ elided) ต้องมี accessible copy/move constructor explicit constructor ไม่สามารถใช้ได้ Direct Initialization Type obj(va

  14. เมื่อไหร่ควรใช้ inline Keyword ใน C++

    inline บอกคอมไพเลอร์ให้แทรกโค้ดฟังก์ชันที่จุดเรียก (inlining) แทนการ call ปกติ เมื่อควรใช้ ฟังก์ชันเล็ก เรียกบ่อย (getters, setters, math helpers) ฟังก์ชันใน header file (หลีกเลี่ยง multiple definition errors) Template functions (implicit inline) Member functions ที่ define ใน class body (implicit

  15. แปลง Endianness (Big/Little Endian) ใน C++

    Endianness: ลำดับเก็บ byte ในหน่วยความจำ Big-endian: MSB (most significant byte) อยู่ address ต่ำสุด Little-endian: LSB อยู่ address ต่ำสุด (x86, ARM ทั่วไป) ตัวอย่าง 32-bit value 0x98765432 (สมมติ 4 bytes): Big-endian: 98 76 54 32 Little-endian: 32 54 76 98 ฟังก์ชันสลับ Byte Order (32-bit) 8)

  16. nullptr ใน C++ คืออะไร?

    nullptr (C++11) คือ pointer literal แทน null pointer แทนที่ NULL (มัก define เป็น 0 หรือ (void*)0) ปัญหาของ NULL void func(int); void func(char*); func(NULL); // Ambiguous! NULL อาจเป็น int 0 หรือ pointer แก้ด้วย nullptr func(nullptr); // ชัดเจน: เรียก func(char*) คุณสมบัติ ชนิด std::nullptr_t แปลง

  17. การเปรียบเทียบ Floating Point ใน C++

    เปรียบเทียบ float/double ต้องใช้ epsilon (ค่าความคลาดเคลื่อนที่ยอมรับได้) ไม่ใช่ == โดยตรง ฟังก์ชันเปรียบเทียบ bool compare_float(float x, float y, float epsilon = 0.01f) { return fabs(x - y) < epsilon; } ตัวอย่าง float x = 22.0f/7.0f; // ~3.142857 float y = 3.1415f; compare_float(x, y);

  18. การเพิ่ม Library ใน Visual Studio 2012

    สองวิธีเพิ่ม library ใน Visual Studio 2012: 1. วิธี Manual (GUI) Add #include library.h ในโค้ด Include Directory: Project → Properties → Configuration Properties → VC++ Directories → Include Directories → Add path Library Directory: 同上 → Library Directories → Add path ของ .lib Linker Input: Configu

  19. ใช้ G++ คอมไพล์หลายไฟล์ .cpp และ .h

    คอมไพล์หลายไฟล์พร้อมกันด้วย g++ รวม .h และ .cpp ได้ (แต่ .h ปกติไม่ compile ตรง) คำสั่ง g++ area.h find_area.cpp -o find_area # หรือ g++ area.cpp find_area.cpp -o find_area ตัวอย่าง // area.h float area(float r); float area(float l, float w); // area.cpp float area(float r) { return 3.1415 * r * r;

  20. การ Terminate Thread ใน C++11

    C++11 ไม่มีฟังก์ชัน terminate thread โดยตรง (unsafe) ใช้ pattern ร่วมกับ std::promise/std::future เพื่อส่งสัญญาณให้ thread หยุดเอง กลไก สร้าง std::promise<void> ใน main thread ดึง std::future<void> ให้ worker thread Worker thread ตรวจ future.wait_for() เป็นระยะ Main thread เรียก promise

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:60/301  20-คอมพิวเตอร์/Page Goto:1 54 55 56 57 58 59 60 61 62 63 64 65 66