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中,我们可以使用以下方式将数组元素复制到向量中

  1. 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);
    
    
  2. 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中将数组元素复制到向量?相关推荐

  1. java数组删除数组元素_如何在Java中删除数组元素

    java数组删除数组元素 When we create an array in Java, we specify its data type and size. This is used by JVM ...

  2. flutter - 如何在Dart/Flutter中将某些元素从一个Map复制到新Map中?

    flutter - 如何在Dart/Flutter中将某些元素从一个Map复制到新Map中? 如何在Dart / Flutter中将某些元素从一个Map复制到新Map中? Old_Map = {'A' ...

  3. c++ stl 获取最小值_如何在C ++ STL中找到向量的最小/最小元素?

    c++ stl 获取最小值 Given a vector and we have to minimum/smallest element using C++ STL program. 给定一个向量,我 ...

  4. stl标准模板库_如何在C ++ STL(标准模板库)中使用Pair

    stl标准模板库 In this article, we'll take a look at using pair in C++ Standard Template Library (STL). 在本 ...

  5. wps怎么关闭修改痕迹_如何在wps文字中将修订标记去掉

    竭诚为您提供优质文档 / 双击可除 如何在 wps 文字中将修订标记去掉 篇一:如何去掉 word 文档的标记修订和批注 如何去掉 word 文档的标记(含修订和批注) (即不显示已删除内容) 你想让 ...

  6. word中将空格替换为_如何在Office 2007中将Word保存为PDF

    word中将空格替换为 How to save a Word document to be a PDF in Office 2007? 如何在Office 2007中将Word 文档保存为PDF? I ...

  7. stl向量_如何检查C ++ STL中向量中是否存在元素?

    stl向量 Given a vector and an element to be searched in the vector. 给定一个向量和要在向量中搜索的元素. To check whethe ...

  8. 一维向量转换为n维向量_如何在N维上固定万向节锁

    一维向量转换为n维向量 Gimbal lock is a common issue that arises in 3D rotation systems. Conventional wisdom sa ...

  9. android pdf阅读器开发_如何在 Windows 10 中将 Firefox 设置为默认 PDF 阅读器

    PDF 作为办公一族中的必备文档,很多人都会编辑或者创建 PDF 文档,而在 Windows 系统中并没有默认的 PDF 阅读器.而在新版的 Microsoft Edge微软已在开始提供有真正的高级 ...

最新文章

  1. 【 MATLAB 】使用 residuez 函数求 z 反变换的几个案例分析
  2. 排查一般MySQL性能问题
  3. python用途与前景-Python 就业方面的选择与应用分析
  4. Django和uwsgi,配合nginx做静态缓存
  5. ubuntu通过xmange连接桌面
  6. [Cake] 0.C#Make自动化构建-简介
  7. Zabbix二次开发_03api列表
  8. UE3 光照 阴影
  9. 混合架构、暗数据...这些云原生安全 bug 稍不留神会带来灾难!
  10. 论文阅读2--《融合多因素的短时交通流预测研究》
  11. 使用redis做缓存处理时出现的问题
  12. mysql sql练习题_Mysql----sql语句练习题(一)
  13. quot 云计算 quot 是计算机,云计算是什么意思?
  14. ios java模拟器 2017_Visual Studio 2017(Xamarin)未显示iPhone模拟器列表
  15. 信息学奥赛一本通 1359:围成面积
  16. 华为手机灵敏度设置_华为调屏幕灵敏度设置方法
  17. 天气预报小程序的设计与实现
  18. 硬盘安装ubuntu 14.04 LTS
  19. 一年一度的食品饮料新消费盛会,FBNB2022首届新食力大会5月开幕
  20. 用爬虫收集网站公开数据丨Python爬虫实战系列(5)

热门文章

  1. Silverlight 异步单元测试
  2. 解决一次由于SSL证书到期导致的网站不能访问的问题(Nginx,php,Apache)
  3. 监听文本框数据修改,特别是微信等客户端直接选择粘贴修改
  4. linux 虚拟机添加网卡
  5. 最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用33
  6. Office2010升级前期准备
  7. 使用网络进行项目托管
  8. 截网页全屏图的方法-截网页全屏软件-Web2Pic Pro
  9. 产品文档如何说清楚产品业务?关注这几点就够了
  10. 魅族Flyme5.x以上系统INSTALL_FAILED_SHARED_USER_INCOMPATIBLE