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 ...
随机推荐
- Unknown lifecycle phase "mvn"
Unknown lifecycle phase "mvn" maven执行命令错误 : 执行输入命令即可,不需要添加 mvn 此处不需要写mvn,而是执行写compile就行,否 ...
- 07. Matplotlib 3 |表格样式| 显示控制
1.表格样式创建 表格视觉样式:Dataframe.style → 返回pandas.Styler对象的属性,具有格式化和显示Dataframe的有用方法 样式创建:① Styler.applymap ...
- Mybatis if test 中int integer判断非空的坑
Mybatis 中,alarmType 是int类型.如果alarmType 为0的话,条件判断返回结果为false,其它值的话,返回true. 1 <if test="alarmTy ...
- 牛客练习赛A 【BFS】
<题目链接> 题目大意: 给出一张图,问你其中 ' # ' 加上那些不能够到达边界的 ' . ' 的点的个数,' # ' 会起阻挡作用. 解题分析: 本题很好做,无非就是将所有能够由边界上 ...
- 在ArchLinux中安装MySQL
最近前端学习用到数据库的知识.鉴于MySQL被甲骨文收购的情况,我从MariaDB开始学习.操作系统Manjaro 17.1, 数据库版本MariaDB 10.1. 1. 安装MariaDb和其客户端 ...
- NiftyNet开源平台使用
NiftyNet是一款开源的卷积神经网络平台,专门针对医学图像处理分析,上一篇博客已经详细介绍了这个平台,接下来让我简单介绍一下目前我了解到的使用方法.更详细的使用方法.以及配置过程请查看NiftyN ...
- MySQL5.6 大量SQL语句处于Writing to net状态的案例分析
[问题现象] 开发同事反馈有应用在21:00-22:00之间出现大量超时报错. [问题分析] 1. 从DB服务器慢查询指标来看,18:00后有大量的慢查询,累计产生了约9000条慢查询 2. 测试直接 ...
- SpringMVC(二五) JSTL View
项目中使用JSTL,SpringMVC会把视图由InternalView转换为JstlView. 若使用Jstl的fmt标签,需要在SpringMVC的配置文件中配置国际化资源文件. 实现过程: 1. ...
- 详解Spring中的ApplicationListener和ContextRefreshedEvent
ApplicationListener和ContextRefreshedEvent一般都是成对出现的.最近在面试中问到了被面试者对于这两个的用法,面试者大多数被问懵了.可见基础知识的掌握程度.基于此本 ...
- XamarinAndroid组件教程RecylerView适配器设置动画
XamarinAndroid组件教程RecylerView适配器设置动画 本小节将讲解动画相关设置,如动画的时长.插值器以及复合动画等. 1.设置动画时长 设置动画持续的时间可以使用Animation ...