• C++11标准模板(STL)- 算法(std::fill_n)


    定义于头文件 

    算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

    将一个给定值复制赋值给一个范围内的 N 个元素

    std::fill_n

    template< class OutputIt, class Size, class T >
    void fill_n( OutputIt first, Size count, const T& value );

    (1)(C++11 前)

    template< class OutputIt, class Size, class T >
    OutputIt fill_n( OutputIt first, Size count, const T& value );

    (C++11 起)
    (C++20 前)

    template< class OutputIt, class Size, class T >
    constexpr OutputIt fill_n( OutputIt first, Size count, const T& value );

    (C++20 起)

    template< class ExecutionPolicy, class ForwardIt, class Size, class T >
    ForwardIt fill_n( ExecutionPolicy&& policy, ForwardIt first, Size count, const T& value );

    (2)(C++17 起)

    1) 若 count > 0 ,则赋值给定的 value 给始于 的范围的首 count 个元素。否则不做任何事。

    2) 同 (1) ,但按照 policy 执行。此重载仅若 std::is_execution_policy_v> 为 true 才参与重载决议。

    参数

    first-要修改的元素范围起始
    count-要修改的元素数
    value-要赋的值
    policy-所用的执行策略。细节见执行策略。
    类型要求
    - OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
    - ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。

    返回值

    (无)

    (C++11 前)

    count > 0 则为指向最后赋值元素后一位置的迭代器,否则为 first

    (C++11 起)

    复杂度

    对于 count > 0 ,准确赋值 count 次。

    异常

    拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

    • 若作为算法一部分调用的函数的执行抛出异常,且 ExecutionPolicy 为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。
    • 若算法无法分配内存,则抛出 std::bad_alloc 。

    可能的实现

    1. template<class OutputIt, class Size, class T>
    2. OutputIt fill_n(OutputIt first, Size count, const T& value)
    3. {
    4. for (Size i = 0; i < count; ++i) {
    5. *first++ = value;
    6. }
    7. return first;
    8. }

     调用示例

    1. #include <algorithm>
    2. #include <vector>
    3. #include <list>
    4. #include <iostream>
    5. #include <iterator>
    6. struct Cell
    7. {
    8. int x;
    9. int y;
    10. };
    11. std::ostream& operator<<(std::ostream &os, const Cell &c)
    12. {
    13. os << '{' << c.x << ',' << c.y << '}';
    14. return os;
    15. }
    16. int main()
    17. {
    18. std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    19. std::cout << "original vector: " ;
    20. std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    21. std::cout << "\n";
    22. std::cout << "fill vector -1: " ;
    23. std::fill_n(v.begin(), 5, -1);
    24. std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    25. std::cout << "\n\n";
    26. std::list<Cell> tCells{{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
    27. std::cout << "original list : " ;
    28. std::copy(tCells.begin(), tCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    29. std::cout << "\n";
    30. std::cout << "fill list : " ;
    31. std::fill_n(tCells.begin(), 3, Cell{10, 24});
    32. std::copy(tCells.begin(), tCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    33. std::cout << "\n";
    34. return 0;
    35. }

    输出

     

     

  • 相关阅读:
    FreeRTOS介绍 和 将FreeRTOS移植到STM32F103C8T6
    绘制第一个三角形
    Matlab论文插图绘制模板第124期—三维气泡图
    Vatee万腾的数字化掌舵:Vatee科技引领未来的新高度
    Robust Real-time LiDAR-inertial Initialization(实时鲁棒的LiDAR惯性初始化)论文学习
    CSS3 新增属性-边框圆角-文字阴影-盒子阴影
    CSS @符规则(@font-face、@keyframes、@media、@scope等)
    小白学go基础04-命名惯例对标识符进行命名
    Jenkins Maven pom jar打包未拉取最新包解决办法,亲测可行
    C++ 【多态】
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127443954