C++

What Is Std in C++ and How to Use It by Example

The Standard Library is part of the C++ standard which is a standardized specification of the C++ programming language. It is maintained and updated by the International Organization for Standardization (ISO).

In C++, β€œstd” is a namespace that contains many of the Standard Library components that are provided by the C++ Standard Library. It is a way to organize and group together the various functions, classes, and objects that are defined in the Standard Library.

The β€œstd” namespace is used to avoid the naming conflicts and to provide a clear distinction between the Standard Library entities and user-defined entities. Placing the Standard Library components in the β€œstd” namespace ensures that they can be accessed through the qualified β€œstd::” name which prevents clashes with other identifiers in the global namespace.

You must either explicitly define the namespace that utilizes the β€œstd::” prefix or add the particular names from the β€œstd” namespace to the current scope that employs the β€œusing” directive in order to use the components from the β€œstd” namespace.

Let’s explore the usage of β€œstd” with some examples.

Method 1: Utilizing the Scope Resolution (::) Operator with Std

To access the names inside the namespaces in C++, use the scope resolution (::) operator which is also referred to as the β€œscope resolution” operator. When used with the β€œstd” namespace, the (::) operator allows you to access the components from the β€œstd” namespace without the need to import the entire namespace or use the β€œusing namespace std;” directive.

Let’s see how we can implement β€œstd::” in a C++ program for input/output operations:

#include <iostream>

int main() {

  std::string country;

  std::cout << "Enter your country: ";

  std::cin >> country;

  std::cout << "Welcome to " << country << "!" << std::endl;

return 0;

}

In this example, we include the <iostream> header to access the input/output operations. Instead of importing the entire β€œstd” namespace or utilizing the β€œusing namespace std;”, we use the (::) operator to access specific components from the β€œstd” namespace.

std::cout: The cout object for standard output.

std::cin: The cin object for standard input.

std::endl: The endl manipulator to insert a new line and flush the output buffer.

Using the (::) operator, we specify the namespace (std) followed by the component name (cout, cin, or endl). This way, we explicitly indicate that these components belong to the β€œstd” namespace.

So, using these components, we perform some input/output tasks. The country is declared as a string variable in the main() function using β€œstr::” with the string. Then, we display the “Enter your country: ” statement. We use the β€œstd::cout”. To take input from the user, β€œstd::” is used with cin.

Since we haven’t used β€œstd” with the global scope, we have to use β€œstd::” every time we want to use a component from the β€œstd” namespace. Despite the fact that we are using the cout component again, we still have to use β€œstd::” with it to display the output.

The generated output from the previous code is provided in the following snapshot:

Method 2: Utilizing the β€œUsing” Declaration with the Std Namespace

The “using” declaration in C++ allows you to bring specific names or the entire namespaces into scope which makes it easier to access them without using the full namespace qualification.

Here’s an example of using the “using” declaration with the β€œstd” namespace:

#include <iostream>

using std::cout;

using std::endl;

int main() {

  int num = 177;

  cout << "The number is: " << num << endl;

return 0;

}

In this example, we include the <iostream> header to access the input/output operations. Then, we use the “using” declaration to include the β€œstd” namespace variables, β€œcout” and β€œendl”, in the scope of the main() function.

By employing the β€œusing std::cout;” and β€œusing std::endl;”, we can directly refer to β€œcout” and β€œendl” without the need to prefix them with β€œstd::”.

The cout << “The number is: ” << num << endl; statement prints the “The number is: 117” message to the console using β€œcout” from the β€œstd” namespace and inserts a newline with β€œendl”.

Using the “using” declaration in this manner allows you to selectively bring specific names from the β€œstd” namespace into scope, reducing the need for repetitive β€œstd::” prefixes. It strikes a balance between convenience and potential naming conflicts as you explicitly import only the names that you need.

The output snapshot is attached in the following:

Method 3: Utilizing the β€œUsing Namespace Std;” Directive

For the first illustration, we utilize the directive which is the β€œusing namespace std”.

The β€œusing namespace std;” directive allows you to avoid the explicit specification of the β€œstd::” prefix for components from the β€œstd” namespace. It includes all of the names from the standard namespace in the current scope which allows you to directly use those names without the β€œstd::” prefix.

#include <iostream>

using namespace std;

int main()

{

  int n;

  cout << "Enter a number: ";

  cin >> n;

  cout << "The number is: " << n << endl;

  return 0;

}

In this illustration, we include the <iostream> header and then import all names from the β€œstd” namespace into the current scope using the β€œusing namespace std;” directive.

After importing the namespace, we can directly use β€œcout” and β€œcin” without the β€œstd::” prefix. The β€œcout” statement prints a message to the console, and the β€œcin” statement reads input from the user. Similarly, we can include a newline using β€œendl” rather than β€œstd::endl” and flush the output buffer.

We create the β€œint” variable β€œn” in the main() method. A statement is put to display using the β€œcout” statement as β€œEnter a number:” and the β€œcin” statement takes in the user input and stores it in the β€œn” variable. Using the β€œcout” statement, the user-provided value is printed. As we used the β€œstd” namespace in global scope, we do not need to use it explicitly inside the program.

The generated output is provided in the following snapshot:

Note that the β€œutilizing using namespace std;” can lead to potential naming conflicts if the names from the β€œstd” namespace clash with the names in your code or with the names from the other namespaces that you are using. It is generally recommended to avoid using the β€œutilizing namespace std;” in global scope but rather use the specific using directives or prefixes to maintain clarity and avoid naming conflicts.

Conclusion

The β€œstd” namespace is a Standard Library that can be utilized with different methods in a C++ program. We covered three ways to use the β€œstd” namespace in this post. The first method discussed the utilization of scope resolution operator (::) with β€œstd”. The second method was about declaring the β€œusing” with β€œstd” in the current scope of the program. Lastly, the utilization of the β€œusing namespace std;” directive into the current scope of the program is elaborated.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.