01 / 21

Namespaces &
std::

Learn how C++ organizes names into namespaces to avoid conflicts, and why the std:: prefix is the professional way to access the Standard Library.

C++

What Is a Namespace?

A namespace is a named container that groups related identifiers: variables, functions, classes, and types. It prevents name collisions when two different libraries define things with the same name.

Why Do Namespaces Exist?

Library A print() sort() log() Library B print() connect() log() Conflict! Conflict! Same names in different libraries cause ambiguity

The std Namespace

The C++ Standard Library places every one of its identifiers inside a namespace called std (short for "standard"). This includes cout, cin, string, vector, sort, and thousands more.

std::cout
Standard output stream for printing to the console
std::string
A class for safely handling text data
std::vector
A resizable array container for collections

Declaring a Namespace

namespace math_utils {
  int add(int a, int b) {
    return a + b;
  }
  double pi = 3.14159;
}

The :: Scope Resolution Operator

std :: cout Namespace Operator Identifier Fully qualified name: std::cout

The scope resolution operator (::) tells the compiler: "Look inside this namespace for this identifier." It is the precise, unambiguous way to access any name.

using namespace vs std::

std:: prefix
Explicit, safe, professional. You always know exactly which name you are using. Recommended for all production code.
using namespace std;
Imports every name from std into the current scope. Convenient but risky: it can cause name collisions as your code grows.

Three Ways to Access std Names

1
Fully qualified name: Write std::cout every time. Most explicit and safest approach.
2
Using-declaration (selective): Write using std::cout; to import just one name into scope.
3
Using-directive (blanket): Write using namespace std; to import everything. Use sparingly.

How to Apply the std:: Prefix

1
Include the header: Write the correct #include directive (e.g., <iostream>, <string>, <vector>).
2
Prefix every Standard Library name with std:: when you use it in your code.
3
Never put using namespace std; in a header file: it would pollute every file that includes it.
4
If typing gets tedious, use a selective using std::cout; declaration inside a function body, not at file scope.

Example 1: Hello World with std::

#include <iostream>

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

Example 2: Multiple std Types

#include <iostream>
#include <string>
#include <vector>

int main() {
  std::string name = "Alice";
  std::vector<int> scores = {95, 87, 92};

  std::cout << name << " has "
         << scores.size()
         << " scores." << std::endl;
  return 0;
}

Example 3: Selective Using-Declaration

#include <iostream>
#include <string>

int main() {
  using std::cout;
  using std::endl;
  using std::string;

  string greeting = "Hi there!";
  cout << greeting << endl;
  return 0;
}

Creating Custom Namespaces

namespace graphics {
  void draw() { /* draw a shape */ }
}

namespace audio {
  void draw() { /* draw from audio buffer */ }
}

int main() {
  graphics::draw(); // Unambiguous
  audio::draw(); // Also unambiguous
}

How the Compiler Resolves Names

Global Scope namespace std cout string vector sort namespace myapp sort init Config std::sort and myapp::sort: no conflict ✓

Common Mistakes

✕ using namespace in Headers
Placing using namespace std; in a .h file forces it on every file that includes it, causing hidden collisions.
✕ Forgetting the Header
Writing std::string without #include <string>. The code may compile on some compilers but fail on others.
✕ Mixing Styles Randomly
Using std::cout on one line and bare endl on the next. Be consistent: either prefix everything or use declarations.

Comparison of Approaches

std:: Prefix using Declaration using namespace
Safety Highest Moderate Lowest
Verbosity More typing Moderate Least typing
In Headers? ✓ Always safe ✓ If inside function ✕ Never
Collision Risk None Low High
Best For All projects Repeated names Tiny demos only

Question 1 of 5

What does the :: operator do in std::cout?

Question 2 of 5

Why should you avoid writing using namespace std; in a header file?

Question 3 of 5

This code will produce a compiler error. Which fix is correct?

Question 4 of 5

Two libraries each define a function called connect(). How do namespaces solve this?

Question 5 of 5

Which of the following is the safest way to use Standard Library names in a large project?