Why I Prefer '' to std::endl 译文

本文探讨了在C++编程中使用' '与std::endl的不同之处,特别是关于输出缓冲区的处理方式。通过一个示例程序说明,使用' '可以减少不必要的缓冲区重置,从而提高程序效率。

原文找自:新浪微博

 

 

译文:

为什么我喜欢用'\n'代替std::endl

两者都能达到同样的目的,获得一个新行。但是有一点不同,endl会引起输出缓冲区的重置,而同一地方用'\n'代替则不会重置。

所以你用下面的代码,向文件中输入大写英文字母表,

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream MyFileStream("Alphabets.txt",ios::out);
for(char Index = 'A';Index <= 'Z';++Index)
{
MyFileStream <<Index <<endl;
}
return 0;
}



输出缓冲区将会被重置26次,然而同样的代码用'\n'代替endl,当程序结束时才会被重置1次。(假设输出缓冲区大于26字节)

 

注:译文如有不合适请留言,互相学习共同进步

### C++ `std::throw` Usage and Common Issues In C++, the keyword `throw` is used to explicitly throw an exception, which can be caught by a matching catch block. The syntax for throwing exceptions involves using this keyword followed by an expression that represents the error condition. The standard library provides several types of objects that are commonly thrown as exceptions; these include classes derived from `std::exception`. For instance: ```cpp #include <stdexcept> void checkValue(int value) { if (value < 0) { throw std::invalid_argument("Invalid argument: Negative values not allowed."); } } ``` When handling exceptions with `try-catch`, it's important to ensure proper type matching between what was thrown and what will catch it. A base class reference or pointer may also capture derived-class exceptions due to polymorphism features in C++. However, when catching exceptions, always prefer const references unless there’s a specific reason otherwise because copying large objects could lead to performance overheads[^1]. Common pitfalls associated with throwing exceptions involve resource management during abrupt function exits caused by unhandled errors propagating up call stacks until they reach appropriate handlers—if any exist at all. To mitigate potential leaks, RAII (Resource Acquisition Is Initialization) principles should guide how resources like memory allocations or file handles get managed within scope-based constructs such as smart pointers (`unique_ptr`, `shared_ptr`) or containers. Another issue arises from attempting to rethrow current active exceptions without specifying them inside another handler context via simply writing `throw;`. This allows forwarding ongoing exceptional states while preserving original stack traces useful for debugging purposes but requires careful attention so as not accidentally terminate programs prematurely through misuse outside valid scopes where only single bareword throws apply correctly according to language rules defined around exception propagation mechanisms implemented internally across different compiler implementations supporting modern standards including C++17 onwards. --related questions-- 1. How does one implement custom exception classes inheriting from std::exception? 2. What best practices should developers follow regarding exception safety guarantees? 3. Can you provide examples demonstrating effective use of try-catch blocks alongside RAII techniques? 4. In what scenarios might noexcept specifiers prove beneficial concerning function declarations involving possible throws?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值