C++标准模板(STL)- 迭代器库 - 流迭代器- 从 std::basic_istream 读取的输入迭代器 (三)

迭代器库-迭代器原语

迭代器库提供了五种迭代器的定义,同时还提供了迭代器特征、适配器及相关的工具函数。

迭代器分类

迭代器共有五 (C++17 前)六 (C++17 起)种:遗留输入迭代器 (LegacyInputIterator) 、遗留输出迭代器 (LegacyOutputIterator) 、遗留向前迭代器 (LegacyForwardIterator) 、遗留双向迭代器 (LegacyBidirectionalIterator) 、遗留随机访问迭代器 (LegacyRandomAccessIterator) ,及 遗留连续迭代器 (LegacyContiguousIterator) (C++17 起)。

迭代器的分类的依据并不是迭代器的类型,而是迭代器所支持的操作。换句话说,某个类型只要支持相应的操作,就可以作为迭代器使用。例如,完整对象类型指针支持所有遗留随机访问迭代器 (LegacyRandomAccessIterator) 要求的操作,于是任何需要遗留随机访问迭代器 (LegacyRandomAccessIterator) 的地方都可以使用指针。

迭代器的所有类别(除了遗留输出迭代器 (LegacyOutputIterator) 和遗留连续迭代器 (LegacyContiguousIterator) )能组织到层级中,其中更强力的迭代器类别(如遗留随机访问迭代器 (LegacyRandomAccessIterator) )支持较不强力的类别(例如遗留输入迭代器 (LegacyInputIterator) )的所有操作。若迭代器落入这些类别之一且亦满足遗留输出迭代器 (LegacyOutputIterator) 的要求,则称之为可变 迭代器并且支持输入还有输出。称非可变迭代器为常迭代器。

从 std::basic_istream 读取的输入迭代器 

std::istream_iterator
template< class T,

          class CharT = char,
          class Traits = std::char_traits<CharT>,
          class Distance = std::ptrdiff_t >
class istream_iterator: public std::iterator<std::input_iterator_tag,

                                             T, Distance, const T*, const T&>
(C++17 前)
template< class T,

          class CharT = char,
          class Traits = std::char_traits<CharT>,
          class Distance = std::ptrdiff_t >

class istream_iterator;
(C++17 起)

std::istream_iterator 是单趟输入迭代器,从为之创建迭代器的 std::basic_istream 对象读取 T 类型的相继对象,通过调用适当的 operator>> 。实际读取操作在自增,而非解引用迭代器时进行。在构造迭代器时读取首个对象。解引用只返回最近读取的对象的副本。

默认构造的 std::istream_iterator 被称为流尾迭代器。合法的 std::istream_iterator 抵达底层流尾时,它变得等于流尾迭代器。解引用和进一步自增它导致未定义行为。

std::istream_iterator 的典型实现保有二个数据成员:指向关联 std::basic_istream 对象的指针和最近读取的 T 类型值。

T 必须满足可默认构造 (DefaultConstructible) 、可复制构造 (CopyConstructible) 和可复制赋值 (CopyAssignable) 要求。

比较两个 istream_iterator

operator==,!=(std::istream_iterator)
template< class T, class CharT, class Traits, class Dist >

bool operator==( const istream_iterator<T,CharT,Traits,Dist>& lhs,

                 const istream_iterator<T,CharT,Traits,Dist>& rhs );
(1)
template< class CharT, class Traits >

bool operator!=( const istream_iterator<T,CharT,Traits,Dist>& lhs,

                 const istream_iterator<T,CharT,Traits,Dist>& rhs );
(2)

检查 lhsrhs 是否相等。二个流迭代器若均为流尾迭代器或均表示同一流则相等。

1) 检查 lhs 是否等于 rhs

2) 检查 lhs 是否不等于 rhs

参数

lhs, rhs-要比较的流迭代器

返回值

1) 若 lhs 等于 rhs 则为 true ,否则为 false 。

2) 若 lhs 不等于 rhs 则为 true ,否则为 false 。

异常

(无)

调用示例

#include <iostream>
#include <sstream>
#include <iterator>
#include <numeric>
#include <algorithm>

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::istringstream istringstream("1 2 3 4 5 6 7 8 9 10");
    std::cout << "partial_sum Cell: ";
    std::partial_sum(std::istream_iterator<Cell>(istringstream),
                     std::istream_iterator<Cell>(),
                     std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::istringstream istringstream2("1 2 3 4 5 6 7 8 9 10");
    std::cout << "The first Cell cell.x + cell.y > 12 is ";
    auto function1 = [](const Cell & cell)
    {
        return cell.x + cell.y > 12;
    };

    std::istream_iterator<Cell> istream_iterator1
        = std::find_if(std::istream_iterator<Cell>(istringstream2),
                       std::istream_iterator<Cell>(), function1);
    //返回到当前元素的指针或引用。
    std::cout << *istream_iterator1 << std::endl;
    alg(istream_iterator1, istream_iterator1);

    auto function2 = [](const Cell & cell)
    {
        return cell.x + cell.y > 10;
    };
    std::istringstream istringstream3("1 2 3 4 5 6 7 8 9 10");
    std::istream_iterator<Cell> istream_iterator2
        = std::find_if(std::istream_iterator<Cell>(istringstream3),
                       std::istream_iterator<Cell>(), function2);
    std::cout << *istream_iterator1
              << " == "
              << *istream_iterator2
              << std::boolalpha << "  "
              << (istream_iterator1 == istream_iterator2)
              << std::endl;
    std::cout << *istream_iterator1
              << " != "
              << *istream_iterator2
              << std::boolalpha << "  "
              << (istream_iterator1 != istream_iterator2)
              << std::endl;
    //从底层流读值并存储它到迭代器对象。若读取失败,则迭代器变为流尾迭代器。
    ++istream_iterator2;
    std::cout << "istream_iterator2: ";
    std::cout << *istream_iterator2 << std::endl;

    std::cout << *istream_iterator1
              << " == "
              << *istream_iterator2
              << std::boolalpha << "  "
              << (istream_iterator1 == istream_iterator2)
              << std::endl;
    std::cout << *istream_iterator1
              << " != "
              << *istream_iterator2
              << std::boolalpha << "  "
              << (istream_iterator1 != istream_iterator2)
              << std::endl;
    return 0;
}

输出

partial_sum Cell: {1,2} {4,6} {9,12} {16,20} {25,30}
The first Cell cell.x + cell.y > 12 is {7,8}
alg() called for input iterator
{7,8} == {5,6}  false
{7,8} != {5,6}  true
istream_iterator2: {7,8}
{7,8} == {7,8}  false
{7,8} != {7,8}  true

  • 23
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用C++20的STL迭代器输入进行排序的完整代码: ```c++ #include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<int> nums; // 通过迭代器读入数字 std::cout << "请输入数字,以-1结束输入:" << std::endl; std::istream_iterator<int> input_iter(std::cin); std::istream_iterator<int> eof; std::copy(input_iter, eof, std::back_inserter(nums)); // 对数字进行排序 std::sort(nums.begin(), nums.end()); // 输出排序后的结果 std::cout << "排序后的结果为:" << std::endl; std::ostream_iterator<int> output_iter(std::cout, " "); std::copy(nums.begin(), nums.end(), output_iter); std::cout << std::endl; return 0; } ``` 在上面的代码中,我们首先定义了一个 `std::vector<int>` 容器 `nums`,然后通过迭代器标准输入中读入数字,并将其加入到容器中。 接着,我们使用STL中的 `std::sort` 算法对容器中的数字进行排序。 最后,我们使用迭代器将排序后的结果输出到标准输出中。 需要注意的是,我们在读入数字时,使用了 `std::istream_iterator<int>` 迭代器来从标准输入中读入数字,这个迭代器会自动读取下一个整数并返回。在读入结束后,我们使用 `std::istream_iterator<int>()` 来创建一个“空”的迭代器,表示输入已经结束。在将数字输出时,我们使用了 `std::ostream_iterator<int>` 迭代器来将数字输出到标准输出中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值