std中常见函数(持续更新)

本文介绍了C++中的几个常用函数,包括copy()用于复制容器元素,ostream_iterator用于向输出流写入元素,fill()和fill_n()用于填充序列,以及make_move_iterator、std::any_of、std::min_element、std::max_element、is_arithmetic、std::optional和std::numeric_limits的使用和功能。
摘要由CSDN通过智能技术生成

1. copy函数

copy() 函数是算法头的库函数,用于复制容器的元素,将容器的元素从给定的范围从给定的开始位置复制到另一个容器。
注意:使用 copy() 函数 - 包括标题或者您可以简单使用<bits/stdc++.h>头文件

std::copy() 函数的语法
std::copy(iterator source_first, iterator source_end, iterator target_start);
参数:
iterator source_first, iterator source_end- 是源容器的迭代器位置。
iterator target_start- 是目标容器的开始迭代器。
返回值: iterator- 它是指向已复制元素的目标范围末尾的迭代器。
//C++ STL program to demonstrate use of
//std::copy() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
   
    //declaring & initializing an int array
    int arr[] = {
    10, 20, 30, 40, 50 };
    //vector declaration
    vector<int> v1(5);

    //copying array elements to the vector
    copy(arr, arr + 5, v1.begin());

    //printing array
    cout << "arr:";
    for (int x:arr)
        cout << x << " ";
    cout << endl;

    //printing vector
    cout << "v1:";
    for (int x:v1)
        cout << x << " ";
    cout << endl;

    return 0;
}
arr:10 20 30 40 50
v1:10 20 30 40 50

2. ostream_iterator 使用说明

  • 使用ostream_iterator需要头文件#include
  • ostream_iterator写入元素的迭代器。
  • ostream_iterator用于向输出流ostream(如cout)中写入连续的元素。
    可以定义如下一个ostream_iterator:std::ostream_iterator oit (std::cout);
  • 也可以在上述构造函数中加入分隔符,如下:
    std::ostream_iterator oit (std::cout, ", ");
    用例

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>


int main()
{
   
    std::vector<int> data = {
    1, 21, 31, 41, 51, 61, 71, 81 };
    std::ostream_iterator<int> dataIter(std::cout, ", ");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gyqJulius_Caesar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值