Initialize a vector in C++ (5 different ways)
https://www.geeksforgeeks.org/initialize-a-vector-in-cpp-different-ways/
Following are different ways to create and initialize a vector in C++ STL
Initializing by one by one pushing values :
// CPP program to create an empty vector// and one by one push values.#include <bits/stdc++.h>using namespace std;int main(){ // Create an empty vector vector<int> vect; vect.push_back(10); vect.push_back(20); vect.push_back(30); for (int x : vect) cout << x << " "; return 0;} |
Output:
10 20 30
Specifying size and initializing all values :
// CPP program to create an empty vector// and one by one push values.#include <bits/stdc++.h>using namespace std;int main(){ int n = 3; // Create a vector of size n with // all values as 10. vector<int> vect(n, 10); for (int x : vect) cout << x << " "; return 0;} |
Output:
10 10 10
Initializing like arrays :
// CPP program to initialize a vector like// array.#include <bits/stdc++.h>using namespace std;int main(){ vector<int> vect{ 10, 20, 30 }; for (int x : vect) cout << x << " "; return 0;} |
Output:
10 20 30
Initializing from array :
// CPP program to initialize a vector from// array.#include <bits/stdc++.h>using namespace std;int main(){ int arr[] = { 10, 20, 30 }; int n = sizeof(arr) / sizeof(arr[0]); vector<int> vect(arr, arr + n); for (int x : vect) cout << x << " "; return 0;} |
Output:
10 20 30
Initializing from another vector :
// CPP program to initialize a vector from// another vector.#include <bits/stdc++.h>using namespace std;int main(){ vector<int> vect1{ 10, 20, 30 }; vector<int> vect2(vect1.begin(), vect.end()); for (int x : vect2) cout << x << " "; return 0;} |
Output:
10 20 30
Initialize a vector in C++ (5 different ways)的更多相关文章
- vector的主要操作
vector常用方法 assign() 对Vector中的元素赋值 void assign( input_iterator start, input_iterator end ); // void a ...
- cocos2d::Vector
C++中的vector使用范例 一.概述 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector是一个容器,它能够存放各种类型的对象,简 ...
- C++之vector模板类
vector 称为容器模板类,是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.vector 不是一种数据类型,而只是一个类模板,可用来定义任意多种数据类型.vector 类型的每一种都指定 ...
- Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例
这个实例在windows.OS X.IOS和Android等平台运行正常.本文参考这个网站提供的方法:http://zarko-gajic.iz.hr/firemonkey-mobile-androi ...
- 课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第三周(Hyperparameter tuning, Batch Normalization and Programming Frameworks) —— 2.Programming assignments
Tensorflow Welcome to the Tensorflow Tutorial! In this notebook you will learn all the basics of Ten ...
- [C2P1] Andrew Ng - Machine Learning
About this Course Machine learning is the science of getting computers to act without being explicit ...
- Delphi使用Zxing创建二维码
效果 DelphiZXingQRCode下载地址:https://www.debenu.com/open-source/delphizxingqrcode/ 为了调用方便unit DelphiZXIn ...
- 改善深层神经网络-week3编程题(Tensorflow 实现手势识别 )
TensorFlow Tutorial Initialize variables Start your own session Train algorithms Implement a Neural ...
- Mersenne twister 随机数算法实现 in Scheme
这个实现基本上是从 Wiki 上的 Python 版翻译过来的,大量使用了赋值. ;; Mersenne twister algorithm from Wikipedia ;; returns a c ...
随机推荐
- BZOJ2119 股市的预测 字符串 SA ST表
原文链接https://www.cnblogs.com/zhouzhendong/p/9069171.html 题目传送门 - BZOJ2119 题意 给定一个股票连续$n$个时间点的价位,问有多少段 ...
- 阿里巴巴Java开发规范手册
Java开发手册 版本号 制定团队 更新日期 备 注 1.0.0 阿里巴巴集团技术部 2016.12.7 首次向Java业界公开 一.编程规约 (一) 命名规约 1. [强制]所有编程相关命 ...
- 044 SimpleDateFormat的线程安全问题与解决方案
这个问题,以前好像写过,不过现在这篇文章,有一个重现的过程,还是值得读一读的. URL:SimpleDateFormat的线程安全问题与解决方案
- 013 mysql中find_in_set()函数的使用
在工作中遇见过,对于新知识,在这里写一写文档. 1.作用 举个例子,也许不理解,在看完后面的SQL示例,再来看就明白了: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点 ...
- Python 动态加载并下载"梨视频"短视频
下载链接:http://www.pearvideo.com/category_1 import requests from lxml import etree import re from urlli ...
- StringBuffer 可变参数拼接
@RequestMapping(value="/getFundsCount",method=RequestMethod.POST) public Result getFundsCo ...
- linux同步Internet时间
输入ntpdate time.nist.gov同步网络时间 如果未安装:yum install ntpdate 结果:3 Jun 15:42:39 ntpdate[4721]: adjust time ...
- Shell学习之Bash变量详解(二)
Shell学习之Bash变量详解 目录 Bash变量 Bash变量注意点 用户自定义变量 环境变量 位置参数变量 预定义变量 Bash变量 用户自定义变量:在Bash中由用户定义的变量. 环境变量:这 ...
- checkbox jquery操作总结
$('input[name="myCheckbox"]').prop('checked','true'); // 全选 $('input[name="myCheckbox ...
- BZOJ.3926.[ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)
题目链接 要对多个串同时建立SAM,有两种方法: 1.将所有串拼起来,中间用分隔符隔开,插入字符正常插入即可. 2.在这些串的Trie上建SAM.实际上并不需要建Trie,还是只需要正常插入(因为本来 ...