Gadget On - Computer Tips

This site stores technical tips/guides for Windows and Linux machines.

Windows 10 Tech Tips

C++ Tips

  1. Avoid Including Multiple Libraries:Instead of including individual libraries (e.g., <iostream>, <vector>, <set>) one by one, you can use #include <bits/stdc++.h> to include all standard libraries at once. This is especially helpful in programming competitions where time is limited.
  2. Globally Define Statements:Define commonly used statements globally using #define. For example, if you frequently print “GFG!” in your code, you can define it as follows:
  3. Use autofor Type Inference: When declaring variables, use auto to let the compiler infer the data type. For example:
  4. Prefer Range-Based forLoops: Use range-based for loops to iterate over containers (e.g., vectors, arrays, maps). They simplify code and prevent off-by-one errors:
  5. Smart Pointers for Memory Management:Use smart pointers (std::unique_ptr, std::shared_ptr, std::weak_ptr) to manage memory automatically and avoid memory leaks.
  6. Understand Move Semantics:Learn about move semantics and use move constructors and move assignment operators to optimize performance when transferring ownership of objects.
  7. Avoid Global Variables:Minimize the use of global variables. Prefer local scope and encapsulation.
  8. Use constCorrectly: Mark variables and function parameters as const whenever possible to prevent accidental modifications.
  9. Know the Standard Library Algorithms:Familiarize yourself with standard algorithms (std::sort, std::find, etc.) provided by the C++ Standard Library.
  10. Learn About RAII (Resource Acquisition Is Initialization):Understand the concept of RAII and use it to manage resources (e.g., file handles, network connections) safely.
  11. Use emplace_backInstead of push_back: When adding elements to a std::vector, prefer emplace_back over push_back. emplace_back constructs the element in-place, avoiding unnecessary copies or moves:
  12. Avoid Raw Pointers When Possible:Use smart pointers (std::unique_ptr, std::shared_ptr, etc.) instead of raw pointers. Smart pointers manage memory automatically and provide better safety.
  13. Learn About the Rule of Three (or Five):Understand the Rule of Three (for user-defined copy constructors, copy assignment operators, and destructors) and the Rule of Five (which includes move semantics). These rules guide proper resource management.
  14. Use std::stringfor String Manipulation: Prefer std::string over C-style strings (char*). It provides safer and more convenient string manipulation methods.
  15. Know the Difference Between constand constexpr: const indicates immutability, while constexpr means a value can be computed at compile time. Use them appropriately based on your requirements.
  16. Avoid Global Variables and Singletons:Global variables and singletons can lead to unexpected side effects and make code harder to reason about. Minimize their use.
  17. Understand the Big O Notation:Learn about algorithmic complexity (Big O notation) to choose the most efficient data structures and algorithms for your tasks.
  18. Use std::make_uniqueand std::make_shared: When creating smart pointers, prefer std::make_unique and std::make_shared over direct constructor calls. They ensure exception safety.
  19. Learn About Lambda Expressions:Lambda expressions allow you to create anonymous functions. They’re useful for custom sorting, filtering, and other operations:
  20. Explore the Standard Library Containers:Familiarize yourself with other containers like std::map, std::set, and std::queue. Each has unique features and use cases.

Linux Tech Tips