STL (Standard Template Library) is simple to use

STL (Standard Template Library) is simple to use

A. STL Introduction
      STL (Standard Template Library, the Standard Template Library) is a generic HP Labs developed a series of software. It is composed of Alexander Stepanov, Meng Lee and David R Musser developed while working out at HP Labs

II. Algorithm
      STL provides approximately 100 algorithm template functions, such as arithmetic for_each for the specified sequence, each element calls the specified function, stable_sort you to the rules specified sort sequence stability and so on. As a result, after as long as we are familiar with the STL, much of the code can be greatly simplified, just by calling a two algorithms template, you can complete the required functions and greatly enhance efficiency.

      The main part of the algorithm header file <algorithm>, <numeric> and <functional> composition.
      (1). <Algorithm> STL header files are all in one of the largest (although it is well understood), which is composed of a bunch of template functions, each function can be considered largely are independent, wherein the common features related to the scope of comparison, switching, find, traversal operation, copy, modify, remove, reverse, sort, merge, and the like.
      (2). <Numeric> small size, a template includes only a few simple math functions in the above sequence, including addition and multiplication operation on the sequence number.
      (. 3). <Functional> it defines the number of template class to declare function object.

Here is a simple use of vector

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

using namespace std;
void del_arr_first(vector<int> &arr)
{
	arr.erase(arr.begin());  //删除vector中的第一个数据
}
void print_arr(vector<int> &arr)
{
	for (vector<int>::iterator vec = arr.begin();vec != arr.end();vec++)
	{
		cout << *vec << " ";
	}
	cout << endl;
}
void print_sort(vector<int> &arr)	//引用传递动态数组
{
	sort(arr.begin(), arr.end());	//对动态数组进行从小到大的排序
}

int main()
{
	vector<int> num;

	//将数据压入数组num中
	num.push_back(213);
	num.push_back(324);
	num.push_back(244);
	num.push_back(355);
	num.push_back(466);

	num.insert(num.begin(), 200);	//在num的开头插入数据200
	num.insert(num.end(), 210);		//在num的末尾插入数据210

	print_arr(num);				//输出数组
	print_sort(num);			//排序数组
	print_arr(num);

	cout << endl;
	del_arr_first(num);			//删除数组首元素
	print_arr(num);

	system("pause");
	return 0;
}

The result:
Here Insert Picture Description
articles Copyright

A: writing tutorials for individuals, for individuals real and effective, but does not guarantee work for everyone, any problems or economic loss would I have nothing
II: More about my tutorial go to: 152.136.70.33

Guess you like

Origin blog.csdn.net/qq_38937025/article/details/90733371