博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c ++ assign函数_vector :: assign()函数,以及C ++ STL中的示例
阅读量:2528 次
发布时间:2019-05-11

本文共 2433 字,大约阅读时间需要 8 分钟。

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
v1; vector
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 
#include
using namespace std;int main(){
//declaring vectors vector
v1; vector
v2; vector
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 100v2: 10 20 30 40 50v3: 10 20 30 40 50

Reference:

参考:

翻译自:

c ++ assign函数

转载地址:http://xsvzd.baihongyu.com/

你可能感兴趣的文章
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>