Home >Backend Development >C++ >How to use std:: in c++
std is the namespace in C that contains standard library components. In order to use std, use the "using namespace std;" statement. Using symbols directly from the std namespace can simplify your code, but is recommended only when needed to avoid namespace pollution.
std usage in C
In C, std
is a name Space, which contains all standard functions, classes and objects of the C standard library. All standard library components are accessible by using the std
namespace.
How to use std
In order to use the std
namespace, you need to use the using namespace std;
statement in the code. This statement instructs the compiler to automatically search for symbols in the std
namespace when parsing code.
<code class="cpp">using namespace std;</code>
After using the using namespace std;
statement, you can directly use the symbols in the std
namespace without using std::
prefix. For example:
<code class="cpp">// 使用 std::cout 输出"Hello, World!" cout << "Hello, World!" << endl;</code>
Common components in the std namespace
The following are some commonly used components in the std
namespace:
cin
, cout
, endl
vector
, map
, set
find
, sort
, reverse
,
bad_alloc,
out_of_range
Although using the
using namespace std; statement can simplify the code, it may cause namespace pollution. Therefore, it is recommended to use the using namespace std;
statement only when needed. For example, you can use using namespace std;
within a specific function or block of code, rather than throughout your entire program.
The above is the detailed content of How to use std:: in c++. For more information, please follow other related articles on the PHP Chinese website!