C++ using keyword

C++ using keyword

The using keyword is used to simplify code and improve readability.

The using keyword provides a flexible way to import namespaces and define aliases in C++.

1. Import namespace

using namespaceAll names in a namespace can be imported into the current scope, allowing all names in that namespace to be used directly without having to use the scope resolution operator::

using namespace std;

Note: Use using namespacemay lead to naming conflicts and name redefinition.

2. Define aliases

Define type alias

using myint = int; // 将myint类型定义为int类型的别名
myint x = 42;

Template type aliases can be specified

template<typename T>
using myVector = std::vector<T>;

// 可以使用myVector<int>来代替std::vector<int>类型

You can define aliases for function pointer types

using bar = void(*)();

Define an alias for a template pointer

template<typename T>
using myPointer = T*;

// 可以使用myPointer<int> 来替代int*类型

Guess you like

Origin blog.csdn.net/first_bug/article/details/132434368