ความแตกต่างหลัก
\n: พิมพ์ newline (platform-specific: Windows เป็น \r\n) ไม่ flush streamstd::endl: พิมพ์ newline และ flush stream
แนวทางการใช้งาน
ปกติไม่ต้อง flush stream ทันที เพราะส่งผลต่อ performance ควรใช้ \n แทน
ใช้ std::endl เมื่อต้องการ flush ด้วยตนเอง เช่น ต้องการให้ output แสดงให้ผู้ใช้เห็นทันที (logging, progress, interactive prompts)
สรุป
| คุณสมบัติ | \n | std::endl |
|---|---|---|
| Newline | ✓ | ✓ |
| Flush stream | ✗ | ✓ |
| Performance | ดีกว่า | ช้ากว่า |
| ใช้เมื่อไหร่ | ทั่วไป | ต้องการ flush ทันที |