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 ...
随机推荐
- ghithub中PHPOffice/PHPWord的学习
1.概念:PHPWord是用纯PHP提供了一组类写入和从不同的文档格式的文件阅读库.PHPWord的当前版本支持微软的Office Open XML(OOXML或处理OpenXML),用于Office ...
- fillder--模拟弱网
##设置路径## Rules--Performemnts---Silamte Mordem Speeds 弱网原理 Rules—>Cutomize Rules打开CustomRules.js 文 ...
- 2018牛客网暑假ACM多校训练赛(第五场)H subseq 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-H.html 题目传送门 - https://www.no ...
- Practice| 类型转换| 逻辑运算
类型转换 class Pratice1{ public static void main(String[] args){ int a = 3; //float f = 4.5;//TypeChange ...
- Mybatis关联一对多映射不能查询出所有的数据的问题
在使用Mybatis进行一对多查询时,如果返回的是一个对象的话,可以发现将一对多的数据全都取出来了,但是这样的缺点是有很多值为null,我们更喜欢将返回值设为Map的形式,这样可以去除那些多余null ...
- TMS320DM642学习----第一篇(硬件连接)
DSP设备型号:SEED-DTK-VPM642(目前实验室用途:视频处理,图像处理方向,预计搭载目标跟踪以及云台防抖等算法) 官网链接:http://www.seeddsp.com/index.php ...
- 用Eclipse上传项目到github
1.安装EGit插件 点击菜单栏help->Eclipse Marketplace 2.配置Git 这里是配置相关账户信息 3.把项目提交到本地仓库 右键项目->Team->Shar ...
- Kafka 概念、单机搭建与使用
目录 Kafka 概念.单机搭建与使用 基本概念介绍 Topic Producer Consumer Kafka单机配置,一个Broker 环境: 配置zookeeper 配置Kafka 使用Kafk ...
- 003.DNS主从正反解析部署
一 实验环境 1.1 实验需求 配置正向解析bind 配置反向解析bind 配置辅助dns的bind 实现主辅dns之间的区域传送 1.2 环境规划 主dns:CentOS6.8-01 172.24. ...
- type__字符串