Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++
C++
  1. การแปลง char เป็น int ใน C และ C++

    ภาษา C (3 วิธี) sscanf() atoi() Typecasting ตัวอย่าง C #include <stdio.h> #include <stdlib.h> int main() { const char *str = 12345; char c = s; int x, y, z; sscanf(str, %d, &x); // Using sscanf printf(\nThe value of x : %d, x); y = atoi(str)

  2. ความแตกต่างระหว่าง printf() และ cout ใน C++

    printf() มาจาก C ใช้ใน C++ ได้ Format Function พิมพ์ไป Standard Output ต้องใช้ Format Specifier (%d, %s, %f ฯลฯ) ส่งคืน int (จำนวนตัวอักษรที่พิมพ์) ไม่ Type-safe (Argument Mismatch ไม่ Error) รูปแบบ printf(string %d, variable); ตัวอย่าง #include <stdio.h> int main() { int a = 24; p

  3. รายการ C++ IDE สำหรับ Linux

    IDE ยอดนิยมสำหรับพัฒนา C++ บน Linux: Eclipse Galileo with CDT Plugin Code Editor + Syntax Highlighting Code Folding + Hyperlink Navigation Refactoring + Code Generation Visual Debugging (Memory, Registers) NetBeans IDE Auto Packaging (.tar, .zip, archives) Multi-compiler (GNU, Clang/LLVM, Cygwi

  4. ตัวดำเนินการ < และ <= ใน C/C++ อันไหนเร็วกว่า?

    ไม่มีความแตกต่างด้านความเร็ว ทั้ง < และ <= ใช้เวลารันเท่ากัน เหตุผล ระดับ Assembly ใช้ Jump Instructions (JCC) ตามประเภทการเปรียบเทียบ: je − Jump if Equal jg − Jump if Greater jne − Jump if Not Equal jge − Jump if Greater or Equal < ใช้ jg ส่วน <= ใช้ jge ทั้งคู่ใช้ Cycle เดียวกันบน

  5. การพิมพ์เนื้อหา Vector ใน C++

    Vector เป็น Dynamic Array ที่ปรับขนาดได้ ใช้ std::vector จาก <vector> ตัวอย่างโค้ด #include <iostream> #include <vector> void print(const std::vector<int>& a) { std::cout << The vector elements are : ; for (size_t i = 0; i < a.size(); i++) std::

  6. การเปลี่ยนแปลงใหม่ใน C++11

    C++11 ได้รับการรับรองจาก ISO วันที่ 12 สิงหาคม 2011 เพิ่มฟีเจอร์มากมายใน Core Language ฟีเจอร์หลักของ C++11 nullptr − Null Pointer Literal แทน 0/NULL ป้องกัน Implicit Conversion Lambdas − Anonymous Functions ใช้ได้ทุกที่ที่คาดหวัง std::function Smart Pointers − shared_ptr, unique_ptr, weak_ptr (แท

  7. ทำไม Global Variables จึงไม่ดีใน C/C++

    Global Variables ประกาศนอกฟังก์ชันใดๆ คงค่าตลอดการรันโปรแกรม เข้าถึงได้จากทุกที่ ปัญหาของ Global Variables Non-const Globals เป็นอันตราย − ฟังก์ชันไหนก็แก้ไขได้ ทำให้ Debug ยาก ลด Modularity และ Flexibility − Coupling สูง ระหว่างฟังก์ชัน Namespace Pollution − ชื่อชนกันง่าย Thread Safety − Race Con

  8. ประเภทของ String Literals ใน C/C++

    String Literals คือชุดตัวอักษรใน Double Quotes () Wide-string Literals นำหน้าด้วย L เสมอ ประเภท String Literals Prefixชื่อคำอธิบาย (none)Narrow StringChar ธรรมดา (char[]) LWide StringWide Char (wchar_t[]) u8UTF-8 StringUTF-8 Encoded (char[]) uUTF-16 StringUTF-16 Encoded (char16_t[]) UUTF-32 String

  9. ความแตกต่างระหว่าง new/delete และ malloc/free ใน C/C++

    new / delete (C++) Operator (ไม่ใช่ฟังก์ชัน) เรียก Constructor/Destructor อัตโนมัติ Type-safe ส่งคืน Pointer ประเภทที่ถูกต้อง โยน Exception (std::bad_alloc) หากจองไม่สำเร็จ รองรับ Overloading และ Placement New delete สำหรับเดี่ยว, delete[] สำหรับอาเรย์ ตัวอย่าง int *ptr1 = new int; float *ptr2 =

  10. การทำงานของ malloc() และ free() ใน C/C++

    malloc() จองหน่วยความจำตามขนาดไบต์ที่ระบุ ส่งคืน Pointer ไปยังไบต์แรก ส่งคืน NULL หากล้มเหลว รูปแบบ pointer_name = (cast_type*) malloc(size_in_bytes); ตัวอย่าง #include <stdio.h> #include <stdlib.h> int main() { int n = 4, i, *p, s = 0; p = (int*) malloc(n * sizeof(int));

  11. ฟีเจอร์ C++11 ใน Visual Studio 2015

    C++11 ได้รับการรับรองจาก ISO วันที่ 12 สิงหาคม 2011 Visual Studio 2015 รองรับฟีเจอร์ C++11 ส่วนใหญ่ ฟีเจอร์หลัก nullptr − Literal สำหรับ Null Pointer แทน NULL/0 ป้องกัน Implicit Conversion Lambdas − Anonymous Functions ใช้ได้ทุกที่ที่คาดหวัง std::function auto − Type Deduction ประกาศตัวแปรไม่ต้องร

  12. วัตถุประสงค์ของ Union ใน C/C++

    Union เป็น User-defined Data Type ที่ สมาชิกทุกตัวใช้หน่วยความจำร่วมกัน ขนาด Union = ขนาดสมาชิกที่ใหญ่ที่สุด เหมาะสำหรับเก็บค่าที่ใช้สลับกัน เพื่อประหยัดหน่วยความจำ รูปแบบ union union_name { member_definition; } union_variables; ตัวอย่างโค้ด #include <stdio.h> #include <string.h>

  13. ตัวดำเนินการ ? (Ternary Operator) ใน C/C++

    ตัวดำเนินการ ? เรียกว่า Ternary Operator หรือ Conditional Operator ต้องการ 3 Operands รูปแบบ Condition ? Expr1 : Expr2 ลดโค้ดและเพิ่มประสิทธิภาพ รูปแบบ Expression1 ? Expression2 : Expression3 ตัวอย่าง 1 #include <stdio.h> int main() { int a = -1; double b = 26.4231; int c = a ?

  14. ความแตกต่างระหว่าง Structure และ Union ใน C

    Structure (โครงสร้าง) User-defined Data Type รวมข้อมูลหลายประเภทไว้ด้วยกัน สมาชิกแต่ละตัวมีหน่วยความจำแยกกัน รูปแบบ struct structure_name { member_definition; } structure_variables; ตัวอย่าง #include <stdio.h> #include <string.h> struct Data { int i; long int f; } data, da

  15. ตัวดำเนินการ Left Shift และ Right Shift ใน C/C++

    Left Shift (<<) เลื่อนบิตของตัวถูกดำเนินการซ้าย ตามจำนวนบิตที่ระบุ ตัวอย่าง #include <stdio.h> int main() { int y = 28; // 11100 ใน binary for (int i = 0; i <= 3; ++i) printf(Left shift by %d: %d\n, i, y << i); return 0; } ผลลัพธ์ Left shift by 0: 28 Left shift

  16. การกำหนดอาเรย์หลายมิติใน C/C++

    อาเรย์หลายมิติ (Multi-dimensional Arrays) คือ อาเรย์ของอาเรย์ ข้อมูลจัดเก็บแบบตาราง Row-major order รูปแบบ type array_name[size1][size2]...[sizeN]; การ Initialization type array_name[size1][size2] = { {elements}, {elements}, ... }; ตัวอย่างโค้ด (2 มิติ) #include <stdio.h> int main() {

  17. การโยน Exception ใน C++

    ใช้คีย์เวิร์ด throw เพื่อโยน Exception ใดๆ ก็ได้ภายในบล็อกโค้ด ตัวอย่างโค้ด #include <iostream> using namespace std; int display(int x, int y) { if (y == 0) { throw Division by zero condition!; } return (x / y); } int main() { int a = 50; int b = 0; int c = 0

  18. การจับ Exception ทั้งหมดใน C++

    Exception คือปัญหาที่เกิดระหว่างรันโปรแกรม ใช้ Try-Catch Block เพื่อจัดการ ใช้ catch(...) เพื่อจับ Exception ทุกประเภท (Catch-all Handler) ตัวอย่างโค้ด #include <iostream> using namespace std; void func(int a) { try { if (a == 0) throw 23.33; if (a == 1) throw s; } c

Total 6007 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:23/301  20-คอมพิวเตอร์/Page Goto:1 17 18 19 20 21 22 23 24 25 26 27 28 29