C++之vector

1、简介

Vector属于顺序容器,代表可改变大小的数组。

像数组一样,vector使用连续存储,意味着它们的元素可以使用偏移来访问。不同于数组,它们的规模可以动态改变。

Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (dequeslists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.

2、用法

(1)遍历时删除

vector<string> strArray;
    strArray.push_back("a");
    strArray.push_back("b");
    strArray.push_back("c");
    strArray.push_back("d");
    for (vector<string>::iterator it = strArray.begin(); it != strArray.end();)
    {
        if(*it=="b")
        {
            it = strArray.erase(it);
        }
        else
        {
            std::cout << ‘ ‘ << *it;
            it++;
        }
    }

  

时间: 2024-12-14 17:20:30

C++之vector的相关文章

vector 学习笔记

vector 使用练习: /**************************************** * File Name: vector.cpp * Author: sky0917 * Created Time: 2014年04月27日 11:07:33 ****************************************/ #include <iostream> #include <vector> using namespace std; int main

初译 Support Vector Machines:A Simple Tutorial(一)

从本次开始我将开始尝试着逐章翻译一下 Alexey Nefedov的<Support Vector Machines:A Simple Tutorial>这本教材,这可是我们导师极力推荐的SVM教材,看了好久一直感觉一脸懵逼,索性开坑翻译一下吧,也当是加深理解,毕竟我也是一知半解,如果翻译的有不对的地方还望大佬们斧正,欢迎提意见,欢迎讨论. 嗯,就是这样. (一)Introduction 在本章节中将会介绍一些用于定义支持向量机(SVM)的基础的概念,这些概念对于理解SVM至关重要,假定读者了

Vector容器 和 iteration 迭代器

vector容器 vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库负责管理存储元素的相关内存.我们把vector称为容器,是因为它可以包含其他对象.一个容器中的所有对象都必须是同一种类型的. 使用vector之前,必须包含相应的头文件.#include <vector> using std::vector; vector是一个类模板(class template).模板允许程序员编写单个类或函数定义,这个类和函数定义可用于不同的数据类型上.

C++ vector 使用

看vector 的使用: #include "stdafx.h" #include <iostream> #include <vector> using namespace std; int main() { vector<long> v; for (long i=0;i<10000;i++) { v.push_back(rand()); } cout << "size = " << v.size()

Vector Tile

Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE PREFIX = "[default] http://www.w3.org/2000/svg" NS = "http://www.w3.org/2000/svg" />License The text of this specification is licens

vector,deque,list相关操作

1.vector的基本操作 (1).对动态数组元素的添加和删除.获取 代码如下: #include<iostream> #include<vector> using namespace std; //数组元素的添加和删除.获取 int main(void){     vector<int> v1;      v1.push_back(1);     v1.push_back(2);     v1.push_back(3);     cout<<"l

vector容器用法详解

vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着vector对象元素个数的增大和缩小而自动变化. vector类常用的函数如下所示: 1.构造函数 vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize vector(int nSize,const t& t):创建一个vector,元

C++使用vector

#include <iostream> #include <string> #include <vector> using namespace std; void show(vector<int> v) { for(auto v1 : v){ cout << v1 << endl; } } void show2(vector<int> v) { for(vector<int>::const_iterator v

C++ STL学习——vector

学过C++的人肯定会很熟悉STL标准模板库,STL其实就是封装了一系列的接口,供我们调用.很多函数或者算法的实现不需要我们从头开始写,大大提高我们的编程效率.这篇博客在简单介绍STL的情况下,会详细的来介绍vector的使用. STL共有六大组件: 一.容器(Container):是一种数据结构,如list,vector,deque,queue等,以模板类的方法提供,为了访问容器中的数据,可以使用由容器类提供的迭代器. 二.迭代器(Iterator):提供了访问容器中对象的方法. 三.算法(Al

stl容器区别: vector list deque set map及底层实现

在STL中基本容器有: vector.list.deque.set.map set 和map都是无序的保存元素,只能通过它提供的接口对里面的元素进行访问 set :集合, 用来判断某一个元素是不是在一个组里面,使用的比较少 map :映射,相当于字典 ,把一个值映射成另一个值,如果想创建字典的话使用它好了 底层采用的是树型结构,多数使用平衡二叉树实现 ,查找某一值是常数时间,遍历起来效果也不错, 只是每次插入值的时候,会重新构成底层的平衡二叉树,效率有一定影响. vector.list.dequ