码迷,mamicode.com
首页 > 编程语言 > 详细

C++ Primer笔记6_STL之泛型算法

时间:2014-06-09 23:35:41      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:c++   泛型   算法   迭代器   

1.泛型算法:

大多数算法定义在头文件algorithm中,标准库还在头文件numeric中定义了一组数值泛型算法

只读算法:

举例:

find函数用于找出容器中一个特定的值,有三个参数

int val =  10;//val为我们需要查找的值
auto result = find(vec.begin(), vec.end(), val):
cout << "The value "<< val  << (result == vec.end() ? "is not present" : "is present") << endl;

find将前两个表示范围的迭代器内的元素与val比较,返回指向第一个等于给定值val的元素的迭代器。如果没有找到,返回第二个参数,即容器尾部迭代器vec.end(),表示失败。


count函数用来统计容器中元素个数,下面给出例子:

#include <iostream>
#include <deque>
#include <algorithm>

using namespace std;

int main()
{
	string val("hello");
	deque<string> str;
	for(int i = 0; i<10; i++)
	{
		str.push_front(string("hello"));
	}
	
	int num = count(str.begin(), str.end(), val);

	cout << "The "<< "hello" << " num is: " << num << endl;

	return 0;
}
可以正确的统计出个数为10;


accumulate函数定义在numeric头文件中,用于统计指定迭代器范围内元素的和。

第三个参数是和的初值,用一般为0.

int num = accumulate(vec.begin(), vec.end(), 0);

例子:

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

int main()
{
	string val("hello");
	deque<string> str;
	vector<int> v;

	for(int i = 0; i<10; i++)
	{
		str.push_front(string("hello"));
		v.push_back(i);
	}
	
	int num = count(str.begin(), str.end(), val);

	string s = accumulate(str.begin(), str.end(), string(""));
	int sum = accumulate(v.begin(), v.end(), 0);
	cout << "sum of v: " << sum  << endl;
	cout << "sum of str: " << s  << endl;

	cout << "The "<< "hello" << " num is: " << num << endl;

	return 0;
}
运行结果:

sum of v: 45
sum of str: hellohellohellohellohellohellohellohellohellohello
The hello num is: 10



写容器元素算法:

fill函数用来向给定范围内容器写入数据。

fill(v.begin(), v.end(), 0); //将每个元素置为0


fill_n函数用来向指定位置写入指定个数的元素值。

fill_n(v.begin(), v.size(), 0); //将所有元素置为0

注意:不要在空容器上调用fill_n(或其他类似的写元素算法)

vector<int> v; //空容器
fill_n(v.begin(), 10, 0); //灾难!修改v中不存在的10个元素值

#include <iostream>
#include <deque>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

int main()
{
	string val("hello");
	deque<string> str;
	vector<int> v;

	for(int i = 0; i<10; i++)
	{
		str.push_front(string("hello"));
		v.push_back(i);
	}
	
	int num = count(str.begin(), str.end(), val);
	string s = accumulate(str.begin(), str.end(), string(""));
	int sum = accumulate(v.begin(), v.end(), 0);

	cout << "sum of v: " << sum  << endl;
	cout << "sum of str: " << s  << endl;
	cout << "The "<< "hello" << " num is: " << num << endl;

//	fill(v.begin(), v.end(), 0);//fill 0
	fill_n(v.begin()+2, v.size()-3, 0);//0、1、2... 从2开始删除保留9  
	for(vector<int>::iterator i = v.begin(); i!=v.end(); ++i)
	{
		cout << *i << " ";
	}

	cout << endl;

	vector<int> vv;//空容器
	//fill_n(vv.begin(), 10, 0);	segment fault!   会产生段错误!

	return 0;
}

copy函数接受三个参数,前两个表示一个范围,最后一个表示目标序列的起始位置。

int a1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int a2[sizeof(a1) / sizeof(a1[0])];//a2与a1一样大
copy(begin(a1), end(a1), a2);//把a1内容拷贝到a2

sort函数&unique函数:

sort函数接受两个迭代器,表示要排序的元素范围。它会使不重复的元素出现在vector的头部。最后面则是重复的元素

我们希望相邻的重复元素只保留一个,可以与unique函数配合使用,unique返回迭代器指向最后一个不重复元素之后的位置。

假设初识序列如下:

[the quick red fox jumps over the slow red turtle]

sort排序后:

[fox jumps over quick red red slow the the turtle]

使用unique后:

[fox jumps over quick red slow the turtle the turtle]

容器本身的大小并没有改变。

由于unique函数返回的是指向最后一个不重复元素之后的位置的迭代器

所以我们可以将最后的重复元素用erase删除。

words.erase(end_unique, words.end());

最终得到:[fox jumps over quick red slow the turtle]


完整的程序如下:

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

using namespace std;

void elimDups(vector<string> &words)
{
	sort(words.begin(), words.end());
	vector<string>::iterator i = unique(words.begin(), words.end());
	words.erase(i, words.end());
	return ;
}

void print(vector<string> &vec)
{
	for(vector<string>::iterator i = vec.begin(); i!=vec.end(); ++i)
	{
		cout << *i << " ";
	}
	cout << endl;
}

int main()
{
	string s[] = {"the", "quick", "red", "fox", "jumps",
					"over", "the", "slow", "red", "turtle"};
	vector<string> vec(s, s+sizeof(s)/sizeof(s[0]));

	print(vec);
	elimDups(vec);
	cout << "---------------------------------------------" << endl;
	print(vec);

	return 0;
}

运行结果如下:

the quick red fox jumps over the slow red turtle 
---------------------------------------------
fox jumps over quick red slow the turtle





C++ Primer笔记6_STL之泛型算法,布布扣,bubuko.com

C++ Primer笔记6_STL之泛型算法

标签:c++   泛型   算法   迭代器   

原文地址:http://blog.csdn.net/scottly1/article/details/28619089

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!