;". Its usage syntax is "vector a(10);", which means that 10 integer types are defined. A vector of elements, but no initial value is given, and its value is undefined."/> ;". Its usage syntax is "vector a(10);", which means that 10 integer types are defined. A vector of elements, but no initial value is given, and its value is undefined.">
HomeBackend DevelopmentC#.Net TutorialDetailed explanation of c++ vector usage

Detailed explanation of c++ vector usage

Detailed explanation of vector usage

vector is an important member of C STL. When using it, you need to include the header file :

#include<vector>;

1. Initialization of vector: There are five ways. Examples are as follows:

Recommended: "C Tutorial"

(1 ) vector a(10); //A vector of 10 integer elements is defined (the element type name is in angle brackets, which can be any legal data type), but no initial value is given, and its value is not sure.

(2) vector a(10,1); //A vector of 10 integer elements is defined, and the initial value of each element is given as 1

( 3) vector a(b); //Use b vector to create a vector, overall copy assignment

(4)vector a(b.begin(),b.begin 3 ); //The value of a is defined as the 0th to 2nd (total 3) elements in b

(5) int b[7]={1,2,3,4,5, 9,8};

        vector a(b,b 7); //Get the initial value from the array

2. Several important operations of the vector object, examples are as follows :

(1) a.assign(b.begin(), b.begin() 3); //b is a vector, assign the vector composed of 0~2 elements of b to a

(2)a.assign(4,2); //A only contains 4 elements, and each element is 2

(3)a.back(); //Return a The last element of

(4) a.front(); //Return the first element of a

(5)a[i]; //Return the i-th element of a Element, if and only if a[i] exists2013-12-07

(6)a.clear(); //Clear the elements in a

(7)a.empty (); //Determine whether a is empty. If it is empty, it returns true, if it is not empty, it returns false

(8) a.pop_back(); //Delete the last element of a vector

(9) a.erase(a.begin() 1,a.begin() 3); //Delete the 1st (from 0th) to the 2nd element in a, that is to say, delete Elements count from a.begin() 1 (including it) to a.begin() 3 (excluding it)

(10) a.push_back(5); //At the last one of a Insert an element after the vector, its value is 5

(11) a.insert(a.begin() 1,5); //The first element of a (counting from the 0th) Insert the value 5 at the position, for example, a is 1,2,3,4, and after inserting the element, it is 1,5,2,3,4

(12) a.insert(a.begin() 1, 3,5); //Insert 3 numbers at the position of the first element of a (counting from the 0th one), and their values ​​are all 5

(13) a.insert(a.begin () 1,b 3,b 6); //b is an array, insert the 3rd element to the 5th element of b (excluding b) at the position of the 1st element of a (counting from 0th) 6), if b is 1,2,3,4,5,9,8, after inserting the element, it will be 1,4,5,9,2,3,4,5,9,8

( 14) a.size(); //Return the number of elements in a;

(15) a.capacity(); //Return the total number of elements that a can accommodate in memory

(16) a.resize(10); //Adjust the number of existing elements of a to 10, delete more elements, fill in fewer elements, and the values ​​are random

(17) a. resize(10,2); //Adjust the number of existing elements of a to 10, delete more elements, and fill in less elements. The value is 2

(18) a.reserve(100); //Expand the capacity of a to 100, which means that the return value when testing a.capacity(); is now 100. This operation only makes sense when a large amount of data needs to be added to a, because This will avoid multiple memory capacity expansion operations (the computer will automatically expand when the capacity of a is insufficient, of course, this will inevitably reduce performance)

(19) a.swap(b); //b is a vector, and The elements in a and the elements in b are exchanged as a whole

(20) a==b; //b is a vector, and the comparison operations of vectors are !=,>=,,

3. Several ways to access vector sequentially. Examples are as follows:

(1) Add elements to vector a

 vector<int> a;
for(int i=0;i<10;i++)
a.push_back(i);

2. You can also Select elements from the array and add them to the vector

int a[6]={1,2,3,4,5,6};

vector b;

for(int i=1;i

b.push_back(a[i]);

3. You can also select elements from an existing vector Add

int a[6]={1,2,3,4,5,6};

vector b;

vector c(a,a 4);

for(vector::iterator it=c.begin();it

b .push_back(*it);

4. You can also read elements from the file and add

ifstream in("data.txt");
vector<int> a;
for(int i; in>>i)
    a.push_back(i);

5 to the vector. [Misunderstanding]

vector<int> a;
for(int i=0;i<10;i++)
    a[i]=i;
//这种做法以及类似的做法都是错误的。刚开始我也犯过这种错误,后来发现,下标只能用于获取已存在的元素,而现在的a[i]还是空的对象

(2) From Reading elements in the vector

1. Reading through subscripts

int a[6]={1,2,3,4,5,6};
vector<int> b(a,a+4);
for(int i=0;i<=b.size()-1;i++)
    cout<<b[i]<<" ";

2. Reading through traversers

int a[6]={1,2,3,4,5,6};
vector<int> b(a,a+4);
for(vector<int>::iterator it=b.begin();it!=b.end();it++)
    cout<<*it<<" ";

4. Several important algorithms, use The header file needs to be included:

#include<algorithm>

(1) sort(a.begin(),a.end()); //From a.begin() (including it) to a. The elements of end() (excluding it) are arranged from small to large

(2)reverse(a.begin(),a.end()); //For the elements in a from a.begin() (including it) to a.end() (excluding it) Invert, but not arrange. For example, the elements in a are 1,3,2,4. After inversion, they are 4,2,3,1

(3) copy(a.begin(),a.end( ),b.begin() 1); //Copy the elements in a from a.begin() (including it) to a.end() (excluding it) to b, from b.begin() Start copying at the position of 1 (including it), overwriting the original elements

(4) find(a.begin(),a.end(),10); //From a. in a. begin() (including it) searches for 10 in the element of a.end() (excluding it), and if it exists, returns its position in the vector

The above is the detailed content of Detailed explanation of c++ vector usage. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Windows 11 系统下的五款最佳免费 C++ 编译器推荐Windows 11 系统下的五款最佳免费 C++ 编译器推荐Apr 23, 2023 am 08:52 AM

C++是一种广泛使用的面向对象的计算机编程语言,它支持您与之交互的大多数应用程序和网站。你需要编译器和集成开发环境来开发C++应用程序,既然你在这里,我猜你正在寻找一个。我们将在本文中介绍一些适用于Windows11的C++编译器的主要推荐。许多审查的编译器将主要用于C++,但也有许多通用编译器您可能想尝试。MinGW可以在Windows11上运行吗?在本文中,我们没有将MinGW作为独立编译器进行讨论,但如果讨论了某些IDE中的功能,并且是DevC++编译器的首选

C++报错:变量未初始化,应该如何解决?C++报错:变量未初始化,应该如何解决?Aug 21, 2023 pm 10:01 PM

在C++程序开发中,当我们声明了一个变量但是没有对其进行初始化,就会出现“变量未初始化”的报错。这种报错经常会让人感到很困惑和无从下手,因为这种错误并不像其他常见的语法错误那样具体,也不会给出特定的代码行数或者错误类型。因此,下面我们将详细介绍变量未初始化的问题,以及如何解决这个报错。一、什么是变量未初始化错误?变量未初始化是指在程序中声明了一个变量但是没有

C++编译错误:未定义的引用,该怎么解决?C++编译错误:未定义的引用,该怎么解决?Aug 21, 2023 pm 08:52 PM

C++是一门广受欢迎的编程语言,但是在使用过程中,经常会出现“未定义的引用”这个编译错误,给程序的开发带来了诸多麻烦。本篇文章将从出错原因和解决方法两个方面,探讨“未定义的引用”错误的解决方法。一、出错原因C++编译器在编译一个源文件时,会将它分为两个阶段:编译阶段和链接阶段。编译阶段将源文件中的源码转换为汇编代码,而链接阶段将不同的源文件合并为一个可执行文

如何优化C++开发中的文件读写性能如何优化C++开发中的文件读写性能Aug 21, 2023 pm 10:13 PM

如何优化C++开发中的文件读写性能在C++开发过程中,文件的读写操作是常见的任务之一。然而,由于文件读写是磁盘IO操作,相对于内存IO操作来说会更为耗时。为了提高程序的性能,我们需要优化文件读写操作。本文将介绍一些常见的优化技巧和建议,帮助开发者在C++文件读写过程中提高性能。使用合适的文件读写方式在C++中,文件读写可以通过多种方式实现,如C风格的文件IO

C++编译错误:无法为类模板找到实例化,应该怎么解决?C++编译错误:无法为类模板找到实例化,应该怎么解决?Aug 21, 2023 pm 08:33 PM

C++是一门强大的编程语言,它支持使用类模板来实现代码的复用,提高开发效率。但是在使用类模板时,可能会遭遇编译错误,其中一个比较常见的错误是“无法为类模板找到实例化”(error:cannotfindinstantiationofclasstemplate)。本文将介绍这个问题的原因以及如何解决。问题描述在使用类模板时,有时会遇到以下错误信息:e

iostream头文件的作用是什么iostream头文件的作用是什么Mar 25, 2021 pm 03:45 PM

iostream头文件包含了操作输入输出流的方法,比如读取一个文件,以流的方式读取;其作用是:让初学者有一个方便的命令行输入输出试验环境。iostream的设计初衷是提供一个可扩展的类型安全的IO机制。

C++中的信号处理技巧C++中的信号处理技巧Aug 21, 2023 pm 10:01 PM

C++是一种流行的编程语言,它强大而灵活,适用于各种应用程序开发。在使用C++开发应用程序时,经常需要处理各种信号。本文将介绍C++中的信号处理技巧,以帮助开发人员更好地掌握这一方面。一、信号处理的基本概念信号是一种软件中断,用于通知应用程序内部或外部事件。当特定事件发生时,操作系统会向应用程序发送信号,应用程序可以选择忽略或响应此信号。在C++中,信号可以

c++数组怎么初始化c++数组怎么初始化Oct 15, 2021 pm 02:09 PM

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft