C + + language Learning (ix) Introduction to the--c++ standard Library I. Introduction to the C + + standard library 1, Introduction to C + + standard library
C + + standard library, a collection of class libraries and functions.
The C + + compiler vendor translates it into code according to the official ISO specification of the C + + standards committee. C + + compiler vendors must rely on the system invocation interfaces provided by their different operating systems in implementing the C + + standard library, so each platform has its own C + + standard library implementation.
The C + + standard library features the following:
A, the C + + standard library is not part of the C + + language standard, consisting of class libraries and function libraries.
B, classes and objects defined in the C + + standard library are located in the Std namespace.
C, C + + standard library header files are not with the. h suffix.
D, C + + standard library covers the function of C library.
2. C + + compilation Environment
The C + + compilation environment consists of the following:
The C + + compilation environment consists of the following:
A, different C + + compilers have their own different C + + extension syntax modules
B, different C + + compilers have the same C + + standard syntax module
C, different C + + compilers have their own compiler extension libraries
D, in order to facilitate the development of software, different C + + compilers are included with the language compatible library, C language compatible library header file with. h suffix, such as # include <stdio.h>, #include <math.h> etc.
E, the C + + standard library contains a sub-library that covers C library functions, usually header files start with C, such as # include <cmath>, #include <cstring>, etc.
F, different C + + compilers are implemented by C + + compiler vendors adhere to the C + + standard library, C + + standard library has the same function interface, but the internal implementation is different.
Second, C + + standard library implementation 1, C + + standard Library Implementation Introduction
The C + + standard library is a set of C + + template classes that provide common programming data structures and functions such as C + + components such as linked lists, heaps, arrays, algorithms, iterators, and so on. The C + + standard library contains the C standard library and is defined in the C + + standard.
The C + + compiler development vendor translates it into code according to the C + + ISO specification, which is officially published by the C + + Standards Board. The C + + compiler vendor must rely on the functionality provided by its operating system (read/write files, allocate memory, create threads, and other system calls) during the compiler implementation, so each platform has its own standard library implementation.
2, Gnu/linux version of the implementation
On Gnu/linux, the C + + standard library is typically implemented by the libstdc++ or GNU standard C + + library project. Typically, libstdc++ is used by default for all regular Linux distributions.
3, Mac and iOS version implementation
On Mac OS before OS X Mavericks (V10.9), libstdc++ is the default C + + standard library implementation. Beginning with OS X Mavericks, Apple switched to using libc++ as the C + + standard library implementation.
4. Windows Edition implementation
On Windows, the implementation of the C + + standard library has been strictly limited to Microsoft's official compiler, Visual Studio. The C + + standard library on Windows is commonly referred to as the C + + Runtime library (CRT), which covers the implementation of both C + +.
Windows started with the C + + standard library implemented as a CRTDLL.DLL library. Beginning with Windows 95, Microsoft began migrating it to MSVCRT [version number]?. DLL (Msvcr20.dll,msvcr70.dll, etc.).
Visual Studio 2015 introduces the depth of CRT refactoring. The implementation of the C + + standard library is migrated to a new library, Universal C Runtime Library (Universal CRT or UCRT), and compiled to UCRTBASE.DLL. UCRT is provided as part of the operating system starting with Windows 10.
5, the implementation of the Android version
Bionic is an implementation of the C standard library written by Google for its Android operating system and is used directly at the bottom. Third-party developers can access BIONIC,NDK through the Android Native Development Kit (NDK) to write Android applications using C and C + + code.
The NDK provides many implementations of the C + + standard library version:
A, Libc++, both the official Android system and the modern Mac operating system starting with Lollipop are used as C + + standard libraries. Starting with the NDK release of version 17, it became the only available C + + standard library implementation in the NDK.
B, gnustl,libstdc++ alias, deleted in NDK release 18.
C, STLport, third-party implementations of the C + + standard library, written by the STLport project, have been inactive since 2008. STLport was removed in the NDK release 18.
6, other implementation version
The C + + standard library also has a different implementation version: The Apache version of the C + + standard library, Ustl and Eastl, and so on. The Apache version of the C + + standard library focuses on portability.
Three, C + + standard library composition 1, language support function
<CSTDDEF>: Defines macros null and offsetof and other standard types size_t and ptrdiff_t. The difference from the corresponding standard C header file is that NULL is a supplemental definition of the C + + null pointer constant (C++11 already has the keyword nullptr), and the macro Offsetof accepts the struct or union type parameter as long as there are no non-static members of the member pointer type.
<LIMITS>: Provides definitions related to the basic data type. For example, for each numeric data type, it defines the maximum and minimum values that can be represented and the number of digits in the binary digits.
<CLIMITS>: Provides C-style definitions related to basic integer data types, and C + + style definitions in <limits>.
<CFLOAT>: Provides C-style definitions related to basic floating-point data types, and C + + style definitions in <limits>.
<CSTDLIB>: Provides macros and functions that support program startup and termination, and also declares many other miscellaneous functions, such as search and sort functions, from strings to numeric functions. <cstdlib> unlike the corresponding standard C header file stdlib.h, abort (void) is defined. The abort () function has additional functionality that does not call destructors for static or automatic objects, nor does it call functions passed to the atexit () function. It also defines the extra functionality of the exit () function, which can release a static object and invoke a function registered with atexit () in reverse order of registration. Clears and closes all open C streams and returns control to the host environment.
<NEW>: Supports dynamic memory allocation.
<TYPEINFO>: Supports type identification of variables during run time.
<EXCEPTION>: Support for exception handling is a way to handle errors that can occur in a program.
<CSTDARG>: Supports a function that accepts a variable number of arguments. That is, when you call a function, you can send data items of varying amounts to the function. Macros Va_arg, Va_end, Va_start, and va_list types are defined.
<CSETJMP>: Provides functions for C-style non-local jumps, which are not commonly used in C + +.
<CSIGNAL>: Provides C-style support for interrupt handling.
2. Stream input/output
<IOSTREAM>: Supports the input and output of standard flow cin, cout, Cerr, and clog, and also supports multibyte character standard streams wcin, Wcout, Wcerr, and Wclog.
<IOMANIP>: Provides a manipulation program that allows changing the state of the stream, thereby altering the format of the output.
<ios>: Defines the base class for iostream.
<istream>: Defines the template class for the input that manages the output stream buffers.
<ostream>: Defines the template class for the output of the management output stream buffers.
<sstream>: A stream input output that supports strings.
<FSTREAM>: Supports stream input and output of files.
<iosfwd>: Provides forward declarations for input and output objects.
<STREAMBUF>: Supports caching of stream inputs and outputs.
<CSTDIO>: provides C-style input and output for standard streams.
<cwchar>: A C-style input output that supports multibyte characters.
3. Diagnostic function
<stdexcept>: defines a standard exception.
<cassert>: Defines an assertion macro for run-time checking.
<CERRNO>: Supports C-style error messages.
4. Tool functions
<UTILITY>: Defines overloaded relational operators, simplifies the writing of relational operators, and defines the pair type, which is a template type that can store a pair of values.
<functional>: Defines a number of function object types and functions that support function objects, which are arbitrary objects that support the operator () () function call operator.
<MEMORY>: Defines a standard memory allocator for containers, functions that manage memory, and auto_ptr template classes.
<CTIME>: Supports system clock functions.
5. String processing
<STRING>: Provides support and definitions for string types, including single-byte strings (consisting of char), string and multibyte strings (composed of wchar_t).
<cctype>: Single-byte character category.
<cwctype>: Multibyte character category.
<CSTRING>: Provides functions for processing non-empty byte sequences and memory blocks. Unlike the corresponding standard C library header file, the C-style string generic C library function is returned with a value of const and non-const function pairs instead.
<CWCHAR>: To provide functions for processing, performing I/O, and converting multibyte character sequences, unlike the corresponding standard C library header files, the General C library functions of several multibyte C-style string operations are returned with a value of const and non-const function pairs instead.
<CSTDLIB>: Provides a function for converting a single-byte string to a numeric value, converting between multibyte characters and multibyte strings.
6. Container class template
<vector>: defines a vector sequence template, which is a type of array that can be re-set in size, safer and more flexible than a normal array.
<list>: Defines a list sequence template, which is a series of linked lists, often inserting and deleting elements at any location.
<deque>: Defines a deque sequence template that supports efficient insert and delete operations at the start and end.
<queue>: Defines the sequence adapter queue and priority_queue for queues (FIFO) data structures.
<stack>: Defines a sequence adapter stack for a stack (last-in-first-out) data structure.
<map>:map is an associative container type that is allowed to be unique based on key values and is stored in ascending order. Multimap is similar to map, but the key is not unique.
<set>:set is an associative container type that is used to store unique values in ascending order. Multiset is similar to set, but the value does not have to be unique.
<bitset>: Defines a bitset template for a fixed-length bit sequence, which can be considered as a fixed-length, compact bool array.
<array>: (TR1) fixed size array, supports replication.
<forward_list>: (c++11) One-way list, support fast random access.
<unordered_set>: (TR1) unordered container set, whose elements are randomly stored. Multiset is similar to set, but the value does not have to be unique.
<unordered_map>: (TR1) unordered container map, its key value is randomly stored. Multimap is similar to map, but the key is not unique.
7. iterators
<ITERATOR>: Provides definitions and support for iterators.
8. Algorithm
<algorithm>: Provides a set of algorithm-based functions, including permutation, sorting, merging, and searching.
<CSTDLIB>: Declare c standard library functions bsearch () and qsort () to search and sort.
<ciso646>: Allows you to use and instead of && in your code.
9. Numerical operation
<COMPLEX>: Supports the definition and operation of complex numeric values.
<VALARRAY>: supports operation of numeric vectors.
<numeric>: Defines a set of general mathematical operations, such as accumulate and inner_product, on a numeric sequence.
<cmath>: This is the C math library, which also has an overloaded function attached to support C + + conventions.
<cstdlib>: The provided function extracts the absolute value of an integer and takes a remainder operation on an integer.
10. Localization
<LOCALE>: The provided localization includes the character category, the sort sequence, and the currency and date representation.
<CLOCALE>: Provides C-style support for localization.
Four, C + + standard library Use example 1, C + + standard library Use example
#include <iostream>#include <string>using namespace std;int main(int argc, char *argv[]){ string s = "Hello, C++ Library."; cout << s <<endl; return 0;}
In the preceding code, the string class and the iostream stream class in the C + + standard library are used.
2. Examples of C library sub-modules used in C + + standard library
#include <iostream>#include <cmath>using namespace std;int main(int argc, char *argv[]){ string s = "Hello, C module of C++ Library."; cout << s <<endl; double d = sqrt(4.0); cout << d << endl; return 0;}
The code above uses the string class in the C + + standard library, the iostream stream class, and the Cmath Library of C libraries in the C + + standard library.
3, C language Compatibility library use example
#include <stdio.h>#include <math.h>int main(int argc, char *argv[]){ const char* s = "Hello, C Compatible Library."; printf("%s\n", s); double d = sqrt(4); printf("%lf\n", d); return 0;}
In the code above, the Stdio library and the math library of the C-compatible library are used.
V. C + + String Class 1, C strings
The C language does not support strings in the true sense, but you can use character arrays and a set of functions to implement string manipulation. The C language does not support custom types, so it is not possible to define string types.
2. C + + language string
The C + + language can be a custom type and can be used to complete the definition of a string type, but the C + + language does not have a native string type.
The C + + language implements support for string types through the string type provided by the C + + standard library.
The properties of the string class are as follows:
A, string connection is supported directly
B, string directly supports size comparison of strings
C, string support substring lookup and extraction directly
D, string directly supports insertion and substitution of strings
3. C + + string and number conversion
The C + + standard library provides related classes to convert strings and numbers.
The string stream class Sstream is used for string conversions.
The use of string stream classes requires include # include <sstream> header files.
Istringstream represents a string input stream, and Ostringstream represents a string output stream.
#include <iostream>#include <sstream>using namespace std;int main(int argc, char *argv[]){ //字符串转换为数字 istringstream iss("3.14"); double pi; if(iss >> pi) { cout << "PI = " << pi<< endl; } //数字转换为字符串 ostringstream oss; if(oss << pi) { string PI = oss.str(); cout << "PI = " << PI << endl; } return 0;}
4. String loop Right Shift example
#include <iostream>#include <sstream>#include <string>using namespace std;string operator >> (const string& s, int n){ string ret; unsigned int pos = 0; //右移位数计算 n = n % s.length(); //右移的开始位置 pos = s.length() - n; //右移部分的子串作为起始 ret = s.substr(pos); //连接未移动子串 ret += s.substr(0,pos); return ret;}int main(int argc, char *argv[]){ string s = "abcdef"; cout << (s >> 3) << endl;//"defabc" return 0;}
5, String class operation precautions
The string class stores character data inside a data space, saving the length of the current string through a member variable. Therefore, in C + + development, you cannot use the C language to directly manipulate the storage space of a string, but instead use the interface functions provided by string for string manipulation.
#include <iostream>#include <string>using namespace std;int main(int argc, char *argv[]){ string s = "12345"; s.reserve(20); //直接对string对象的存储空间进行操作 for(int i = 0; i < 5; i++) { s[5+i] = ‘a‘+i; } cout << s << endl;//12345 //string对象的存储空间修改结果如下: for(int i = 0; i < 5; i++) { cout << s[5+i] << endl; } //通过string类接口进行字符串操作 s.append("ABCD"); cout << s <<endl;//12345ABCD return 0;}
In the preceding code, after string manipulation of the underlying data of a string object directly, an interface using the string class may not have access to modifications to the underlying data of a string object.
C + + language Learning (ix) Introduction to--c++ Standard library