c ++ assign函数_vector :: assign()函数,以及C ++ STL中的示例

C++ `vector::assign()` 是`vector`头文件中的一个库函数,用于初始化或赋值给定向量,它可以更新现有内容并根据新内容调整向量大小。在使用向量时,需要包含 `<vector>` 头文件。函数有两种用法,一种是通过迭代器指定范围赋值,另一种是设置向量大小和固定值。示例程序展示了如何使用 `vector::assign()` 进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c ++ assign函数

C ++ vector :: assign()函数 (C++ vector::assign() function)

vector::assign() is a library function of "vector" header, it is used to initialize a vector or assign content to a vector, it assigns the new content to the vector, update the existing content, and also resizes the vector's size according to the content.

vector :: assign()“ vector”标头的库函数,用于初始化矢量或将内容分配给矢量,将新内容分配给矢量,更新现有内容,并调整矢量的大小根据内容。

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::assign() function

vector :: assign()函数的语法

    vector::assign(iterator_first, iterator_last);
    vector::assign(size_type n, value_type value);

Parameter(s):

参数:

In case of type 1: iterator_first, iterator_last – are the first and last iterators of a sequence with them we are going to assign the vector.
In case of type 2: n – is the size of the vector and value – is a constant value to be assigned.

在类型1的情况下, iterator_first和iterator_last –是序列的第一个和最后一个迭代器,我们将为其分配向量。
对于类型2: n –是向量的大小,而value –是要分配的常数。

Return value: void – In both of the cases it returns nothing.

返回值: void –在两种情况下均不返回任何内容。

Example:

例:

    Input:
    vector<int> v1;
    vector<int> v2;
    
    //assigning
    v1.assign(5, 100);
    v2.assign(v1.begin(), v1.end());

    Output:
    //if we print the value
    v1: 100 100 100 100 100
    v2: 100 100 100 100 100

C ++程序演示vector :: assign()函数的示例 (C++ program to demonstrate example of vector::assign() function)

//C++ STL program to demonstrate example of
//vector::assign() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //declaring vectors
    vector<int> v1;
    vector<int> v2;
    vector<int> v3;

    //an array that will be used to assign a vector
    int arr[] = { 10, 20, 30, 40, 50 };

    //assigning vectors
    //assigning v1 with 5 elements and 100 as default value
    v1.assign(5, 100);

    //assigning v1 with array
    v2.assign(arr + 0, arr + 5);

    //assigning v3 with vector v2
    v3.assign(v2.begin(), v2.end());

    //pritning the vectors
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    cout << "v2: ";
    for (int x : v2)
        cout << x << " ";
    cout << endl;

    cout << "v3: ";
    for (int x : v3)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

输出量

v1: 100 100 100 100 100
v2: 10 20 30 40 50
v3: 10 20 30 40 50

Reference: C++ vector::assign()

参考: C ++ vector :: assign()

翻译自: https://www.includehelp.com/stl/vector-assign-function-with-example.aspx

c ++ assign函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值