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)的更多相关文章

  1. vector的主要操作

    vector常用方法 assign() 对Vector中的元素赋值 void assign( input_iterator start, input_iterator end ); // void a ...

  2. cocos2d::Vector

    C++中的vector使用范例 一.概述 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector是一个容器,它能够存放各种类型的对象,简 ...

  3. C++之vector模板类

    vector 称为容器模板类,是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.vector 不是一种数据类型,而只是一个类模板,可用来定义任意多种数据类型.vector 类型的每一种都指定 ...

  4. Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例

    这个实例在windows.OS X.IOS和Android等平台运行正常.本文参考这个网站提供的方法:http://zarko-gajic.iz.hr/firemonkey-mobile-androi ...

  5. 课程二(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 ...

  6. [C2P1] Andrew Ng - Machine Learning

    About this Course Machine learning is the science of getting computers to act without being explicit ...

  7. Delphi使用Zxing创建二维码

    效果 DelphiZXingQRCode下载地址:https://www.debenu.com/open-source/delphizxingqrcode/ 为了调用方便unit DelphiZXIn ...

  8. 改善深层神经网络-week3编程题(Tensorflow 实现手势识别 )

    TensorFlow Tutorial Initialize variables Start your own session Train algorithms Implement a Neural ...

  9. Mersenne twister 随机数算法实现 in Scheme

    这个实现基本上是从 Wiki 上的 Python 版翻译过来的,大量使用了赋值. ;; Mersenne twister algorithm from Wikipedia ;; returns a c ...

随机推荐

  1. 20165235祁瑛 2018-3 《Java程序设计》第三周学习总结

    20165235祁瑛 2018-3 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象学习总结 类:java作为面向对象型语言具有三个特性:①封装性.②继承性.③多态性.jav ...

  2. 【JavaScript】underscore

    例: 'use strict'; _.map([1, 2, 3], (x) => x * x); // [1, 4, 9] No1: [every/some] 当集合的所有元素都满足条件时,_. ...

  3. 多个SDK控制管理

    需求:制作一个公共组件,可以实现多个SDK想用哪个用哪个,集中管理 组织方式: 架构形式 注意点: 1.sdk必须通过maven库来compile,因为jar会打到aar中:所以library和主mo ...

  4. Android Studio项目生成Jar包

    步骤: 1)在module的gradle文件中,将apply plugin:'com.android.application'改为apply plugin:'com.android.library' ...

  5. HDU 2841-Visible Trees 【容斥】

    <题目链接> 题目大意: 有一个农民,站在(0,0)点,从(1,1)点到(m,n)点每个点上有棵树,问这个农民能看到多少棵树.(如果多棵树在同一条直线上,那么他只能看到一颗) 解题分析: ...

  6. 实现简单的web框架

    实现简单的web框架 流程: 服务端启动---服务端等待请求---客户端访问---服务端响应请求 代码: from wsgiref.simple_server import make_server # ...

  7. 从零搭建 ES 搜索服务(四)拼音搜索

    一.前言 上篇介绍了 ES 的同义词搜索,使我们的搜索更强大了,然而这还远远不够,在实际使用中还可能希望搜索「fanqie」能将包含「番茄」的结果也罗列出来,这就涉及到拼音搜索了,本篇将介绍如何具体实 ...

  8. Navicat -- 远程连接问题

    有朋友可能会碰到使用Navicat for mysql 远程连接 mySql数据库会提示10061.1045错误或 2003-Can’t connect to MySQL on ’192.168.1. ...

  9. SQL——用FOR XML Path完成字符串的聚合

  10. JavaScript基础笔记(十四)最佳实践

    最佳实践 一)松散耦合 1.解耦HTML/JavaScript: 1)避免html种使用js 2)避免js种创建html 2.解耦CSS/JS 操作类 3.解耦应用逻辑和事件处理 以下是要牢记的应用和 ...