C++標準模板(STL)- 迭代器庫 - 流迭代器- 寫入 std::basic_streambuf 的輸出迭代器(二)

迭代器庫-流迭代器

?
迭代器庫提供了五種迭代器的定義,同時還提供了迭代器特征、適配器及相關的工具函數。

迭代器分類


迭代器共有五 (C++17 前)六 (C++17 起)種:遺留輸入迭代器 (LegacyInputIterator) 、遺留輸出迭代器 (LegacyOutputIterator) 、遺留向前迭代器 (LegacyForwardIterator) 、遺留雙向迭代器 (LegacyBidirectionalIterator) 、遺留隨機訪問迭代器 (LegacyRandomAccessIterator) ,及 遺留連續迭代器 (LegacyContiguousIterator) (C++17 起)。

迭代器的分類的依據并不是迭代器的類型,而是迭代器所支持的操作。換句話說,某個類型只要支持相應的操作,就可以作為迭代器使用。例如,完整對象類型指針支持所有遺留隨機訪問迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遺留隨機訪問迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指針。

迭代器的所有類別(除了遺留輸出迭代器 (LegacyOutputIterator) 和遺留連續迭代器 (LegacyContiguousIterator) )能組織到層級中,其中更強力的迭代器類別(如遺留隨機訪問迭代器 (LegacyRandomAccessIterator) )支持較不強力的類別(例如遺留輸入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入這些類別之一且亦滿足遺留輸出迭代器 (LegacyOutputIterator) 的要求,則稱之為可變 迭代器并且支持輸入還有輸出。稱非可變迭代器為常迭代器。

寫入 std::basic_streambuf 的輸出迭代器

std::ostreambuf_iterator
template< class CharT, class Traits = std::char_traits<CharT> >

class ostreambuf_iterator : public std::iterator<std::output_iterator_tag,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?void, void, void, void>
(C++17 前)

template< class CharT, class Traits = std::char_traits<CharT> >
class ostreambuf_iterator;

(C++17 起)

std::ostreambuf_iterator 是單趟遺留輸出迭代器 (LegacyOutputIterator) ,寫入相繼元素到為之創建迭代器的 std::basic_streambuf 對象。實際寫操作在賦值給迭代器(無論是否解引用)時進行。自增 std::ostreambuf_iterator 是無操作。

典型實現中, std::ostreambuf_iterator 僅有的數據成員是指向關聯 std::basic_streambuf 的指針,和指示是否抵達文件尾條件的布爾標志。

成員類型

成員類型定義
iterator_categorystd::output_iterator_tag
value_typevoid
difference_typevoid
pointervoid
referencevoid
char_typeCharT
traits_typeTraits
streambuf_typestd::basic_streambuf<CharT, Traits>
ostream_typestd::basic_ostream<CharT, Traits>

要求通過從 std::iterator<std::output_iterator_tag, void, void, void, void> 繼承獲得成員類型 iterator_categoryvalue_typedifference_typepointerreference

(C++17 前)

成員函數

(構造函數)

構造新的 ostreambuf_iterator
(公開成員函數)

(析構函數)

(隱式聲明)

銷毀 ostreambuf_iterator
(公開成員函數)

operator=

寫字符到關聯的輸出序列
(公開成員函數)

operator*

無操作
(公開成員函數)

operator++operator++(int)

無操作
(公開成員函數)

failed

測試是否輸出失敗
(公開成員函數)

寫字符到關聯的輸出序列

std::ostreambuf_iterator<CharT,Traits>::operator=

ostreambuf_iterator& operator=( CharT c );

若 failed() 返回 false ,則用 pbuf->sputc(c) 插入 c 到關聯的輸出流緩沖,其中 pbufstreambuf_type* 類型的私有成員。否則,不做任何事。

若調用 pbuf->sputc(c) 返回 Traits::eof ,則設置 failed() 標志為 true 。

參數

c-要插入的字符

返回值

*this

無操作

std::ostreambuf_iterator<CharT,Traits>::operator*

ostreambuf_iterator& operator*();

不做任何事,提供此函數以滿足遺留輸出迭代器 (LegacyOutputIterator) 的要求。

它返回迭代器自身,這使得可以用諸如 *iter = value 的代碼輸出(插入)值到底層的流。

參數

(無)

返回值

*this

無操作

std::ostreambuf_iterator<CharT,Traits>::operator++

ostreambuf_iterator& operator++();

ostreambuf_iterator& operator++( int );

不做任何事。提供這些運算符以滿足遺留輸出迭代器 (LegacyOutputIterator) 的要求。它們使得表達式 *iter++=value 和 *++iter=value 可用于輸出(插入)值到底層的流。

參數

(無)

返回值

*this

測試是否輸出失敗

std::ostreambuf_iterator<CharT,Traits>::failed

bool failed() const throw();

(C++11 前)

bool failed() const noexcept;

(C++11 起)

若迭代器遇到文件尾條件,即若先前對 std::basic_streambuf::sputc (由 operator= 執行)的調用返回 Traits::eof ,則返回 true。

參數

(無)

返回值

若迭代器在輸出時已遇到文件尾條件則為 true ,否則為 false 。

調用示例

#include <iostream>
#include <sstream>
#include <iterator>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <vector>
#include <time.h>struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell(const Cell &cell){x = cell.x;y = cell.y;}Cell(Cell &cell){x = cell.x;y = cell.y;cell.x = 0;cell.y = 0;}Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator +(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator *(const Cell &cell){x *= cell.x;y *= cell.y;return *this;}Cell &operator ++(){x += 1;y += 1;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}bool operator ==(const Cell &cell) const{return x == cell.x && y == cell.y;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}std::istream &operator>>(std::istream &is, Cell &cell)
{is >> cell.x;is >> cell.y;return is;
}// 定義一個簡單的迭代器適配器
template<typename _Iterator>
class move_iterator : public std::move_iterator<_Iterator>
{
public:// 使用基類的構造函數using std::move_iterator<_Iterator>::move_iterator;// 可以在此添加其他成員函數,如有需要
};template< class BDIter >
void alg(BDIter, BDIter, std::input_iterator_tag)
{//遺留輸入迭代器std::cout << "alg() called for input iterator" << std::endl;
}template< class BDIter >
void alg(BDIter, BDIter, std::output_iterator_tag)
{//遺留輸出迭代器std::cout << "alg() called for output iterator" << std::endl;
}template< class BDIter >
void alg(BDIter, BDIter, std::forward_iterator_tag)
{//遺留向前迭代器std::cout << "alg() called for forward iterator" << std::endl;
}template< class BDIter >
void alg(BDIter, BDIter, std::bidirectional_iterator_tag)
{//遺留雙向迭代器std::cout << "alg() called for bidirectional iterator" << std::endl;
}template <class RAIter>
void alg(RAIter, RAIter, std::random_access_iterator_tag)
{//遺留隨機訪問迭代器std::cout << "alg() called for random-access iterator" << std::endl;
}template< class Iter >
void alg(Iter first, Iter last)
{alg(first, last,typename std::iterator_traits<Iter>::iterator_category());
}int main()
{std::mt19937 g{std::random_device{}()};srand((unsigned)time(NULL));std::cout << std::boolalpha;std::string string = "This is an example";std::copy(string.cbegin(), string.cend(), std::ostreambuf_iterator<char>(std::cout));std::cout << std::endl;std::basic_filebuf<char> basic_filebuf;basic_filebuf.open("ostreambuf_iterator.txt", std::ios::out);std::ostreambuf_iterator<char> ostreambuf_iterator1(&basic_filebuf);std::ostreambuf_iterator<wchar_t> ostreambuf_iterator2(std::wcout);//寫字符到關聯的輸出序列//無操作*ostreambuf_iterator1 = 'a';std::copy(string.cbegin(), string.cend(), ostreambuf_iterator1);*ostreambuf_iterator2 = L'a';alg(ostreambuf_iterator1, ostreambuf_iterator1);//測試是否輸出失敗std::cout << "ostreambuf_iterator1.failed():    "<< ostreambuf_iterator1.failed() << std::endl;basic_filebuf.close();std::ostreambuf_iterator<char> ostreambuf_iterator3(&basic_filebuf);std::cout << "ostreambuf_iterator3.failed():    "<< ostreambuf_iterator3.failed() << std::endl;return 0;
}

輸出

This is an example
aalg() called for output iterator
ostreambuf_iterator1.failed():    false
ostreambuf_iterator3.failed():    false

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/40366.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/40366.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/40366.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MySQL環境搭配

下載版本37滴 下載第二個 之后進行安裝 進入安裝界面 next 選擇默認的 進行下一步 安裝成功后&#xff0c;進行一系列配置&#xff0c;成功界面如下&#xff1a; 配置 MySQL8.0 環境變量 如果不配置 MySQL 環境變量&#xff0c;就不能在命令行直接輸入 MySQL 登錄命令。 步…

強烈推薦!12 組超驚艷的 Midjourney 風格提示詞!

前言 Midjourney 的 --sref random 隨機風格功能推出之后&#xff0c;出現了很多對不同代碼生成效果的探索。今天就為大家推薦 12 組我覺得非常驚艷的風格代碼&#xff0c;將它們添加在提示詞中&#xff0c;不需要寫復雜的關鍵詞就能得到高質量的指定風格&#xff0c;并且效果…

CUDA編譯配置中來自 CUDA 12.1.targets 的MSB3721錯誤和核函數調用語法錯誤‘<’解決及可用的代碼示例框架

今天開始整cuda編程處理圖像&#xff0c;好久沒玩cuda&#xff0c;又從小白開始。情況不妙&#xff0c;第一個工程坑不少&#xff0c;記錄一下如下2個重要的錯誤&#xff1a; &#xff08;1&#xff09;來自 CUDA 12.1.targets 的MSB3721錯誤 錯誤 命令““C:\Program Files\N…

Scrapy框架的基本使用教程

1、創建scrapy項目 首先在自己的跟目錄文件下執行命令&#xff1a; PS D:\BCprogram\python_pro\bigdata> scrapy startproject theridion_grallatorscrapy startproject 項目名 具體執行操作如下&#xff1a;1、創建項目目錄&#xff1a;Scrapy會在當前工作目錄下創建一…

Git 操作總結

1. 安裝、Git 環境配置 1.1 安裝 Git 官方版本可以在 Git 官方網站下載&#xff1a;打開 https://git-scm.com/download/win&#xff0c;選擇相應版本即可。 Git 安裝完成后&#xff0c;可以在開始菜單中看到 Git 的三個啟動圖標&#xff08;Git Bash、Git CMD、Git GUI&…

koa導出數據為csv文件給前端下載

后端代碼 async userActivityExport(ctx) {const limit ctx.query.limit || 2const offset ctx.query.offset || 0const UserActivity ctx.module.db().entity(userActivity)const findOption {}const ret await UserActivity.findMany_(findOption)const firtCol Objec…

QT5.12環境搭建與源碼編譯

一、概述 QT版本&#xff1a;QT5.12.10 Qt網址&#xff1a;http://download.qt.io/archive/qt/ 編譯平臺 ubuntu18.04 二、安裝交叉編譯工具鏈 1、獲取交叉編譯工具鏈 一般如果是編譯系統如果有對應的gcc 就是用這個就可以了 比如rk3128 lin…

【Qt】QTableWidget設置可以選擇多行多列,并能復制選擇的內容到剪貼板

比如有一個 QTableWidget*m_tbwQuery m_tbwQuery->installEventFilter(this); //進行事件過濾處理//設置可以選擇多行多列 m_tbwQuery->setSelectionMode(QAbstractItemView::MultiSelection); m_tbwQuery->setSelectionBehavior(QAbstractItemView::SelectItems); …

字符串相似度算法完全指南:編輯、令牌與序列三類算法的全面解析與深入分析

在自然語言處理領域&#xff0c;人們經常需要比較字符串&#xff0c;這些字符串可能是單詞、句子、段落甚至是整個文檔。如何快速判斷兩個單詞或句子是否相似&#xff0c;或者相似度是好還是差。這類似于我們使用手機打錯一個詞&#xff0c;但手機會建議正確的詞來修正它&#…

如何為老化的汽車鉛酸電池充電

一項小研究表明&#xff0c;汽車鉛酸電池不同于深循環或固定電池。汽車電池旨在限度地提高啟動電流容量&#xff0c;并且對深度放電或浮充(也稱為第 3 階段充電循環)反應不佳。起動電池的極板結構使表面積化&#xff0c;并且電解液比重 (SG) 高于其他電池&#xff0c;以提供高啟…

C# 實現位比較操作

1、目標 對兩個字節進行比較&#xff0c;統計變化位數、一位發生變化的位數、二位發生變化的位數、多位發生變化的位數。 2、代碼 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Lin…

php 跨域問題

設置header <?php $origin isset($_SERVER[HTTP_ORIGIN])? $_SERVER[HTTP_ORIGIN]:;$allow_originarray(http://www.aaa.com,http://www.bbb.com, ); if( $origin in $allow_origin ){header("Access-Control-Allow-Origin:".$origin);header("Access-Co…

Electron Forge 打包更改打包后圖片

確認 ICO 文件有效 確保 icon.ico 文件是有效的并且包含多種分辨率的圖標&#xff08;如 16x16, 32x32, 48x48, 256x256&#xff09;。可以使用工具如 icoconverter 來生成有效的 ICO 文件。 https://icoconvert.com/確認圖標文件路徑 確保圖標文件路徑正確并且文件存在。 確…

O2OA(翱途) 開發平臺之HTTP端口規劃

O2OA(翱途) 開發平臺[下稱O2OA開發平臺或者O2OA]采用相對靈活的系統架構&#xff0c;支持三種服務器運行的方式。本篇主要闡述合并服務運行獨立服務運行代理端口運行三種服務器運行方式。 一、先決條件&#xff1a; 1、O2Server服務器正常運行&#xff0c;系統安裝部署請參考文…

Vue84-Vuex的工作原理與搭建開發環境

一、vuex工作原理 stats&#xff1a;是一個object對象&#xff0c;里面有很多key-value&#xff0c;存放的就是要操作的數據。mutations&#xff1a;是一個object對象&#xff0c;真正去操作stats的人。actions的作用&#xff1a;是一個object對象&#xff0c;當一個動作對應的…

【Spring Boot】關系映射開發(一):一對一映射

關系映射開發&#xff08;一&#xff09;&#xff1a;一對一映射 1.認識實體間關系映射1.1 映射方向1.2 ORM 映射類型 2.實現 “一對一” 映射2.1 編寫實體2.1.1 新建 Student 實體2.1.2 新建 Card 實體 2.2 編寫 Repository 層2.2.1 編寫 Student 實體的 Repository2.2.2 編寫…

DFS,BFS最短路,樹與圖的深度/廣度優先遍歷,拓撲排序

DFS 例題&#xff1a;排列數字 在排列組合問題中&#xff0c;每個位置需要嘗試多個不同的數字組合&#xff0c;需要回溯以嘗試不同的可能性。因此&#xff0c;需要顯式地恢復現場&#xff08;撤銷標記&#xff09;&#xff0c;以確保每個可能的路徑都被探索。 #include <b…

從漣漪到波浪:資產代幣化的變革力量

原文標題&#xff1a;《From ripples to waves: The transformational power of tokenizing assets》撰文&#xff1a;Anutosh Banerjee&#xff0c;Matt Higginson&#xff0c;Julian Sevillano&#xff0c;Matt Higginson編譯&#xff1a;Chris&#xff0c;Techub News本文來…

還是NC,項目代碼開源|scRNA+bulkRNA+因子分析驗證地塞米松治療Covid19

說在前面 平時發文章的話&#xff0c;做藥物用的大多都是僅僅是GEO的bulkRNA&#xff0c;有人的有鼠的&#xff0c;然后做做流水線分析&#xff0c;最后面PCR。今天看一篇發NC的工作量&#xff0c;怎么用轉錄組分析做藥物的轉化免疫學 這篇文章作者已經上傳Github了&#xff…

LabVIEW自動探頭外觀檢測

開發了一套基于LabVIEW的軟件系統&#xff0c;結合視覺檢測技術&#xff0c;實現探頭及連接器外觀的自動檢測。通過使用高分辨率工業相機、光源和機械手臂&#xff0c;系統能夠自動定位并檢測探頭表面的細微缺陷&#xff0c;如劃痕、殘膠、異色、雜物等。系統支持多種探頭形態&…