Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Importing the C++ Standard Library

Importing the C++ Standard Library

In C++ programming, the Standard Library is a very important component.

The Standard Library provides a large number of predefined functions and classes that help us accomplish various tasks more efficiently. To use these features, we need to import the corresponding Standard Library header files in our program.

This article details how to import the Standard Library in C++ and explains some common Standard Library header files and their uses.

What is the C++ Standard Library?

The C++ Standard Library is part of the C++ language; it contains a series of classes and functions for handling common programming tasks such as input/output, string manipulation, mathematical computation, and container management.

The design goal of the Standard Library is to provide efficient, portable, and easy-to-use tools to help developers build applications quickly.


How to import the C++ Standard Library?

1. Using #include to include header files

In C++, we use #include The preprocessor directive is used to import Standard Library header files.

#include The directive tells the compiler to insert the specified header file's content into the current file at compile time; a header file usually contains function declarations, class definitions, macro definitions, and so on.

Basic Syntax

The syntax format for importing the C++ Standard Library is as follows:



#include 

Whereis the name of the Standard Library header file.

Example

Example

#include  // import the input/output stream library

#include    // import the vector container library

#include     // import the math functions library

In the example above, we imported three commonly used Standard Library header files:

2. Importing the Standard Library using Modules (C++20/C++23)

Starting from C++20, C++ introduced Modules, and further improved support for Standard Library modules in C++23.

Modules provide a more efficient and safer way to import the Standard Library.

Example

import

std

;

// import the entire Standard Library (C++

23

feature)

int

main

(

)

{

    std::cout

<<

"Hello, C++23 Modules!\n"

;

// use std::cout for output

   

return

0

;

}

Or import specific parts of the Standard Library:

Example

import

std.

core

;

     // import the core library

import

std.

iostream

;

 // import the input/output stream library

int

main

(

)

{

    std::cout

<<

"Hello, C++23 Modules!\n"

;

   

return

0

;

}

Currently, support for C++ Modules in mainstream compilers is being gradually improved.

The following are the support status and usage of some compilers:

GCC: needs the C++23 standard and module support enabled.

Example compile command:



g++ -std=c++23 -fmodules-ts -o program main.cpp

Clang: needs the C++23 standard and module support enabled.

Example compile command:



clang++ -std=c++23 -fmodules -o program main.cpp

MSVC (Visual Studio): needs the C++23 standard and module support enabled.

Example compile command:



cl /std:c++23 /experimental:module /EHsc /Fe:program main.cpp


Common C++ Standard Library Header Files

The C++ Standard Library contains many header files; each header file provides specific functionality.

The following are some common Standard Library header files and their uses:

is one of the most commonly used header files in the C++ Standard Library; it provides input/output stream functionality. Through this header file, we can usestd::coutandstd::cinfor standard output and input.

Example

#include

int

main

(

)

{

    std

::

cout

<<

"Hello, World!"

<<

std

::

endl

;

   

return

0

;

}

provides the implementation of the vector container. A vector is a dynamic array that can be dynamically resized at runtime. It provides many useful member functions, such aspush_back(), size(), etc.

Example

#include

int

main

(

)

{

    std

::

vector

<

int

vec

=

{

1

,

2

,

3

,

4

,

5

}

;

    vec.

push_back

(

6

)

;

   

for

(

int

i

:

vec

)

{

        std

::

cout

<<

i

<<

" "

;

   

}

   

return

0

;

}

provides common math functions, such as square root, trigonometric functions, and exponential functions. These functions are usually used in numerical computation.

Example

#include

#include

int

main

(

)

{

   

double

x

=

4.0

;

   

double

y

=

std

::

sqrt

(

x

)

;

    std

::

cout

<<

"Square root of "

<<

x

<<

" is "

<<

y

<<

std

::

endl

;

   

return

0

;

}

provides the implementation of the string class. The C++ string classstd::stringis easier to use than character arrays in C, and provides many useful member functions, such aslength(), substr(), etc.

Example

#include

#include

int

main

(

)

{

    std

::

string

str

=

"Hello, C++!"

;

    std

::

cout

<<

"Length of string: "

<<

str.

length

(

)

<<

std

::

endl

;

   

return

0

;

}

provides many common algorithms, such as sorting, searching, and traversal. These algorithms can be applied to various containers, such as vectors, lists, and so on.

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

int

vec

=

{

5

,

3

,

1

,

4

,

2

}

;

    std

::

sort

(

vec.

begin

(

)

, vec.

end

(

)

)

;

   

for

(

int

i

:

vec

)

{

        std

::

cout

<<

i

<<

" "

;

   

}

   

return

0

;

}


Notes