STL fill算法

本文内容来自C++Plus,本文只是本人的总结和翻译而已。本人只是C++的搬运工。

原文传送门:http://www.cplusplus.com/reference/algorithm/fill/

fill():在范围区间内填充值。

template <class ForwardIterator, class T>
  void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
  while (first != last) {
    *first = val;
    ++first;
  }
}

没有返回值。

// fill algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::fill
#include <vector>       // std::vector

int main () {
  std::vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0

  std::fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
  std::fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(标准模板库)提供了许多有用的算法函数,包括以下内容: 1. 非修改性序列操作: `std::all_of`, `std::any_of`, `std::none_of`, `std::for_each`, `std::count`, `std::count_if`, `std::mismatch`, `std::find`, `std::find_if`, `std::find_if_not`, `std::adjacent_find`, `std::search`, `std::search_n`, `std::equal`, `std::is_permutation`, `std::lexicographical_compare`. 2. 修改序列操作:`std::copy`, `std::copy_if`, `std::copy_n`, `std::copy_backward`, `std::move`, `std::move_backward`, `std::fill`, `std::fill_n`, `std::transform`, `std::generate`, `std::generate_n`, `std::replace`, `std::replace_if`, `std::replace_copy`, `std::replace_copy_if`, `std::swap`, `std::swap_ranges`, `std::iter_swap`, `std::reverse`, `std::reverse_copy`, `std::rotate`, `std::rotate_copy`, `std::unique`, `std::unique_copy`. 3. 排序和相关操作:`std::sort`, `std::stable_sort`, `std::partial_sort`, `std::partial_sort_copy`, `std::nth_element`. 4. 二分法操作:`std::lower_bound`, `std::upper_bound`, `std::binary_search`, `std::equal_range`. 5. 堆操作:`std::make_heap`, `std::push_heap`, `std::pop_heap`, `std::sort_heap`. 6. 集合操作:`std::merge`, `std::inplace_merge`, `std::includes`, `std::set_union`, `std::set_intersection`, `std::set_difference`, `std::set_symmetric_difference`. 7. 其他操作:`std::accumulate`, `std::iota`, `std::max`, `std::max_element`, `std::min`, `std::min_element`, `std::next_permutation`, `std::prev_permutation`. 这里只是列举了一些常见的算法函数,还有很多其他函数没有列举出来。这些函数可以在 `<algorithm>` 头文件中找到。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值