DSA Tips & Tricks

1️⃣ using namespace std

In C++, writing std:: before standard library functions, objects, or types (like std::cout, std::string, etc.) can become repetitive. To avoid this, you can use the using namespace std; directive at the beginning of your program. This allows you to omit the std:: prefix for standard library elements.

Example without using namespace std:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Example with using namespace std:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

When to Use using namespace std;

  • Small Programs or Learning: For small projects, personal practice, or while learning, it's convenient to use using namespace std; to keep the code cleaner and more readable.
  • Quick Prototyping: When you're quickly prototyping something and don't want to clutter your code with std::.

When to Avoid using namespace std;

  • Larger Projects: In larger or professional projects, it's generally better to avoid using namespace std; because it can lead to naming conflicts, especially if you're using multiple libraries or writing a library that others will use.
  • Header Files: Avoid using it in header files (.h or .hpp) because it can unintentionally affect all files that include that header, leading to conflicts.

Alternative: Using Specific Elements

If you want to avoid conflicts but still reduce repetition, you can selectively use specific elements from the std namespace.

#include <iostream>
using std::cout;
using std::endl;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

2️⃣ Include all libraries in C++

#include <bits/stdc++.h> is a non-standard header file that is commonly used in competitive programming. It includes almost all the standard C++ headers in one go, making it convenient for quick prototyping or solving problems where you need to use a wide variety of standard library features without worrying about which specific headers to include.

Pros of Using #include <bits/stdc++.h>:

  1. Convenience: It saves time during coding by including all the standard library headers at once.
  2. Competitive Programming: In competitive programming, where speed of coding is crucial, it allows you to quickly use any standard library feature without worrying about which header file it belongs to.

Cons of Using #include <bits/stdc++.h>:

  1. Non-Standard: bits/stdc++.h is a GCC-specific extension and is not part of the standard C++ library. It won't work on compilers other than GCC (like MSVC or Clang on some platforms).
  2. Slow Compilation: Including all the headers can significantly slow down compilation time because the compiler has to process a lot of unnecessary headers.
  3. Increased Binary Size: Including unnecessary libraries can lead to larger binary sizes.
  4. Poor Practice for Production Code: It's not suitable for production code, where you should only include the specific headers you need to keep your code clean, portable, and efficient.

Example:

This is how it is used in competitive programming context:

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {4, 3, 2, 1};
    sort(v.begin(), v.end());
    
    for(int i : v) {
        cout << i << " ";
    }
    cout << endl;
    
    return 0;
}