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

2024-03-28 10:18

本文主要是介绍C++ Primer 5th笔记(10)chapter10 泛型算法 :write,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/855339

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关