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 ...
随机推荐
- BZOJ2527 [Poi2011]Meteors 整体二分 树状数组
原文链接http://www.cnblogs.com/zhouzhendong/p/8686460.html 题目传送门 - BZOJ2527 题意 有$n$个国家. 太空里有$m$个太空站排成一个圆 ...
- ConcurrentModificationException(并发修改异常)的解决
[异常解释] ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常.[产生的原因] 迭代器是依赖于集合而存在的,在判断成功后,集合 ...
- Java中用Scanner扫描控制台输入时的一个小问题
package com.hxl; import java.util.Scanner; public class Test { public static void main(String[] args ...
- python自带线程池
1. 注意: 导包是: from multiprocessing.pool import ThreadPool #线程池不在thrading中 2. 代码: from mutiprocessing.p ...
- INSERT 中ON DUPLICATE KEY UPDATE的使用
如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE.例如,如果列a被定义为UNIQUE,并 ...
- JAVA项目中常用的异常处理情况
1.数学运算异常( java.lang.arithmeticexception) 程序中出现了除以零这样的运算就会出这样的异常,对这种异常,大家就要好好检查一下自己程序中涉及到数学运算的地方,公式是不 ...
- java多线程之守护线程(Daemon)
https://blog.csdn.net/u010739551/article/details/51065923/
- antd + node.js + mongoose小总结
最近开发太忙,都没时间更新博客,想通过这篇博客总结一下相关经验,以备后续能用到: 一.antd 1.onChange of undefined问题:可能是页面中表单取了相同的名称,也可能是在遍历时表单 ...
- 前后端通过API交互
前两篇已经写好了后端接口,和前段项目环境也搭建好了 现在要通过接口把数据展示在页面上 先占位置写架子 创建一个头部组件和底部组件占位置 <template> <h1>这是头部组 ...
- VeeamBackup9.5-查看和添加备份设备
备份代理(Backup Proxies)也就是VB主体,是VEEAM的核心组件之一,所有的备份的建立和还原都需要备份代理来完成.VEEAM安装完成后,默认VeeamBackup备份服务器即为备份代理, ...