Essential C++ 第四章小结

2024-02-29 23:48
文章标签 c++ 小结 第四章 essential

本文主要是介绍Essential C++ 第四章小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随手画了一个图小结一下:


下面是根据书中的例子写的一些代码:

#include <iostream>
#include <vector>
#include "Triangulate_iterator.h"
using namespace std;static int arr[13] = {1,1,2,3,5,8,13,21,34,55,89,144,233};class Triangulate_iterator;class Triangulate {
public:typedef Triangulate_iterator iterator;/*申明Triangulate_iterator为Triangulate的友元,Triangulate_iterator里的函数都能对Triangulate的私有成员进行访问了*/friend class Triangulate_iterator;Triangulate();Triangulate(int length , int beg_pos);Triangulate(int length);Triangulate(const Triangulate&);Triangulate_iterator begin();Triangulate_iterator end();friend int operator* (const Triangulate_iterator &rhs);int length() const { return _length; }int beg_pos() const { return _beg_pos; }int length() { return _length; }int beg_pos() { return _beg_pos; }bool next(int &x) const;void next_reset() const;static bool is_elem( int );static int get_elem( int pos ) {return _elem[pos];}static void gen_elements( int length );static void gen_elems_to_value( int value );static void display( int length, int beg_pos, ostream &os = cout );static vector<int> _elem;private:string _name;int _length;int _beg_pos;mutable int _next;static const int _max_elems = 1024;};class Triangulate_iterator {
public:friend class Triangulate;Triangulate_iterator(int index) : _index(index-1) {}friend int operator* (const Triangulate_iterator &rhs);bool operator== ( const Triangulate_iterator& ) const;bool operator!= ( const Triangulate_iterator& ) const;int Triangulate_iterator::operator* (const Triangulate_iterator &rhs);Triangulate_iterator& operator++ ();Triangulate_iterator operator++ ( int );private:void check_integrity() const;  //检查 _index是否合理int _index;
};class iterator_overflow{};int operator* (const Triangulate_iterator &rhs) {rhs.check_integrity();return Triangulate::_elem[rhs._index];
}
inline bool Triangulate_iterator::operator == ( const Triangulate_iterator& rhs ) const {return this->_index == rhs._index;
}inline bool Triangulate_iterator::operator != ( const Triangulate_iterator& rhs ) const {return (! (*this == rhs ));
}
inline
int Triangulate_iterator::operator* (const Triangulate_iterator &rhs) {rhs.check_integrity();return Triangulate::_elem[_index];
}Triangulate_iterator& Triangulate_iterator::operator++ () {_index++;check_integrity();return *this;
}Triangulate_iterator Triangulate_iterator::operator++ ( int ) {Triangulate_iterator tem = *this;_index++;check_integrity();return tem;
}inline
void Triangulate_iterator::check_integrity() const {if ( _index >= Triangulate::_max_elems)throw iterator_overflow();if(_index >= Triangulate::_elem.size())Triangulate::gen_elements( _index + 1);
}Triangulate_iterator Triangulate::begin() {return Triangulate_iterator(_beg_pos);}Triangulate_iterator Triangulate::end() {return Triangulate_iterator(_beg_pos + _length);
}Triangulate::Triangulate(): _name("Triangulate"),_length(1),_beg_pos(1),_next(0) {}
Triangulate::Triangulate(int l, int b) : _name("Triangulate") {_length = l > 0 ? l : 1;_beg_pos = b > 0 ? b : 1;_next = _beg_pos-1;
}Triangulate::Triangulate(int l) : _name("Triangulate") {_length = l > 0 ? l : 1;_beg_pos = 1;_next = _beg_pos - 1;}Triangulate::Triangulate(const Triangulate &rhs) {_length = rhs._length;_beg_pos = rhs._beg_pos;_next = rhs._next;_name = rhs._name;
}bool Triangulate::next(int &elem) const {if(_next >= _length)return false;elem = _elem[_next];_next++;return true;
}void Triangulate::next_reset() const {_next = _beg_pos - 1;
}vector<int> Triangulate::_elem;int sum( const Triangulate &trian ) {if ( !trian.length())return 0;int sum = 0;int val = 0;trian.next_reset();while ( trian.next( val ))sum += val;return sum;
}bool Triangulate::is_elem( int value ) {if ( !_elem.size() || _elem[_elem.size()-1] < value)gen_elems_to_value(value);vector<int>::iterator found;vector<int>::iterator last = _elem.end();found = find(_elem.begin(), last, value);return found != last;
}void Triangulate::gen_elems_to_value( int value ) {int ix = _elem.size();if ( !ix ) {_elem.push_back(1);ix = 1;}if ( ix == 1) {_elem.push_back(1);ix = 2;}while ( _elem[ix-1] < value && ix < _max_elems) {_elem.push_back(_elem[ix-2] + _elem[ix-1]);ix++;}if( ix == _max_elems )cerr << " value too large " << value << " -- exceeds max size of "<< _max_elems << endl;}void Triangulate::gen_elements( int length ) {if ( length <= 0 || length > _max_elems )return;int ix = _elem.size();if ( length == 1 && ix < length ) {_elem.push_back(1);ix++;}if ( length == 2 && ix < length ) {if( ix == 0) {_elem.push_back(1);++ix;}_elem.push_back(1);++ix;}for (; ix < length; ix++ ) {_elem.push_back(_elem[ix-2] + _elem[ix-1]);}
}int main() {char ch;bool more = true;int i = Triangulate::_elem[0];while( more ) {cout << "Enter your value ";int value;cin >> value;cout << "the value " << (Triangulate::is_elem(value) ? "is " : "is not ")<< "the element of the Triangulate "<< " try more (y/n) " << endl;cin >> ch;if ( ch=='y' || ch=='Y' ) {more = true;} elsemore = false;}return 0;}


这篇关于Essential C++ 第四章小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Flutter打包APK的几种方式小结

《Flutter打包APK的几种方式小结》Flutter打包不同于RN,Flutter可以在AndroidStudio里编写Flutter代码并最终打包为APK,本篇主要阐述涉及到的几种打包方式,通... 目录前言1. android原生打包APK方式2. Flutter通过原生工程打包方式3. Futte

Docker镜像pull失败两种解决办法小结

《Docker镜像pull失败两种解决办法小结》有时候我们在拉取Docker镜像的过程中会遇到一些问题,:本文主要介绍Docker镜像pull失败两种解决办法的相关资料,文中通过代码介绍的非常详细... 目录docker 镜像 pull 失败解决办法1DrQwWCocker 镜像 pull 失败解决方法2总

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Ollama Python 使用小结

《OllamaPython使用小结》Ollama提供了PythonSDK,使得开发者能够在Python环境中轻松集成和使用本地运行的模型进行自然语言处理任务,具有一定的参考价值,感兴趣的可以了解一... 目录安装 python SDK启动本地服务使用 Ollama 的 Python SDK 进行推理自定义客

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

java String.join()的使用小结

《javaString.join()的使用小结》String.join()是Java8引入的一个实用方法,用于将多个字符串按照指定分隔符连接成一个字符串,本文主要介绍了javaString.join... 目录1. 方法定义2. 基本用法2.1 拼接多个字符串2.2 拼接集合中的字符串3. 使用场景和示例3