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 complete various tasks more efficiently. To use these features, we need to include the corresponding standard library header files in our programs.

This article will introduce in detail how to import the standard library in C++ and explain some common standard library header files and their uses.

What is the C++ standard library?

The C++ Standard Library is a part of the C++ language. It contains a series of classes and functions for handling common programming tasks, such as input/output, string operations, mathematical calculations, container management, and so on.

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


How to import the C++ standard library?

1. Use #include to include header files

In C++, we use#includePreprocessor directives are used to import standard library header files.

#includeThe directive tells the compiler to insert the specified header file contents into the current file during compilation. Header files usually contain function declarations, class definitions, macro definitions, and other content.

Basic Syntax

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

#include <header_file>

where,<header_file>is the name of the standard library header file.

Example

Example

#include <iostream> // import input/output stream library
#include <vector> // import vector container library
#include <cmath> // import math function library

In the above example, we imported three commonly used standard library header files:

  • <iostream>: provides input/output stream functionality, such asstd::coutandstd::cin。
  • <vector>Provides the implementation of the vector container, which is used to store dynamic arrays.
  • <cmath>: provides common mathematical functions, such assqrt()、sin()、cos()etc.

2. Use modules to import the standard library (C++20/C++23)

Starting from C++20, C++ introduced modules, and in C++23, support for standard library modules was further improved.

Modules provide a more efficient and safer way to import the standard library.

  • Faster compilation speedModules are compiled only once, and subsequent imports directly use the compiled binary interface.

  • Better isolationModules do not leak macro definitions and private symbols, avoiding naming conflicts.

  • Clear dependenciesThe module import and export mechanism makes code dependencies clearer.

Example

import std;// import the entire standard library (C++23features)

int main() {
    std::cout << "Hello, C++23 Modules!\n";// use std::cout to output
    return 0;
}

Or import specific parts of the standard library:

Example

import std.core;// import core library
import std.iostream;// import input/output stream library

int main() {
    std::cout << "Hello, C++23 Modules!\n";
    return 0;
}

Currently, mainstream compilers' support for C++ modules is gradually improving.

The following are some compilers' support status and usage methods:

GCCRequires enabling C++23 standard and module support.

Compile command example:

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

ClangRequires enabling C++23 standard and module support.

Compile command example:

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

MSVC(Visual Studio)Requires enabling C++23 standard and module support.

Compile command example:

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 of which provides specific functionality.

The following are some common standard library header files and their uses:

<iostream>

<iostream>It is one of the most commonly used header files in the C++ Standard Library, providing input/output stream functionality. Through this header file, we can usestd::coutandstd::cinto perform standard output and input.

Example

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

<vector>

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

Example

#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    vec.push_back(6);
    for (int i : vec) {
        std::cout << i << " ";
    }
    return 0;
}

<cmath>

<cmath>It provides commonly used mathematical functions, such as square root, trigonometric functions, exponential functions, etc. These functions are usually used for numerical calculations.

Example

#include <cmath>
#include <iostream>

int main() {
    double x = 4.0;
    double y = std::sqrt(x);
    std::cout << "Square root of " << x << " is " << y << std::endl;
    return 0;
}

<string>

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

Example

#include <string>
#include <iostream>

int main() {
    std::string str = "Hello, C++!";
    std::cout << "Length of string: " << str.length() << std::endl;
    return 0;
}

<algorithm>

<algorithm>It provides many commonly used algorithms, such as sorting, searching, traversal, etc. These algorithms can be applied to various containers, such as vectors, lists, etc.

Example

#include <algorithm>
#include <vector>
#include <iostream>

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

  • namespaceThe functions and classes in the C++ Standard Library are usually located instdthe namespace. Therefore, when using these functions and classes, you usually need to addstd::Prefix, or useusing namespace std;to avoid repeated inputstd::。

  • Header file protectionWhen writing your own header files, you usually need to use header guards to prevent duplicate inclusion. Standard library header files already include these protection mechanisms, so we do not need to worry about duplicate inclusion.

  • CompatibilityThe C++ Standard Library may have some differences across different compilers and platforms. Therefore, when writing cross-platform code, you need to pay attention to these differences and ensure that the code works correctly in different environments.

other extensions