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 ...
随机推荐
- 浏览器iscroll
::-webkit-scrollbar{width:4px;height:4px;background:transparent}::-webkit-scrollbar-track{background ...
- 做生活的有心人——xxx系统第一阶段总结
2017秋,桃子已经步入大学三年级了,觉得格外幸运 因为现在,有了学习的动力. 如果你和我一样也是在大学中后部分才意识到,自己是个大人了,思维模式开始转变开始融入一些前所未有的认知,觉得自己渺小得如沧 ...
- 关于ACtion类利用file类型取得上传文件的内容,名字和内容类型
上面的Action提供了两个属性:uploadFileName和uploadContentType,这个两个属性分别用于封转上传文件的文件名.上传文件的文件类型.Action类直接通过File类型属性 ...
- ActiveMQ在windows下启动失败解决方案
activemq.xml文件中的 <transportConnectors> <!-- DOS protection, limit concurrent connections to ...
- execution(* com.sample.service.impl..*.*(..))
execution(* com.sample.service.impl..*.*(..)) 解释如下: 符号 含义 execution() 表达式的主体: 第一个”*“符号 表示返回值的类型任意: c ...
- Java实现简单计算器、抽票程序
计算器: import java.awt.BorderLayout; import java.awt.Container; import java.awt.Font; import java.awt. ...
- HDU 3639 Hawk-and-Chicken (强连通缩点+DFS)
<题目链接> 题目大意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则:投票具有传递性,A支持B,B支持C,那么C获得2票(A.B共两 ...
- 一个小demo 实用selenium 抓取淘宝搜索页面内的产品内容
废话少说,上代码 #conding:utf-8 import re from selenium import webdriver from selenium.webdriver.common.by i ...
- 简单分析Java中审批业务流程业务原理
- Python datetime与timestamp之间的转换
# !!!! Python 2 datetime.datetime 对象没有timestamp方法! 在用Python处理datetime和timestamp的转换时发现在时区方面,Python的处理 ...