C++标准模板库vector的常用操作

一:介绍

vector是C++标准模板库,是一个容器,底层是数组,为连续内存。

命名空间为std,所属头文件为<vector>   注意:不是<vector.h>

vector存储数据时,会分配一个存储空间,如果继续存储,该分配的空间已满,就会分配一块更大的内存,把原来的数据复制过来,继续存储,这些性能也会一定程度上会有损耗

二:常用操作

容量:

  • a.vector大小:vector.size()
  • b.vector所占内存实际大小:vector.capacity()

修改:

  • a.尾部添加元素:vector.push_back()
  • b.尾部删除元素:vector.pop_back()
  • c.交换两个vector元素:vector.swap()
  • d.清空vector元素:vector.clear()
  • e.删除指定元素:vector.erase(it)

迭代器:

  • a.vector开始指针:vector.begin()
  • b.vector尾部指针:vector.end()   注:最后一个元素的下一个位置,类似为NULL,不是容器的最后一个元素

访问元素:

  • a.下标访问:vector[1]  //不检查是否越界
  • b.at方法访问:vector.at(1) //自动检查是否越界,如越界会抛出异常
  • c.访问第一个元素:vector.front()
  • d.访问最后一个元素:vector.back()

三:存储

简单存储

  //存储方式1
  vector<int> v1(10);
  for (int i=0; i<10; i++)
  {
    v1[i] = i;
  }
  //存储方式2
  vector<int> v2;
  for (int i=0; i<10; i++)
  {
    v2.push_back(i);
  }

存储结构体和结构体指针

  struct Student
  {
    char name[32];
    int age;
  };
  //存储结构体
  vector<Student> vStu1;
  for (int i=0; i<10; i++)
  {
    Student stu;
    strcpy(stu.name, "woniu201");
    stu.age = 30 + i;
    vStu1.push_back(stu);
  }
  //存储结构体指针
  vector<Student*> vStu2;
  for (int i=0; i<10; i++)
  {
    Student* pStu = (Student*)malloc(sizeof(Student));
    strcpy(pStu->name, "woniu201");
    pStu->age = 30 + i;
    vStu2.push_back(pStu);
  }

四:vector遍历

  vector<int> v;
  for (int i=0; i<100; i++)
  {
    v.push_back(i);
  }
  //遍历方式1
  for (int i=0; i<100; i++)
  {
    int& a = v[i];
    printf("%d ", a);
  }
  //遍历方式2
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    int&a = *it;
    printf("%d ", a);
  }

五:排序

对vector整形进行排序

#include "stdlib.h"
#include <vector>
#include <algorithm>
using namespace std;
//升序比较函数
int compare1(const int &a, const int &b)
{
  return a < b;
}
//降序比较函数
int compare2(const int &a, const int &b)
{
  return a > b;
}
int main()
{
  vector<int> v;
  for (int i=0; i<10; i++)
  {
    v.push_back(rand() % 10);
  }
  //遍历输出
  printf("排序前数据:");
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%d ", *it);
  }
  //升序排序
  sort(v.begin(), v.end(), compare1);
  //遍历输出
  printf("\n升序后数据:");
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%d ", *it);
  }
  //降序排序
  sort(v.begin(), v.end(), greater<int>());
  //遍历输出
  printf("\n降序后数据:");
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%d ", *it);
  }
  getchar();
  return 1;
}

对存放类成员变量排序

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
  Student(string n, int c) :name(n), core(c) {}
  string  name;
  int    core;
};
//升序比较函数
bool compare1(const Student& s1, const Student& s2)
{
  return s1.core < s2.core;
}
//降序比较函数
bool compare2(const Student& s1, const Student& s2)
{
  return s1.core > s2.core;
}
int main()
{
  vector<Student> v;
  Student s1("aaaa", 97);
  Student s2("bbbb", 99);
  Student s3("cccc", 95);
  v.push_back(s1);
  v.push_back(s2);
  v.push_back(s3);
  printf("排序前数据:\n");
  for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
  }
  //升序排序
  sort(v.begin(), v.end(), compare1);
  printf("\n升序后的数据:\n");
  for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
  }
  //降序排序
  sort(v.begin(), v.end(), compare2);
  printf("\n降序后的数据:\n");
  for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
  {
    printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
  }
  getchar();
  return 1;
}

六:查找

  vector<int>::iterator it = find(v.begin(), v.end(), 5);
  if(it != v.end())
  {
    cout << "found";
  }
  else
  {
    cout << "not found";
  }

七:删除

  for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
  {
    if(*it == 8)
    {
      it = v.erase(it);//it会++一次
      it--;    //删除完后需要--,否则最终循环越界
    }
  }

八:释放内存

存放整形vector释放

 //存放整型
 vector<int> v;
 for (int i=0; i<100; i++)
 {
 v.push_back(i);
 }
  //释放内存
  {
    vector<int> vEmpty;
    v.swap(vEmpty);
  }

存放结构体vector释放

 //存储结构体
 vector<Student> vStu1;
 for (int i=0; i<10; i++)
 {
 Student stu;
 strcpy(stu.name, "woniu201");
 stu.age = 30 + i;
 vStu1.push_back(stu);
 }
 //释放内存
    {
      vector<Student>
    }
 vector<Student> vEmpty;
     vStu1.swap(vEmpty);

存放结构体指针vector释放

 //存储结构体指针
 vector<Student*> vStu2;
 for (int i=0; i<10; i++)
 {
 Student* pStu = (Student*)malloc(sizeof(Student));
 strcpy(pStu->name, "wangpengfei");
 pStu->age = 30 + i;
 vStu2.push_back(pStu);
 }
 //释放内存
 for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++)
 {
 if (NULL != *it)
 {
  delete *it;
  *it = NULL;
 }
 }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

(0)

相关推荐

  • 详解Java中的Vector

    Vector实现了AbstractList抽象类和List接口,和ArrayList一样是基于Array存储的 Vector 是线程安全的,在大多数方法上存在synchronized关键字 //Vector存放的元素,初始化默认长度为10 protected Object[] elementData; //元素个数 protected int elementCount; //每次扩容大小,默认为0 protected int capacityIncrement; //构造函数,无指定初始化大小和

  • C++中stack、queue、vector的用法详解

    一.栈(stack) 引入头文件 #include<stack> 常用的方法 empty() 堆栈为空则返回真 pop() 移除栈顶元素 push() 在栈顶增加元素 size() 返回栈中元素数目 top() 返回栈顶元素 3.实例代码 #include<iostream> #include<stack> using namespace std; int main(){ //创建栈 s stack<int> s; //将元素压入栈 for(int i=0;

  • 浅谈c++ vector和map的遍历和删除对象

    示例如下: // Aa.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <vector> #include <map> #include <iostream> using namespace std; int main(int argc, char* argv[]) { printf("Hello World!\n

  • c++中stack、queue和vector的基本操作示例

    前言 这几天在接触搜索的题目,用bfs时基本都用到队列,就顺便学习了数据结构的栈.队列.本文将详细给大家介绍关于c++中stack.queue和vector的基本操作,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. stack 的基本操作有: 入栈,如例:s.push(x); 出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素. 访问栈顶,如例:s.top() 判断栈空,如例:s.empty() ,当栈空时,返回true. 访问栈中的元素个数,如例:s.

  • C++中vector可以作为map的键值实例代码

    因为项目中需要根据状态找到一个对应的结果,就采用了map的结构,但是状态本身较为复杂,存在一个vector中.上次使用map的经验是自定义类类型作为键值必须重载<操作符,因为map的快速查找是基于红黑树的构建,因而键值必须能相互之间比较.所以担心vector作为类类型的键值会引发一些错误,就写了一个例子测试.结果证明vector可以直接作为map的键值使用. #include<iostream> #include<string> #include<vector>

  • C++中vector和map的删除方法(推荐)

    1.连续内存序列容器(vector,string,deque) 序列容器的erase方法返回值是指向紧接在被删除元素之后的元素的有效迭代器,可以根据这个返回值来安全删除元素. vector<int> c; for(vector<int>::iterator it = c.begin(); it != c.end();) { if(need_delete()) it = c.erase(it); else ++it; } 2.关联容器(set,multiset,map,multima

  • 深入分析JAVA Vector和Stack的具体用法

    前面我们已经接触过几种数据结构了,有数组.链表.Hash表.红黑树(二叉查询树),今天再来看另外一种数据结构:栈. 什么是栈呢,我们先看一个例子:栈就相当于一个很窄的木桶,我们往木桶里放东西,往外拿东西时会发现,我们最开始放的东西在最底部,最先拿出来的是刚刚放进去的.所以,栈就是这么一种先进后出(FirstInLastOut,或者叫后进先出)的容器,它只有一个口,在这个口放入元素,也在这个口取出元素.那么我们接下来学习JDK中的栈. 一.Vector&Stack的基本介绍和使用 我们先看下JDK

  • C++ STL入门教程(1) vector向量容器使用方法

    一.简介 Vectors 包含着一系列连续存储的元素,其行为和数组类似. 访问Vector中的任意元素或从末尾添加元素都可以在O(1)内完成,而查找特定值的元素所处的位置或是在Vector中插入元素则是O(N). 二.完整程序代码 /*请务必运行以下程序后对照阅读*/ #include <vector> #include <iostream> #include <algorithm> #include <stdexcept> using namespace

  • C++ 中Vector常用基本操作

    标准库vector类型是C++中使用较多的一种类模板,vector类型相当于一种动态的容器,在vector中主要有一些基本的操作,下面通过本文给大家介绍,具体内容如下所示: (1)头文件#include<vector>. (2)创建vector对象,vector<int> vec; (3)尾部插入数字:vec.push_back(a); (4)使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的. (5)使用迭代器访问元素. vect

  • Java 中的vector和list的区别和使用实例详解

    要了解vector,list,deque.我们先来了解一下STL. STL是Standard Template Library的简称,中文名是标准模板库.从根本上说,STL是一些容器和算法的集合.STL可分为容器(containers).迭代器(iterators).空间配置器(allocator).配接器(adapters).算法(algorithms).仿函数(functors)六个部分.指针被封装成迭代器,这里vector,list就是所谓的容器. 我们常常在实现链表,栈,队列或者数组时,

随机推荐