C++小技巧

目录

STL

algorithm

for_each(start , end , function)

 is_sorted(start,end)

is_sorted_until(start,end)

sort(start,end,function)&stable_sort(start,end,function)

 lower_bound(start,end,val)&upper_bound(start,end,val)

 heap 

最大(max/max_element)/最小操作(min/min_element)。

 vector

map

set


STL

STL 是惠普实验室开发的一系列软件的统称,可以理解为一些容器的集合。STL的目的是标准化组件,这样就不用重新开发,可以使用现成的组件。STL 现在是C++的一部分,因此不用额外安装什么。

在 C++标准中,STL 被组织为下面的 17 个头文件:<algorithm>、<deque>、<functional>、<iterator>、<array>、<vector>、<list>、<forward_list>、<map>、<unordered_map>、<memory>、<numeric>、<queue><set>、<unordered_set>、<stack>和<utility>。

algorithm

它主要是对数组类型(或类似)的数据结构进行操作。

for_each(start , end , function)

【解释】
这个我只在 C++ primer 见过。意思是,从某个容器的 start 指针,到这个容器的 end 指针,均执行 function 函数。
【实例】

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int ia[] = { 1,2,3 };
vector<int>ivec(ia, ia + sizeof(ia) / sizeof(int));
void print(int& x)
{
	cout << x << endl;
}
int main()
{
	for_each(ivec.begin(), ivec.end(), print);
	return 0;
}
/* 输出结果
1
2
3  

相当于:将这个容器内的一个元素传入第一个未被占用的参数。缺参数?看一下这个例子。

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int ia[] = { 1,2,3 };
vector<int>ivec(ia, ia + sizeof(ia) / sizeof(int));
struct print
{
	const char* _prefix;
	print(const char* prefix) :_prefix(prefix) {}
	void operator()(int elem)
	{
		cout << _prefix << elem << endl;
	}
};
int main()
{
	for_each(ivec.begin(), ivec.end(), print("Element:"));
	return 0;
}
/* 输出结果
Element:1
Element:2
Element:3

 is_sorted(start,end)

【解释】返回一个 bool 值,判断这个区间是否排序。

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[5] = { 1,3,4,2,5 };
	cout << is_sorted(a, a + 5);
}
/* 输出
0
*/

is_sorted_until(start,end)

【解释】返回这个区间最大已排序的指针。

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[5] = { 1,3,4,2,5 };
	cout << is_sorted_until(a, a + 5) << endl;
	cout << *is_sorted_until(a, a + 5);
}
/* 输出
0078F870    
2
*/

sort(start,end,function)&stable_sort(start,end,function)

【解释】使得 start~end 按 function 规则排序。无返回值。复杂度为稳定 O(nlogn),其中 sort 为不稳定排序。function 当且仅当该容器为数组且 function 未定义时默认为从小到大。

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[5] = { 1,3,4,2,5 };
	sort(a, a+5);
	for (int i = 0; i < 5; i++)
		cout << a[i] << " ";
}
/* 输出
1 2 3 4 5
*/

 lower_bound(start,end,val)&upper_bound(start,end,val)

【解释】lower_bound(start,end,val):返回一个不小于 value 的最小指针;upper_bound(start,end,val):  返回一个大于 value 的最小指针。

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[5] = { 1,3,4,2,5 };
	cout << lower_bound(a, a + 5, a[1]) << endl;
	cout << *lower_bound(a, a + 5, a[1]) << endl;
	cout << upper_bound(a, a + 5, a[1]) << endl;
	cout << *upper_bound(a, a + 5, a[1]) << endl;
}
/* 输出
012FF7A4
3
012FF7A8
4
*/

 heap 

 

 

 

最大(max/max_element)/最小操作(min/min_element)。

【解释】max(num1,num2)&min(num1,num2);
max_element(start,end)&min_element(start,end);

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[5] = { 1,3,4,2,5 };
	cout << max_element(a, a + 5) << endl;
	cout << *max_element(a, a + 5) << endl;
	cout << min_element(a, a + 5) << endl;
	cout << *min_element(a, a + 5) << endl;
}
/* 输出
00AFF7A4
5
00AFF794
1
*/

 vector

【解释】<vector>是在 C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。

可以为一大堆有序容器建立容器, 这是一个动态数组,节约空间。

vector<int>vec;			//创建

vec.push_back(a);		//尾部插入元素:a 为一个元素

vec[0];				//访问:使用数组[]

vec.size();			//求大小

vec.clear();			//清空

vec.insert(pos, val);	        //v.插入:在 pos 的位置的前面插入 val 元素

vec.reverse(start, end);        //反转:将[start,end)之间的元素顺序颠倒。需要头文件<algorithm>

vec.erase(pos);			//vi.删除:删除第 pos + 1 个元素。
vec.erase(start, end);	        //或者:删除[start, end)之间的所有元素。
					
//迭代器:
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++)cout << *it << endl;

map

map 建立的是一个映射,可应用于离散化模型。

插入

// 定义一个map对象
map<int, string> mapStudent;
 
// 第一种 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, "student_zero"));
 
// 第二种 用insert函数插入value_type数据
mapStudent.insert(map<int, string>::value_type(001, "student_one"));
 
// 第三种 用"array"方式插入
mapStudent[123] = "student_first";
mapStudent[456] = "student_second";if (it != m.end())m.erase(it);

查找

map<int, int>::iterator it;
it = m.find(num);

删除

if (it != m.end())m.erase(it);

set

STL 对这个序列可以进行查找,插入删除序列中的任意一个元素,而完成这些操作的时间同这个序列中元素个数的对数成比例关系,并且
当游标指向一个已删除的元素时,删除操作无效。而一个经过更正的和更加实际的定义应该是:一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。这在收集一个数据的具体值的时候是有用的。集合中的元素按一定的顺序排列,并被作为集合中的实例。一个集合通过一个链表来组织,在插入操作和删除操作上比向量(vector)快,但查找或添加末尾的元素时会有些慢。具体实现采用了红黑树的平衡二叉树的数据结构。

//插入:
s.insert(num);
//删除:
s.erase(num);
//查找:
s.find(num);
//集合运算:
set set_intersection(set1, set2); //交集
set set_union(set1, set2); //并集
set set_difference(set1, set2); //差集

算法小技巧

判断是否为整数

使用Math.round(取最近的整数)、Math.ceil(向上取整)、Math.floor(向下取整)判断

整数取整后还是等于自己。利用这个特性来判断是否是整数,Math.floor示例,如下

#include <iostream>
#include<cmath>
bool function (double obj) {
    return floor(obj) == obj;
}
int main()
{
    double t;
    std::cin >> t;
    std::cout << function(t);
}
/*
输入 3
1
输入 3.3
0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2020-03-04 12:24  我等着你  阅读(165)  评论(0)    收藏  举报