STL_vector

简介: STL_vector

STL_vector

简介

vector属于动态数组,类比于Java里面的ArrayList,本文旨在为读者讲解最常用的vector的函数,与一些使用技巧,作者为算法工程师,所以比较偏向于算法题的写法。

常用方法

vector的定义

方法一
// 定义具有10个int类型元素的向量,不具备初始值
vector<int> v(10);
方法二
// 定义具有10个int类型的元素,初始全为1
vector<int>v(10, 1);
方法三
// 拷贝构造,将v0的内容拷贝构造到v中
vector<int>v(v0);
方法四
// 将向量v0中的0-5的元素赋值给v,两个向量需要类型相同
// 这里涉及到vector的一个方法begin(),对应的还有end(),begin()代表vector的第一个元素
// end()代表最后一个元素的后一位的指针
vector<int>v(v0.begin(), v.begin() + 5); // 一共5个元素
方法五
// 从数组中获取初始值
int v0[3] = {0, 1, 2};
vector<int> v(v0, v0 + 3);

vector的遍历

方式一

按照数组下标式遍历

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v(10, 1);
    for (int i = 0; i < v.size(); ++ i) // size()函数为获取容器的大小,每个容器都有的
    {
        cout << v[i] << " ";
    }
    // 运行结果:
    // 1 1 1 1 1 1 1 1 1 1 
    return 0;
}
方式二

auto遍历(新语法devc++可能需要调成c++11才能用)

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v(10, 1);
    for (auto x : v) // x为当前的值,v为容器
    {
        cout << x << " ";
    }
    // 运行结果:
    // 1 1 1 1 1 1 1 1 1 1 
    return 0;
}
方式三

迭代器遍历

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v(10, 1);
    // 不推荐 比较长 不适合搞算法题
    for (vector<int>::iterator it = v.begin(); it != v.end(); ++ it) 
    {
        // it为指针
        cout << *it << " ";
    }
    // 运行结果:
    // 1 1 1 1 1 1 1 1 1 1 
    return 0;
}

push_back()与pop_back()

void push_back(T x):给向量最后的位置加一个元素

void pop_back():弹出向量最后一个元素

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v(10, 1);
    // push_back()前 
    for(auto it : v)
    {
        cout << it << " ";
    }
    cout << endl;
    // 运行结果
    // 1 1 1 1 1 1 1 1 1 1 
    // push_back()后
    v.push_back(10);
    for(auto it : v)
    {
        cout << it << " ";
    }
    cout << endl;
    // 运行结果
    // 1 1 1 1 1 1 1 1 1 1 10 
    // pop_back()后
    v.pop_back();
    v.pop_back();
    for(auto it : v)
    {
        cout << it << " ";
    }
    // 运行结果
    // 1 1 1 1 1 1 1 1 1 
    return 0;
}

front()与back()

T front():获取容器中第一个元素

T back(): 获取容器中最后一个元素

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int v0[5] = {1, 2, 3, 4, 5};
    vector<int> v(v0, v0 + 5);
    cout << "第一个元素为:" << v.front() << endl;
    cout << "最后一个元素为:" << v.back() << endl;
    // 运行结果
    /*
    第一个元素为:1
    最后一个元素为:5
    */
    // vector支持下标访问 也可以这样
    cout << "第一个元素为:" << v[0] << endl;
    cout << "最后一个元素为:" << v[v.size() - 1] << endl;
    // 运行结果
    /*
    第一个元素为:1
    最后一个元素为:5
    */
    return 0;
}

排序sort()

默认排序
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int v0[9] = {1, 5, 1, 0, -1, -666, 8, 994, 456};
    vector<int> v(v0, v0 + 9);
    cout << "排序前:";
    for (auto x : v) cout << x << " ";
    cout << endl;
    // 运行结果:
    // 排序前:1 5 1 0 -1 -666 8 994 456 
    cout << "排序后:";
    sort(v.begin(), v.end()); // 可以控制范围,可以对v部分排序,我这里是对整个vector排序 默认升序排序
    for (auto x : v) cout << x << " ";
    cout << endl;
    // 运行结果:
    // 排序后:-666 -1 0 1 1 5 8 456 994 
    return 0;
}
自定义排序

这基本上是sort()的内容

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool compare(int a, int b) {
    return a > b;//降序排列
}
int main()
{
    int v0[9] = {1, 5, 1, 0, -1, -666, 8, 994, 456};
    vector<int> v(v0, v0 + 9);
    cout << "排序前:";
    for (auto x : v) cout << x << " ";
    cout << endl;
    // 运行结果:
    // 排序前:1 5 1 0 -1 -666 8 994 456 
    cout << "排序后:";
    // 可以控制范围,可以对v部分排序,我这里是对整个vector排序
    sort(v.begin(), v.end(), compare); 
    for (auto x : v) cout << x << " ";
    cout << endl;
    // 运行结果:
    // 排序后:994 456 8 5 1 1 0 -1 -666 
    return 0;
}

reverse()

翻转函数

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool compare(int a, int b) {
    return a > b;//降序排列
}
int main()
{
    int v0[9] = {1, 5, 1, 0, -1, -666, 8, 994, 456};
    vector<int> v(v0, v0 + 9);
    cout << "翻转前:";
    for (auto x : v) cout << x << " ";
    cout << endl;
    // 运行结果:
    // 翻转前:1 5 1 0 -1 -666 8 994 456 
    reverse(v.begin(),v.end());
    cout << "翻转前:";
    for (auto x : v) cout << x << " ";
    cout << endl;
    // 运行结果:
    // 翻转前:456 994 8 -666 -1 0 1 5 1 
    return 0;
}

find()

在vector的一段区间里面寻找一个元素,不包括右边界,如果找到了返回第一次出现的指针,没有找到返回```null``

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool compare(int a, int b) {
    return a > b;//降序排列
}
int main()
{
    int v0[9] = {1, 5, 1, 0, -1, -666, 8, 994, 456};
    vector<int> v(v0, v0 + 9);
    vector<int>::iterator it = find(v.begin(), v.end(), 100);
    cout << *it << endl;
    // 运行结果
    // 0 // 0代表空指针
    it = find(v.begin(), v.end(), 1);
    cout << *it << endl;
    // 运行结果
    // 1 // 1代表第一个位置的指针
    return 0;
}


相关文章
|
4月前
|
存储 C++ 索引
C++:STL - vector
C++:STL - vector
65 1
|
11月前
|
存储 C++ 容器
【C++】STL之vector操作
【C++】STL之vector操作
|
11月前
|
存储 算法 编译器
【剖析STL】vector
vector的介绍及使用 1.1 vector的介绍 cplusplus.com/reference/vector/vector/ vector是表示可变大小数组的序列容器。
35 0
|
存储 Linux C++
C++【STL】之vector的使用
C++ STL vector类常用接口详细讲解,干货满满!
108 0
C++【STL】之vector的使用
|
小程序 C++ 容器
C++ STL学习之【vector的使用】
vector 是表示可变大小数组的序列 容器,其使用的是一块 连续 的空间,因为是动态增长的数组,所以 vector 在空间不够时会扩容;vector 优点之一是支持 下标的随机访问,缺点也很明显,头插或中部插入效率很低,这和我们之前学过的 顺序表 性质很像,不过在结构设计上,两者是截然不同的
269 0
C++ STL学习之【vector的使用】
|
算法 C++ 容器
STL之vector
STL之vector
|
存储 C++ 容器
【C++】-- STL之vector详解(一)
【C++】-- STL之vector详解
131 0
【C++】-- STL之vector详解(一)
|
Linux 编译器 C++
【C++】-- STL之vector详解(二)
【C++】-- STL之vector详解
141 0
【C++】-- STL之vector详解(二)
|
算法 C++ 容器
STL初识 vector基础
STL初识 vector基础