stl向量_如何在C ++ STL中将数组元素复制到向量?
stl向量
Given an array and we have to copy its elements to a vector in C++ STL.
给定一个数组,我们必须将其元素复制到C ++ STL中的向量。
将数组元素复制到向量 (Copying array elements to a vector)
In C++ STL, we can copy array elements to a vector by using the following ways,
在C ++ STL中,我们可以使用以下方式将数组元素复制到向量中 :
Assigning array elements while declaring a vector
在声明向量的同时分配数组元素
When we declare a vector we can assign array elements by specifying the range [start, end] of an array.
声明向量时,可以通过指定数组的范围[开始,结束]来分配数组元素。
vector<type> vector_name(array_start, array_end);
By using copy function
通过使用复制功能
copy() function is a library function of algorithm header it can be used to copy an array’s elements to a vector by specifying the array range [start, end] and an iterator pointing to the initial position (from where we want to assign the content) of the vector.
copy()函数是算法标头的库函数,可通过指定数组范围[start,end]和指向初始位置的迭代器(用于从中分配内容)将其复制到向量中向量)。
vector<type> vector_name(size); std::copy(array_start, array_end, vector_start_iterator);
Note: To use vector – include <vector> header, and to use copy() function – include <algorithm> header or we can simply use <bits/stdc++.h> header file.
注意:要使用vector –包含<vector>头文件,而要使用copy()函数 –包含<algorithm>头文件,或者我们可以简单地使用<bits / stdc ++。h>头文件。
C ++ STL程序将数组元素复制到向量 (C++ STL program to copy array elements to a vector )
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{//an array
int arr[] = { 10, 20, 30, 40, 50 };
//assigning array to vector while declaring it
vector<int> v1(arr + 0, arr + 5);
//declaring an arrray first
//and then copy the array content
vector<int> v2(5);
copy(arr + 0, arr + 5, v2.begin());
//printing the vectors
cout << "vector (v1): ";
for (int x : v1)
cout << x << " ";
cout << endl;
cout << "vector (v2): ";
for (int x : v2)
cout << x << " ";
cout << endl;
return 0;
}
Output
输出量
vector (v1): 10 20 30 40 50
vector (v2): 10 20 30 40 50
翻译自: https://www.includehelp.com/stl/how-to-copy-array-elements-to-a-vector.aspx
stl向量
stl向量_如何在C ++ STL中将数组元素复制到向量?相关推荐
- java数组删除数组元素_如何在Java中删除数组元素
java数组删除数组元素 When we create an array in Java, we specify its data type and size. This is used by JVM ...
- flutter - 如何在Dart/Flutter中将某些元素从一个Map复制到新Map中?
flutter - 如何在Dart/Flutter中将某些元素从一个Map复制到新Map中? 如何在Dart / Flutter中将某些元素从一个Map复制到新Map中? Old_Map = {'A' ...
- c++ stl 获取最小值_如何在C ++ STL中找到向量的最小/最小元素?
c++ stl 获取最小值 Given a vector and we have to minimum/smallest element using C++ STL program. 给定一个向量,我 ...
- stl标准模板库_如何在C ++ STL(标准模板库)中使用Pair
stl标准模板库 In this article, we'll take a look at using pair in C++ Standard Template Library (STL). 在本 ...
- wps怎么关闭修改痕迹_如何在wps文字中将修订标记去掉
竭诚为您提供优质文档 / 双击可除 如何在 wps 文字中将修订标记去掉 篇一:如何去掉 word 文档的标记修订和批注 如何去掉 word 文档的标记(含修订和批注) (即不显示已删除内容) 你想让 ...
- word中将空格替换为_如何在Office 2007中将Word保存为PDF
word中将空格替换为 How to save a Word document to be a PDF in Office 2007? 如何在Office 2007中将Word 文档保存为PDF? I ...
- stl向量_如何检查C ++ STL中向量中是否存在元素?
stl向量 Given a vector and an element to be searched in the vector. 给定一个向量和要在向量中搜索的元素. To check whethe ...
- 一维向量转换为n维向量_如何在N维上固定万向节锁
一维向量转换为n维向量 Gimbal lock is a common issue that arises in 3D rotation systems. Conventional wisdom sa ...
- android pdf阅读器开发_如何在 Windows 10 中将 Firefox 设置为默认 PDF 阅读器
PDF 作为办公一族中的必备文档,很多人都会编辑或者创建 PDF 文档,而在 Windows 系统中并没有默认的 PDF 阅读器.而在新版的 Microsoft Edge微软已在开始提供有真正的高级 ...
最新文章
- 【 MATLAB 】使用 residuez 函数求 z 反变换的几个案例分析
- 排查一般MySQL性能问题
- python用途与前景-Python 就业方面的选择与应用分析
- Django和uwsgi,配合nginx做静态缓存
- ubuntu通过xmange连接桌面
- [Cake] 0.C#Make自动化构建-简介
- Zabbix二次开发_03api列表
- UE3 光照 阴影
- 混合架构、暗数据...这些云原生安全 bug 稍不留神会带来灾难!
- 论文阅读2--《融合多因素的短时交通流预测研究》
- 使用redis做缓存处理时出现的问题
- mysql sql练习题_Mysql----sql语句练习题(一)
- quot 云计算 quot 是计算机,云计算是什么意思?
- ios java模拟器 2017_Visual Studio 2017(Xamarin)未显示iPhone模拟器列表
- 信息学奥赛一本通 1359:围成面积
- 华为手机灵敏度设置_华为调屏幕灵敏度设置方法
- 天气预报小程序的设计与实现
- 硬盘安装ubuntu 14.04 LTS
- 一年一度的食品饮料新消费盛会,FBNB2022首届新食力大会5月开幕
- 用爬虫收集网站公开数据丨Python爬虫实战系列(5)
热门文章
- Silverlight 异步单元测试
- 解决一次由于SSL证书到期导致的网站不能访问的问题(Nginx,php,Apache)
- 监听文本框数据修改,特别是微信等客户端直接选择粘贴修改
- linux 虚拟机添加网卡
- 最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用33
- Office2010升级前期准备
- 使用网络进行项目托管
- 截网页全屏图的方法-截网页全屏软件-Web2Pic Pro
- 产品文档如何说清楚产品业务?关注这几点就够了
- 魅族Flyme5.x以上系统INSTALL_FAILED_SHARED_USER_INCOMPATIBLE