Skip to main content

C++ wiki

Avoid using namespace std;

It is common to encounter using namespace std; in various C++ sample codes, as it negates the need to prefix std:: before each standard library object. However, it is not recommended to use it in your code. The reason is that using namespace std; might cause name conflicts.

Consider a scenario where you have a function or variable named max. If you declare using namespace std;, then you will get an error when you try to use max because it conflicts with std::max.

#include <iostream>
using namespace std;

int max = 0;
int main() {
cout << max << endl; // error: reference to 'max' is ambiguous
}

To avoid the preceding error, use the following code instead:

#include <iostream>

int max = 0;
int main() {
std::cout << max << std::endl; // 0
}

std::endl vs \n for output

std::endl inserts a newline character (\n) into the output stream and flushes the buffer.

What does std::endl do?

The following code examples show the difference between using std::endl and not using it.

#include <iostream>

int main() {
for (int i = 0; i < 5; i++) {
std::cout << i << std::endl;
}
}
// 0
// 1
// 2
// 3
// 4

Standard output vs file output

In standard output, \n usually has the same effect as std::endl, that is, it inserts a newline character and flushes the buffer. However, in file output, \n does not flush the stream, while std::endl does.

quote

In many implementations, standard output is line-buffered, and writing \n causes a flush anyway, unless std::ios::sync_with_stdio(false) was executed. In those situations, unnecessary endl only degrades the performance of file output, not standard output.

—— C++ reference: std:endl

The following code examples show the same output with std::endl and \n in standard output.

#include <unistd.h>

#include <iostream>

int main() {
for (int i = 0; i < 5; i++) {
sleep(1);
std::cout << i << std::endl;
}
}
// Sleep 1 second
// 0
// Sleep 1 second
// 1
// Sleep 1 second
// 2
// Sleep 1 second
// 3
// Sleep 1 second
// 4

To see the difference between std::endl and \n in file output, run the following code examples.

g++ std_endl.cpp -o std_endl
./std_endl | cat
# Sleep 1 second
# 0
# Sleep 1 second
# 1
# Sleep 1 second
# 2
# Sleep 1 second
# 3
# Sleep 1 second
# 4

Performance

The following example shows the duration for printing 100,000 numbers with std::endl and \n in standard output.

#include <chrono>    // for timers
#include <iostream> // for cin, cout

int endl_each_time(int n = 10000) {
const auto start_time = std::chrono::steady_clock::now();
for (int i = 0; i < n; i++) {
std::cout << i << std::endl;
}
const auto end_time = std::chrono::steady_clock::now();
auto duration_ns = std::chrono::duration_cast<std::chrono::microseconds>(
end_time - start_time);
return duration_ns.count();
}

int new_line_each_time(int n = 10000) {
const auto start_time = std::chrono::steady_clock::now();
for (int i = 0; i < n; i++) {
std::cout << i << "\n";
}
const auto end_time = std::chrono::steady_clock::now();
const auto duration_ns =
std::chrono::duration_cast<std::chrono::microseconds>(end_time -
start_time);
return duration_ns.count();
}

int main() {
int endl_duration = endl_each_time();
int new_line_duration = new_line_each_time();
std::cout << "endl_each_time duration: " << endl_duration << "ns"
<< std::endl;
std::cout << "new_line_each_time duration: " << new_line_duration << "ns"
<< std::endl;
return 0;
}