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之泛型算法的更多相关文章

  1. [C++ Primer] : 第10章: 泛型算法

    概述 泛型算法: 称它们为"算法", 是因为它们实现了一些经典算法的公共接口, 如搜索和排序; 称它们是"泛型的", 是因为它们可以用于不同类型的元素和多种容器 ...

  2. C++ Primer 读书笔记:第11章 泛型算法

    第11章 泛型算法 1.概述 泛型算法依赖于迭代器,而不是依赖容器,需要指定作用的区间,即[开始,结束),表示的区间,如上所示 此外还需要元素是可比的,如果元素本身是不可比的,那么可以自己定义比较函数 ...

  3. C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构

    STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...

  4. C++ Primer : 第十章 : 泛型算法 之 只读、写和排序算法

    大多数算法都定义在<algorithm>头文件里,而标准库还在头文件<numeric>里定义了一组数值泛型算法,比如accumulate. ●  find算法,算法接受一对迭代 ...

  5. C++ Primer 5th 第10章 泛型算法

    练习10.1:头文件algorithm中定义了一个名为count的函数,它类似find,接受一对迭代器和一个值作为参数.count返回给定值在序列中出现的次数.编写程序,读取int序列存入vector ...

  6. 【足迹C++primer】30、概要(泛型算法)

    概要(泛型算法) 大多数算法的头文件中定义algorithm在. 标准库也是第一个文件numeric它定义了一套通用算法. #include<iostream> #include<n ...

  7. Chapter10(泛型算法)--C++Prime笔记

    关键:算法通过在迭代器上进行操作来实现类型无关.算法不改变所操作序列的大小. 1.算法大多都定义在algorithm头文件中,标准库还在头文件numeric中定义了一组数值泛型算法. 2.泛型算法永远 ...

  8. c++ primer 11 泛型算法

    使用泛型算法必须包含头文件#inlucde <algorithm> 标准库还定义一组泛化的算术算法,其命名习惯与泛型算法相同,包含头文件#include <numeric> f ...

  9. 【c++ Prime 学习笔记】第10章 泛型算法

    标准库未给容器添加大量功能,而是提供一组独立于容器的泛型算法 算法:它们实现了一些经典算法的公共接口 泛型:它们可用于不同类型的容器和不同类型的元素 利用这些算法可实现容器基本操作很难做到的事,例如查 ...

随机推荐

  1. Problem C: 指针:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面m个数

    #include<stdio.h> int move(int *x,int n,int m) { ]; int i; //int *p;指针循环变量p ;i<n;i++) t[i]= ...

  2. nginx负载均衡upstream参数配置

    一定要注意两台机器能够telnet 访问通过  如果不能通过则两台机器都执行一下 iptables -F 机器A: php-fpm配置[www]user = wwwgroup = wwwlisten ...

  3. IOS集成到支付宝的步骤及问题

    一,在支付宝开放平台下载支付宝SDK(https://openhome.alipay.com/platform/document.htm#down) 二,添加sdk文件到xcode 1,新建一个ali ...

  4. web安全之如何防止CSRF跨站请求伪造

    CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站 ...

  5. delphi AlphaControls

    http://blog.csdn.net/akof1314/article/details/7076804

  6. 【docker】解决docker pull镜像 拉取镜像龟速的问题,docker拉取镜像使用阿里云docker镜像加速器

    在docker拉取mysql镜像过程中,出现龟速的问题,解决这个问题的方法: 这个页面 停留了好久好久,依旧没有下载完成. 碰上这种情况 1.先退出Ctrl+C 2.在浏览器上进入阿里云docker库 ...

  7. if __name__=='__main__"在有的virtualenvs环境下执行成功,在有的环境下执行失败?

    我的项目是erebus,所以默认的python解释器位于virtualenvs下的erebus,但是这个执行环境执行某个py文件总是失败,换其他的执行器执行反而没有问题: 排查了半天,才发现erebu ...

  8. [转]Mapping Stored Procedure Parameters in SSIS OLE DB Source Editor

    本文转自:http://geekswithblogs.net/stun/archive/2009/03/05/mapping-stored-procedure-parameters-in-ssis-o ...

  9. MySQL5.6安装步骤(windows7/8_64位)

    [转]MySQL5.6安装步骤(windows7/8_64位) 1. 下载MySQL Community Server 5.6.11 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义 ...

  10. lumisoft邮件头不规范造成内容无法读取

    解决邮件内容为multipart,并且Param_Boundary等于null的不规范邮件内容无法读取问题,这样的邮件内容头部往往带两个或多个ContentType. 红色为自己加的,绿色为注释掉原来 ...