Basic Concepts in C++ Standard Library

  • template

The so-called templates are functions or categories written for "one or more unspecified types". When using templates, types can be passed as parameters either explicitly or implicitly.

For example, the following returns the larger of two numbers:

template<class T>
inline const T& max(const T& a,const T& b)
{
	return a < b ? : b : a;
}

The first line defines T as an arbitrary data type, specified by the caller when the function is called. This type is guided by class, but the type itself does not have to be class—any data type can be applied to this template as long as it provides the operations used in the template definition

  • Namespaces¶

 Namespaces group different identifiers into a named scope. If you define all identifiers in the namespace, the name of the namespace itself becomes the only identifier that may conflict with other global symbols. You must add the name of the namespace before the identifier to reference the symbols in the namespace. The name of the namespace and the identifier are separated by ::.

namespace AA {
	class File;
	void myGlobalFunc;
	...
}
...
//using a namespace identifier
AA::File obj;
AA::myGlobalFunc();
  •  The definition of main()

 According to the C++ standard specification, only two main() are correct and portable:

int main()
{
	...
}

int main(int argc, char* argv[])
{
	...
}

argv is a command line parameter, which can also be defined as char**. The function return type must be stated as int.

  • namespace std

Name conflicts often occur when you use different modules and libraries, because different modules and libraries may use the same identifier for different objects. The so-called namespace refers to a certain visible scope of the identifier.

In fact, all identifiers in the C++ standard library are defined in a namespace called std.

For any identifier in the C++ standard library, there are three choices:

① Directly specify the identifier

std::cout << std::hex << 3.4 << std::endl;

 ②Using declaration

using std::cout;
using std::endl;

cout << std::hex << 3.4 << endl;

 ③ Use using directive. All identifiers defined in std can be valid

using namespace std;

cout << hex << 3.4 << endl;
  •  STL components

①Containers Containers are used to manage a collection of certain types of objects. In response to different needs in the program, STL has prepared different container models. common are

        array;vector;list;forward_list;queue;deque;stack;

        set/multiset;map/multimap;unordered_set/unordered_map;

②Iterators Iterators are used to perform traversal actions on the elements of an object cluster. The main benefit of iterators is to provide all containers with a small set of common interfaces by which an operation can proceed to the next element in the cluster. An iterator can be thought of as a kind of smart pointer.

Algorithms Algorithms , used to process the elements in the cluster.

Allocator , responsible for space configuration and management. From the perspective of implementation, the allocator is a class template that realizes dynamic space configuration, space management, and space release

Adapter Adapter , a thing used to modify the container or functor or iterator interface, (just like the socket 220V voltage needs to pass through the transformer before it is connected to the computer)

Functor Function object , a class that behaves like a function.

The basic concept of STL is to separate data and operations. Data is managed by container classes, operations are defined by customizable algorithms, and iterators act as the glue between the two, allowing any algorithm to interact with any container.

STL treats data and algorithms separately. In a sense, the concept of STL is contradictory to the idea of ​​object-oriented programming (OOP). However, it allows you to combine various containers with various algorithms, with a very large flexibility within a small framework. 

Guess you like

Origin blog.csdn.net/yangSHU21/article/details/131534185