1. fill: 对给定区间全部赋予某值(algorithm.h)

将指定范围内的每个元素都设定为给定的值。如果输入范围有效,则可以安全写入。这个算法只会对输入范围内已存在的元素进行写入操作。

template<class FwdIt, class T>
void fill(FwdIt first, FwdIt last, const T& x);

fill(vec.begin(),vec.end(),0);//将每个元素都重置为0
fill(v.begin(), v.begin() + v.size()/2, 10);  //将容器的一个子序列设置为0

eg. “0 0 0 0 0 0 0”

     vector<int> v2 = { 4,5,6,7,8,9,4 };fill(v2.begin(), v2.end(), 0);//将每个元素都重置为0for(auto it:v2)cout << " " << it;

2. fill_n

对给定迭代器后的n个元素赋予某值。(从迭代器指向的元素开始,将指定数量的元素设置为给定的值)

template<class OutIt, class Size, class T>
void fill_n(OutIt first, Size n, const T& x);

eg.

fill_n(vec.begin(),n,val);
vector<int> vec;
fill_n(vec.begin(), 10, 0);//调用错误,fill_n并不是向容器中插入元素,它只负责更新元素的值。

eg. “0 0 0 7 8 9 4”

     vector<int> v2 = { 4,5,6,7,8,9,4 };fill_n(v2.begin(), 3, 0);//将每个元素都重置为0for (auto it : v2)cout << " " << it;

3. back_inserter:

back_inserter 实参是一个容器的引用,返回一个绑定在该容器上的插入迭代器。

  • 需要确保算法有足够的元素存储输出数据 。
  • 当对此迭代器赋值时,就push_back一个元素

eg. “4 5 6 7 8 9 4 0 0 0”

     vector<int> v2 = { 4,5,6,7,8,9,4 };  fill_n(back_inserter(v2), 3, 0);//将每个元素都重置为0for (auto it : v2)cout << " " << it;

4. copy

向目的位置迭代器指向的输出序列中的元素写入数据,接受三个迭代器(前两个指定输入范围,第三个指向目标序列的第一个元素。长度同样需要由程序员来保证)

template<class InIt, class OutIt>
OutIt copy(InIt first, InIt last, OutIt x);

copy算法带有三个迭代器参数:
eg. “0 1 2 3 4 5 6”

vector<int> v2 = { 4,5,6,7,8,9,4 };
int a1[] = { 0,1,2,3,4,5,6 };
auto ret = copy(begin(a1), end(a1), v2.begin());//把a1的内容拷贝给v2
for (auto it : v2)cout << " " << it;

5. replace

读入一个序列范围,将序列中某个值全部用一个新值来替换。
template<caass FwdIt, class T>
void replace(FwdIt first, FwdIt last, const T& vold, const T& vnew);
//该算法指定范围[first, last)内的所有元素值为vold替换为vnew。

eg. “42 5 6 7 8 9 42”

     vector<int> v2 = { 4,5,6,7,8,9,4 };int a1[] = { 0,1,2,3,4,5,6 }; replace(v2.begin(), v2.end(), 4, 42); for (auto it : v2)cout << " " << it;

6.replace_copy

保留原序列不变,此算法额外接受第三个迭代器参数,指出调整后序列的保存位置。指定保存替换后的序列的目标位置, 替换后放在另外一个序列中

template<class InIt, class OutIt, class T>
OutIt replace_copy(InIt first, InIt last, OutIt x, const T& vold, const T& vnew);

eg. “42 5 6 7 8 9 42”

     vector<int> v2 = { 4,5,6,7,8,9,4 };int a1[] = { 0,1,2,3,4,5,6 };vector<int> v3;replace_copy(v2.cbegin(), v2.cend(), back_inserter(v3), 4, 42);for (auto it : v3)cout << " " << it;

7.sort重排元素

stable_sort排序算法是稳定排序。(algorithm.h)
template
void sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr);
template
void stable_sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void stable_sort(RanIt first, RanIt last, Pred pr);

eg.
using stable_sort: 3 apples winter winter1 apples1 2222222222222
using sort: 3 apples winter winter1 apples1 2222222222222

vector<string> v2 = { "2222222222222","3" , "winter1", "apples1" , "apples" , "winter"};vector<string> v1;  v1.assign(v2.begin(), v2.end()); std::cout << "using stable_sort:";std::stable_sort(v1.begin(), v1.end(), less_len);for (auto it : v1)cout << " " << it;std::cout << '\n';v1.assign(v2.begin(), v2.end());std::cout << "using sort:";std::sort(v1.begin(), v1.end(), less_len);for (auto it : v1)cout << " " << it;std::cout << '\n';

8.unique和unique_copy

  • unique函数执行重排的操作,并不包含“去”的过程(即不会删除元素,算法不会改变容器大小)只是在有重复元素时,把后面的元素向前移动覆盖了原来的元素。函数返回的迭代器指向无重复元素序列最后一个元素的下一个位置。

  • unique_copy是它的“_copy”版本,返回的是生成的序列的最后一个元素的下一个位置。(algorithm.h)

    template
    FwdIt unique(FwdIt first, FwdIt last);
    template<class FwdIt, class Pred>
    FwdIt unique(FwdIt first, FwdIt last, Pred pr);

    template<class InIt, class OutIt>
    OutIt unique_copy(InIt first, InIt last, OutIt x);
    template<class InIt, class OutIt, class Pred>
    OutIt unique_copy(InIt first, InIt last, OutIt x, Pred pr);

注意:unique调用后,原序列的前面部分是无重复元素的序列,而后半部分是剩下没有被覆盖的序列。这里,需要手动删除后面的元素序列,范围由返回的迭代器和容器末端决定。

  • 删除一个空范围没有影响。
    eg. “fox quick red slow the”
     vector<string> words = { "the", "quick", "red", "fox","red", "the","slow" };sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());cout << " " << *end_unique;//值不确定words.erase(end_unique, words.end());for (auto it : words)cout << " " << it;std::cout << '\n';

eg. “1 2 the quick red fox red the slow”

vector<string> wordsNew = { "1", "2" };
vector<string> words = { "the", "quick", "red", "fox","red", "the","slow" }; //Create an insert_iterator for results
insert_iterator<vector<string> > ins(wordsNew, wordsNew.end());auto end_unique = unique_copy(words.begin(), words.end(), ins);
//cout << " " << end_unique;
for (auto it : wordsNew)cout << " " << it;
std::cout << '\n';

【引用】

  1. 代码 https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/genericAlgorithm.h

C++ Primer 5th笔记(10)chapter10 泛型算法 :write相关推荐

  1. C++ Primer 学习笔记 第十章 泛型算法

    C++ Primer 学习笔记 第十章 泛型算法 336 find函数 #include <iostream> #include <vector> #include <s ...

  2. C++ Primer 5th笔记(10)chapter10 泛型算法 :泛型算法结构

    名称 定义 输入迭代器 只读,不写:单遍扫描,只能递增 输出迭代器 只写,不读:单遍扫描,只能递增 前向迭代器 可读写,多遍扫描,只能递增 双向迭代器 可读写,多遍扫描,可递增递减 随机访问迭代器 可 ...

  3. C++ Primer 5th笔记(10)chapter10 泛型算法 :迭代器

    迭代器类别 名称 定义 插入迭代器 这些迭代器被绑定到一个容器上,可用来向容器插入元素 流迭代器 这些迭代器被绑定到输入或输出流上,可用来遍历所有关联的IO流 反向迭代器 这些迭代器向后而不是向前移动 ...

  4. C++primer第十章 泛型算法 10.4 再探迭代器 10.5 泛型算法结构

    除了为每个容器定义的迭代器之外,标准库在头文件iterator中还定义了额外几种迭代器.这些迭代器包括以下几种. 插入迭代器(insert iterator):这些迭代器被绑定到一个容器上,可用来向容 ...

  5. C++ primer 第10章 泛型算法

    文章目录 概述 find count 初识泛型算法 只读算法 只读算法accumulate 只读算法equal 写容器元素的算法 算法fill 算法fill_n back_inserter 算法cop ...

  6. C++ Primer 5th笔记(chap 14 重载运算和类型转换)函数调用运算符

    1. 定义 如果类定义了调用运算符(重载了函数调用运算符),则该类的对象被称作函数对象(function object),可以像使用函数一样使用该类的对象, eg. struct absInt{int ...

  7. C++ Primer 5th笔记(10)chapter10 泛型算法 : read

    标准库提供一组不依赖特定的容器类型的共性算法 指定迭代器范围, eg. [begin, end)这种左闭包 3种类型: 只读.写.sort 1. find 查询 template<class I ...

  8. C++ Primer 5th笔记(10)chapter10 泛型算法 :谓词

    1. 定义 谓词(predicate)是一个可调用的表达式,返回结果是一个能用作条件的值. 用于一个对象或一个表达式,如果可以对其使用调用运算符,则称为可调用的: 可调用的对象有:函数.函数指针.重载 ...

  9. C++ Primer 5th笔记(10)chapter10 泛型算法 :bind

    1. 定义 一个函数适配器,接受一个函数调用,增加或更改参数传递给另外一个函数调用. 比如 auto newCallable=bind(callable, arg_list); newCallable ...

最新文章

  1. python3.8.5怎么用-Python 3.8 新功能大揭秘【新手必学】
  2. SAP Cloud SDK for JavaScript 的搭建和使用方法介绍
  3. NEKO's Maze Game(思维)
  4. 施一公:如何提高英文的科研写作能力
  5. 【转】Word 2010 取消拼写/语法检查,隐藏红线/绿线
  6. @Import-给容器中加载bean的第三种方式
  7. C/C++ 编译器全面采用 LLVM
  8. Codeforces Round #503 (by SIS, Div. 2)
  9. 2015轻院校赛 H五子棋
  10. android markdown简历,Markdown+GitHub制作DIY简历
  11. ESP8266 - 首次使用点灯科技
  12. PYTHON开发毕业设计做什么好鸭?
  13. qq聊天纪录被删除应该如何恢复
  14. 前端面试题汇总(含答案)(HTML+CSS篇)
  15. 区块链共识机制 和 经典分布式共识机制
  16. 使用Selenium爬取网易云音乐的所有排行榜歌曲
  17. 开机脚本之——鼠标滚轮调整音量
  18. Linux 查看磁盘IO的使用
  19. KingbaseES PL/SQL 过程语言参考手册(4. 数据类型)
  20. Messko MT-ST160SK/TT/4/6M油温表

热门文章

  1. 【Python】青少年蓝桥杯_每日一题_6.19_画风车
  2. Python 值传递与地址传递总结
  3. c 多语言切换dll,【图片】老C教学之——给你的程序添加多语言支持【dll】【vb吧】_百度贴吧...
  4. 数据中心节水管理办法(范例)
  5. DL之SSD:基于tensorflow利用SSD算法实现目标检测(21类)
  6. DL之DeepLabv3:DeepLab v3和DeepLab v3+算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
  7. 成功解决AttributeError: module 'string' has no attribute 'find'
  8. ML之回归预测之Lasso:利用Lasso算法对红酒品质wine数据集实现红酒口感评分预测(实数值评分预测)
  9. Py之MT:Multithreaded的简介、引入、使用方法之详细攻略
  10. DL之InceptionV2/V3:InceptionV2 InceptionV3算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略