Using Keyword in C++ STL - GeeksforGeeks (2024)

Last Updated : 22 Jul, 2024

Summarize

Comments

Improve

The using keyword in C++ is a tool that allows developers to specify the use of a particular namespace. This is especially useful when working with large codebases or libraries where there may be many different namespaces in use. The using keyword can be used to specify the use of a single namespace, or multiple namespaces can be listed using the using keyword.

When using the using keyword, it is important to keep in mind that the specified namespace will take precedence over any other namespace that is in scope. This can lead to unexpected results if the code is not well-organized. For this reason, it is generally considered good practice to use the using keyword sparingly and only when absolutely necessary.

In addition to the using keyword, C++ also provides the using namespace directive. This directive can be used to specify the use of all namespaces in a particular library or codebase. The using namespace directive should be used with caution, as it can make code difficult to read and maintain. This can be especially useful when working with large codebases or when you want to make sure that your code will run in a specific environment.

Use of “using” keyword in C++ STL

The using keyword can be used in the following ways:

  1. Using for namespaces
  2. Using for inheritance
  3. Using for aliasing
  4. Using for directives

1. “using” for namespaces

Using allows you to specify that you want to use a particular namespace. This is useful if you want to avoid typing out the full namespace name every time you want to use something from that namespace.

Example:

C++
#include <iostream>using namespace std;int main() { cout << "Hello, world!"; return 0;}

2. “using” for inheritance

Using allows you to specify that a class inherits from another class. This is useful if you want to avoid having to type out the full name of the base class every time you want to use it.

Example:

C++
#include <iostream>using namespace std;class Base {public: int x; // parameterized constructor Base(int a) : x(a){};};class Derived : public Base {public: int y; using Base::Base;};int main(){ Derived d(42); d.y = 12; cout << d.x << " " << d.y << '\n'; return 0;}

Output

42 12

Note: The C++ compiler does not create any default and copy constructor once we explicitly define any constructor. So we need to declare them explicitly to avoid bugs.

3. “using” for aliasing:

Using allows you to specify an alternate name for a type. This can be useful if you want to avoid having to type out the full name of a type every time you want to use it.

Example:

C++
#include <iostream>using namespace std;using ll = long long; // Here we alias "ll" to stand for long long and save typing it outint main() {  ll a = 5; cout << a; return 0;}

Output

5

4. “using for directives:

Using can be used as a directive to the compiler to tell it to include a particular header file. This is useful if you want to make sure that a particular header file is always included when you compile your code.

C++
#include <iostream>using std::cout;using std::endl;int main() { cout << "Hello, world!" << endl; return 0;}

Output

Hello, world!

Forms of “using” Keyword

The “using” keyword has two different forms:

  1. using namespace std; – This form will bring everything in the std namespace into your code.
  2. using std::cout; or using std:: cin; – This form will only bring the cout object into your code.
  3. using std:: endl;– This form is used to make only specific names in a namespace available in the current namespace.
  4. You can also use the “using” keyword with your own namespaces.

Note: if you have a namespace called “mynamespace”, you could use it like this:

using namespace mynamespace;

Or, if you only wanted to bring in the “myclass” object from that namespace, you could use this form:

using mynamespace::myclass;

Limitation of “using” Keyword:

The “using” keyword can be very useful, but it can also lead to problems if you’re not careful.

  • you need to be careful about using it with large codebases. The reason for this is that it can sometimes lead to name collisions.
  • If you have multiple namespaces in a file, you have to fully qualify the names of any type in a namespace other than the one you are currently in.
  • You cannot use the using keyword to make types in the global namespace visible.
  • You cannot use the using keyword to make types in the System namespace visible.
  • You cannot use the using keyword to make types in a parent namespace visible.
  • You cannot use the using keyword to make static classes visible.

Note:

Imagine you have a codebase that includes both the “std” and “mynamespace” namespaces. If you use the “using” keyword to bring everything in both namespaces into your code, you might end up with two objects with the same name. This can cause compile-time errors or, worse, unexpected behavior at runtime.

To avoid these problems, it’s best to be explicit about which namespaces you’re using.

Example: if you only want to use the “std” namespace, you should write your code like this: using namespace std;

And if you only want to use the “mynamespace” namespace, you should write your code like this: using namespace mynamespace; This may seem like a lot of extra work, but it’s worth it to avoid problems down the road. The “using” keyword is a powerful tool that allows you to specify the use of a particular entity in your program. In particular, it is often used to specify the use of a namespace, class, or function.

Example 1:

C++
// C++ Program to implement// "using" keyword#include <iostream>using namespace std;int main(){ cout << "Hello, world!" << endl; return 0;}

Output

Hello, world!

In this code, the “using” keyword is used to specify the use of the “cout” object from the “std” namespace. Without the “using” keyword, the code would not compile.

The “using” keyword can also be used to specify the use of a particular function or class.

Example 2:

C++
// C++ Program to implement// "using" keyword#include <iostream>#include <string>using std::cout;using std::endl;using std::string;int main(){ string s = "Hello, world!"; cout << s << endl; return 0;}

Output

Hello, world!

In this code, the “using” keyword is used to specify the use of the “string” and “cout” objects from the “std” namespace. Without the “using” keyword, the code would not compile.



O

ojasvigupta

Using Keyword in C++ STL - GeeksforGeeks (1)

Improve

Previous Article

Template Specialization in C++

Next Article

C++ Standard Template Library (STL)

Please Login to comment...

Using Keyword in C++ STL - GeeksforGeeks (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Dong Thiel

Last Updated:

Views: 6673

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.