STL vector的使用(一)基础

一. vector介绍:

vector是C++标准模板库中的部分内容。它是一个多功能的,可以操作多种数据结构和算法的模板类和函数库。vector之所以被觉得是一个容器,是由于它可以像容器一样存放各种类型的对象,简单地说。vector是一个可以存放随意类型的动态数组,可以添加和压缩数据。

二. 使用介绍:

1. 为了能够使用vector。必须在你的头文件里包括以下的代码:

#include <vector>

2. vector属于std命名域的。因此须要通过命名限定,例如以下完毕你的代码:

using std::vector;
vector<int> vInts;

或者连在一起。使用全名:

std::vector<int> vInts;

三. Vector成员函数(Vector Member Functions):

Function Description
assign Erases a vector and copies the specified
elements to the empty vector.

1. c.assign(beg,end):[beg; end)区间中的数据赋值给c

2. c.assign(n,elem):将n个elem的拷贝赋值给c。

at Returns a reference to the element at a specified location in the vector.

1. c.at(idx):传回索引idx所指的数据。假设idx越界,抛出out_of_range。

back Returns a reference to the last element of the vector.

传回最后一个数据,不检查这个数据是否存在。

begin
Returns a random-access iterator to the first element in the container.

传回迭代器的第一个数据。

capacity Returns the number of elements that the vector could
contain without allocating more storage.

返回容器中数据个数。

clear Erases the elements of the vector.

移除容器中全部数据。

empty Tests if the vector container is empty.

推断容器是否为空。

end Returns a random-access iterator that points just beyond the end of thevector.

指向迭代器中的最后一个数据地址。

erase Removes an element or a range of elements in a vector from
specified positions.

1. c.erase(pos):  删除pos位置的数据。传回下一个数据的位置。

2. c.erase(beg,end):删除[beg,end)区间的数据,传回下一个数据的位置。

front Returns a reference to the first element in a vector.

传回第一个数据。

get_allocator Returns an object to the allocator class used by a vector.

使用构造函数返回一个拷贝。

insert Inserts an element or a number of elements into the vector at
a specified position.

1. c.insert(pos,elem):在pos位置插入一个elem拷贝。传回新数据位置。

2. c.insert(pos,n,elem):在pos位置插入n个elem数据。

无返回值。

3. c.insert(pos,beg,end):在pos位置插入在[beg,end)区间的数据。

无返回值。

max_size Returns the maximum length of the vector.

返回容器中最大数据的数量。

pop_back Deletes the element at the end of the vector.

删除最后一个数据。

push_back Adds an element to the end of the vector.

在尾部增加一个数据

rbegin Returns an iterator to the first element in a reversed vector.

传回一个逆向队列的第一个数据。

rend
Returns an iterator to the end of a reversed vector.

传回一个逆向队列的最后一个数据的下一个位置。

resize Specifies a new size for a vector.

又一次指定队列的长度。

reserve Reserves a minimum length of storage for a vector object.

保留适当的容量。

size Returns the number of elements in the vector.

返回容器中实际数据的个数。

swap Exchanges the elements of two vectors.

1. c1.swap(c2)

2. swap(c1,c2)

都是将c1和c2元素互换。

vector Constructs a vector of a specific size
or with elements of a specific value or with a specific allocator or as a copy of some other vector.

四.
Vector操作符(Vector Operators):

Operator Description

operator[] Returns a reference to the vector element
at a specified position.

五. 构建Vector(Constructing a Vector)

1. Construct an empty vector to hold objects of type Widget:

vector<Widget> vWidgets;
//     ------
//      |
//      |- Since vector is a container, its member functions
//         operate on iterators and the container itself so
//         it can hold objects of any type.

2. Construct a vector to hold 500 Widgets:

vector<Widget> vWidgets(500);

3. Construct a vector to hold 500 Widgets initialized to 0:

vector<Widget> vWidgets(500, Widget(0));

4. Construct a vector of Widgets from another vector of Widgets:

vector<Widget> vWidgetsFromAnother(vWidgets);

六. 向vector加入数据:

for(int i= 0;i<10; i++)
    vWidgets.push_back(Widget(i));

七. 获取元素的个数:

非常多时候我们不必要知道vector里面有多少数据,vector里面的数据是动态分配的,使用push_back()的一系列分配空间经常决定于文件或一些数据源。

假设你想知道vector存放了多少数据。你能够使用empty()。获取vector的大小,能够使用size()。比如,假设你想获取一个vector v的大小,但不知道它是否为空,或者已经包括了数据,假设为空想设置为-1,你能够使用以下的代码实现:

int nSize = v.empty() ? -1 : static_cast<int>(v.size());

八. 訪问vector中的数据

1 vector::at()
2 vector::operator[]
vector<int> v;
v.reserve(10);

for(int i=0; i<7; i++)
    v.push_back(i);

try
{
 int iVal1 = v[7];  // not bounds checked - will not throw
 int iVal2 = v.at(7); // bounds checked - will throw if out of range
}
catch(const exception& e)
{
 cout << e.what();
}

时间: 2024-08-01 00:09:49

STL vector的使用(一)基础的相关文章

STL vector用法介绍

介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象

C++ stl vector介绍

转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是

STL vector

author:Donald-hu    theme:STL vector 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.v

STL vector使用方法介绍

介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该可以有效地使用vector容器,并且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,可以操作多种数据结构和算法的模板类和函数库.vector之所以被觉得是一个容器,是由于它可以像容器一样存放各种类型的对象

STL vector用法介绍(转)

介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象

STL vector中的rbegin方法(5)

public member function <vector> std::vector::rbegin C++98 C++11 reverse_iterator rbegin() noexcept; const_reverse_iterator rbegin() const noexcept; Return reverse iterator to reverse beginning 返回一个反向的首元素. 例子: #include <iostream> #include <v

C++ STL vector容器学习

STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector,list, deque, set, map等),算法完成特定任务,迭代器用来遍历容器对象,扮演容器和算法之间的胶合剂. 模板类vector 在计算中,矢量(vector)对应数组,它的数据安排以及操作方式,与array非常类似.在C++中,使用vector模板类时,需要头文件包含#include<v

STL ——vector 学习

STL简介 C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 stacks(栈)等.  C++ STL 提供给程序员以下三类数据结构的实现: 标准容器类   顺序性容器  vector 从后面快速的插入与删除,直接访问任何元素  deque 从前面或后面快速的插入与删除,直接访问任何元素 list 双链表,从任何地方快速插入与删除   关联容器  

STL vector的介绍(1)

尝试下翻译STL里面的一些容易和算法.四级过了,六级刚考.顺便练练自己的英语水平,翻译的不好的地方请大神多多指教哈,方便我改正. 原来均来自:http://www.cplusplus.com/ template < class T, class Alloc = allocator<T> > class vector; // generic template Vector Vectors are sequence containers representing arrays that