热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c++用数组初始化向量_用C++初始化向量

c用数组初始化向量Inthisarticle,we’lltakealookatsomeofthewaystoinitializeavectorinC.Thereareabuncho

c++用数组初始化向量

In this article, we’ll take a look at some of the ways to initialize a vector in C++. There are a bunch of ways to do this, so we’ll go through each approach.

在本文中,我们将介绍一些在C ++中初始化向量的方法。 有很多方法可以做到这一点,因此我们将介绍每种方法。

Let’s get started!

让我们开始吧!



方法1 :(推荐):使用初始化列表(C ++ 11及更高版本) (Method 1: (Recommended): Use an Initializer List (C++11 and above))

If your compiler supports the C++ version above C++11, you can simply initialize the vector using the {} notation.

如果您的编译器支持C ++ 11以上的C ++版本,则可以简单地使用{}表示法初始化向量 。

Since std::vector is a class, to initialize an object of this class using the above fashion, this refers to an Initializer List in C++.

由于std :: vector是一个类,因此要使用上述方式初始化该类的对象,因此它指的是C ++中的Initializer List 。

Here is an example using the initializer list declaration:

这是使用初始化列表声明的示例:


std::vector vec = { 1, 2, 3, 4, 5 };

Since initializer lists were introduced only in C++11, you need a minimum compiler version supporting at least C++11 to use this method.

由于初始化程序列表仅在C ++ 11中引入,因此您需要至少支持C ++ 11的最低编译器版本才能使用此方法。

Here is an example program to demonstrate this type of initialization:

这是一个示例程序来演示这种类型的初始化:


#include
#include int main() {// Initialize a vector using Initializer Listsstd::vector vec &#61; { 1, 2, 3, 4, 5 };// Use a range based for loop to print elements of the vectorfor (const auto &i: vec) {// Using access by reference to avoid copying// Using const since we&#39;re not modifying elements of the vectorstd::cout <}

Here, we use a range-based for-loop to print the vector elements.

在这里&#xff0c;我们使用基于范围的for循环来打印矢量元素。

Output

输出量


1
2
3
4
5

方法2&#xff1a;借助数组&#xff08;C &#43;&#43; 0x&#xff09;在C &#43;&#43;中初始化Vector (Method 2: Initialize a Vector in C&#43;&#43; with the help of an array (C&#43;&#43;0x))

If you’re using a C&#43;&#43;0x (C&#43;&#43;03, C&#43;&#43;07, etc) based compiler, you can still achieve the initialization using arrays.

如果您使用的是基于C &#43;&#43; 0x &#xff08;C &#43;&#43; 03&#xff0c;C &#43;&#43; 07等&#xff09;的编译器&#xff0c;则仍可以使用数组实现初始化。


int tmp[] &#61; { 1, 2, 3, 4, 5 };
std::vector vec( tmp, tmp &#43; sizeof(tmp)/sizeof(tmp[0]) );

Test this on a C&#43;&#43;0x based compiler, to verify that it works.

在基于C &#43;&#43; 0x的编译器上进行测试&#xff0c;以验证其是否有效。


#include
#include using namespace std;int main() {// Initialize a vector using Arraysint tmp[] &#61; { 1, 2, 3, 4, 5 };std::vector vec(tmp, tmp &#43; sizeof(tmp)/sizeof(tmp[0]));// Range based for loops not supported in older compilers!// Nor is auto!for (std::vector::iterator it &#61; vec.begin(); it !&#61; vec.end(); &#43;&#43;it) {std::cout <<*it <}

If you’re on Linux (g&#43;&#43; based compiler), compile and run using:

如果您使用的是Linux&#xff08;基于g &#43;&#43;的编译器&#xff09;&#xff0c;请使用以下命令进行编译和运行&#xff1a;


gcc -o test.out test.cpp -std&#61;c&#43;&#43;0x
./test.out

Output

输出量


1
2
3
4
5

方法3&#xff1a;使用库&#xff08;C &#43;&#43; 11及更高版本&#xff09; (Method 3: Using the library (C&#43;&#43;11 and above))

We can also use the library to achieve this.

我们还可以使用库来实现此目的。

The namespace has the list_of construct to initialize vectors.

名称空间具有用于初始化向量的list_of构造。


#include // Initialize a vector of {1, 2, 3, 4, 5}
std::vector vec &#61; boost::assign::list_of(1)(2)(3)(4)(5);

We can also use the overloaded &#43; operator to declare a vector and add elements.

我们还可以使用重载的&#43;运算符声明一个向量并添加元素。


// Courtesy: https://stackoverflow.com/a/2236233#include // Declare a vector
std::vector vec;// Add elements to it
vec &#43;&#61; 1, 2, 3, 4, 5;

However, this type of operator overloading is not advisable, as it can be confusing for many readers, as a person reading this code may also think that the first element will add to 1, the second element adding to 2, etc.

但是&#xff0c;这种类型的运算符重载是不可取的 &#xff0c;因为它可能会使许多读者感到困惑&#xff0c;因为阅读此代码的人可能还会认为第一个元素将加1&#xff0c;第二个元素加2&#xff0c;依此类推。



结论 (Conclusion)

In this article, we learned how we could initialize a vector in the C&#43;&#43; language in different ways.

在本文中&#xff0c;我们学习了如何以不同的方式用C &#43;&#43;语言初始化向量。



参考资料 (References)

  • StackOverflow Question on initializing vectors

    关于初始化向量的StackOverflow问题
  • JournalDev Article on Initializer Lists

    JournalDev关于初始化程序列表的文章


翻译自: https://www.journaldev.com/37575/initialize-a-vector-in-c-plus-plus

c&#43;&#43;用数组初始化向量



推荐阅读
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • Java中包装类的设计原因以及操作方法
    本文主要介绍了Java中设计包装类的原因以及操作方法。在Java中,除了对象类型,还有八大基本类型,为了将基本类型转换成对象,Java引入了包装类。文章通过介绍包装类的定义和实现,解答了为什么需要包装类的问题,并提供了简单易用的操作方法。通过本文的学习,读者可以更好地理解和应用Java中的包装类。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文探讨了C语言中指针的应用与价值,指针在C语言中具有灵活性和可变性,通过指针可以操作系统内存和控制外部I/O端口。文章介绍了指针变量和指针的指向变量的含义和用法,以及判断变量数据类型和指向变量或成员变量的类型的方法。还讨论了指针访问数组元素和下标法数组元素的等价关系,以及指针作为函数参数可以改变主调函数变量的值的特点。此外,文章还提到了指针在动态存储分配、链表创建和相关操作中的应用,以及类成员指针与外部变量的区分方法。通过本文的阐述,读者可以更好地理解和应用C语言中的指针。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
author-avatar
小杰01234
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有