C++之vector学习记录

在用LeetCode编程时,想尝试用C++写,但参数类型都是vector什么的,与C语言很不一样,如下所示。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
    }
};

1 vector与数组

在使用vector关键字时,需要加上头文件#include <vector>。

并且C++里的头文件没有.h后缀。

为了认识vector,写了一个程序如下:

#include <iostream>
#include <vector>
using namespace std;

void test(vector<int>& a)
{
 cout << *a << endl;
}

void main()
{
 int b[1] = {0};
 test(b);
}

运行出错,如下所示。

错误1:

cout << *a << endl;  //error C2100: illegal indirection

首先test函数的形参变量a并不是一个指针变量,它还是一个容器。

并且不能使用*标识符对vector的成员变量进行访问,应改为a[0]。

错误2:

test(b);  //cannot convert parameter 1 from 'int [1]' to 'class std::vector<int,class std::allocator<int> > &'

这里主要将vector与数组完全等价了,对test函数的形参赋值,形参应该也是个vector,不能是数组。

修改后的程序如下:

#include <iostream>
#include <vector>
using namespace std;


void test(vector<int>& a)
{
    cout << a[0] << endl;
}

void main()
{
    vector<int> b(1);
    b[0] = 3;
    test(b);
}

2 &标识符的作用

在编程时,看到有的函数在形参类型后加了一个&标识符,如下所示:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
    }
};

为了测试&标识符的作用,测试程序如下所示,其中test1函数没有加&标识符,test2函数加了&标识符。

#include <iostream>
#include <vector>
using namespace std;

void test1(vector<int> a)
{
    a[0] = 4;

}
void test2(vector<int>& a)
{
    a[0] = 4;
}

void main()
{
    vector<int> b(3);
    vector<int> c(3);
    b[0] = 3;
    c[0] = 3;
    test1(b);
    test2(c);
    cout << "test1: "<< b[0] << endl;
    cout << "test2: "<< c[0] << endl;
}

运行结果如下:

test1: 3
test2: 4
Press any key to continue

可以发现加了&标识符后,传入的形参在函数外发生了变化;

而没有加&标识符的话,传入的形参在函数外不会发生变化。

其中加&标识符称为引用传参;不加&标识符称为值传参

对于值传参,在函数内,相当于新开辟了一块内存,以形参进行赋值,函数执行完后,内存又释放了,对形参的值没有什么影响。

而对于引用传参,在函数内,相当于对形参所在的内存直接处理,函数执行完后,形参直接就改变了。

可以看出使用值传递的话,对于形参数据比较多的情况下,比较浪费内存空间。

而对于引用传递,则比较节省内存空间,但容易误操作。

3 vector选取二维数组的行与列

row = matrix.size();//行数
column = matrix[0].size();//列数

4 判断二维数组是否为空

if(matrix.empty() || matrix[0].empty()) return false;

若没有第二个判断条件,[[]]这种测试用例无法通过。

5 如何在容器中存数据

存数据用push_back()函数,用a[]这种赋值语句会报错。

在读数据的时候可以用a[]来取值。

而且不用赋‘’来结束数组,与数组有些不一样,在定义时也不用定义容器的大小。

 1     vector<int> a;
 2     vector<int> b;
 3     int count = 0;
 4     int i = 0;
 5     if(head == NULL)
 6     {
 7         // b.push_back('');
 8         return b;
 9     }
10         
11     while(head != NULL)
12     {
13         a.push_back(head->val);
14         count++;
15         head = head->next;
16     }
17     // a[count] = '';
18     for( i = 0; i < count; i++)
19     {
20         // b[i] = a[count - i - 1];
21         b.push_back(a[count - i - 1]);
22     }
23     // b[i] = '';
24     return b;
25     }

6 push_back()函数与pop_back()函数

push_back()函数用来给容器添加数据,如下列程序所示:

1 vector<int> stack1, stack2;
2 stack2.push_back(stack1[point1]);

pop_back()函数用来删除容器的尾部数据,如下列程序所示:

1 stack2.pop_back();

7 引用vector数据

在引用vector数据前,一定要为vector分配空间。

8 vector初始化

括号内左边的参数是个数,右边的参数是要赋的值。

vector<vector<bool>> flag;
flag = vector<vector<bool>>(board.size(), vector<bool>(board[0].size(), false));

9 求vector最大值和最小值

#include <algorithm>    //需要包含的头文件
min_element()与max_element()函数是返回的指针
int min_value = *min_element(A.begin(), A.end());//求最小值
int max_value = *max_element(A.begin(), A.end());//求最大值

10 如何添加二维vector

vector<vector<int>> V;
vector<int> temp;
temp.push_back(head -> val);
V.push_back(temp);

11 在vector中插入一个元素

V.insert(V.begin() + j, V[k - 1]);

在j前插入一个元素。

如果j == 0,就是在第一个元素前插入一个元素,而不是在其后插入一个元素。

12 定义二维vector

1 vector<bool>  A(n, true);
2 vector<vector<bool> > B(n + 1, A);

13 sizeof(B)

得到的值并不是B中元素的数量。

参考

引用形式传参,int &a,vector<int> &a,const vector<int> &a

https://blog.csdn.net/weixin_41353540/article/details/79923986

C++数组或vector求最大值最小值

https://www.cnblogs.com/Tang-tangt/p/9352093.html

C++ std::vector指定位置插入

https://www.cnblogs.com/kevinWu7/p/10163530.html

原文地址:https://www.cnblogs.com/QQ2962269558/p/12931880.html