我们已经讨论过了 阵列 和 载体 .在这篇文章中,我们将讨论向量相对于普通阵列的优势。
null
矢量比阵列的优势 :
- 向量是 模板类 而且是 C++构造 而数组是 内置语言结构 C和C++都存在。
- 向量实现为 动态数组 具有 列表界面 而数组可以实现为 静态地或动态地 具有 基本数据类型 界面
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
array[100];
// Static Implementation
int
* arr =
new
int
[100];
// Dynamic Implementation
vector<
int
> v;
// Vector's Implementation
return
0;
}
- 阵列的大小是 固定的 而 向量是 可调整大小 i、 它们可以随着向量在堆内存上的分配而增长和收缩。
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
array[100];
// Static Implementation
cout <<
"Size of Array "
<<
sizeof
(array) /
sizeof
(array[0]) <<
""
;
vector<
int
> v;
// Vector's Implementation
// Inserting Values in Vector
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
cout <<
"Size of vector Before Removal="
<< v.size() <<
""
;
// Output Values of vector
for
(
auto
it : v)
cout << it <<
" "
;
v.erase(v.begin() + 2);
// Remove 3rd element
cout <<
"Size of vector After removal="
<< v.size() <<
""
;
// Output Values of vector
for
(
auto
it : v)
cout << it <<
" "
;
return
0;
}
输出 :
Size of Array 100 Size of vector Before Removal=5 1 2 3 4 5 Size of vector After removal=4 1 2 4 5
- 阵列 一定是 明确解除分配 如果动态定义,而向量是 自动取消分配 从堆内存。
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
* arr =
new
int
[100];
// Dynamic Implementation
delete
[] arr;
// array Explicitly deallocated
vector<
int
> v;
// Automatic deallocation when variable goes out of scope
return
0;
}
- 数组大小 无法确定 如果 动态分配 而向量的大小可以在 O(1)时间 .
- 将数组传递给函数时 还传递了大小的单独参数 然而,在将向量传递给函数的情况下,不需要 vector维护始终跟踪容器大小的变量 .
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
* arr =
new
int
[100];
// Dynamic Implementation
cout <<
"Size of array= "
;
cout <<
sizeof
(arr) /
sizeof
(*arr) <<
""
;
// Pointer cannot be used to get size of
// block pointed by it
return
0;
}
输出 :
Size of array= 2
- 当数组变满并插入新元素时; 不会隐式地进行重新分配 然而,当向量大于其容量时,重新分配是隐式进行的。
- 阵列 无法归还 除非动态分配 从函数 鉴于v 埃克托斯 可以退货吗 从函数 .
// Program to demonstrate arrays cannot be returned
#include <bits/stdc++.h>
using
namespace
std;
int
* getValues()
{
int
arr[10];
// Array defined locally
for
(
int
i = 0; i < 10; i++)
// Putting Values in array
arr[i] = i + 1;
return
arr;
// returning pointer to array
}
// main function
int
main()
{
int
* array;
// pointer of int type
array = getValues();
// Call function to get arr
for
(
int
i = 0; i < 10; i++) {
// Printing Values
cout <<
"*(array + "
<< i <<
") : "
;
cout << *(array + i) << endl;
}
return
0;
}
输出 :
warning: address of local variable 'arr' returned [-Wreturn-local-addr] Segmentation Fault (SIGSEGV)
// Program to demonstrate vector can be returned
#include <bits/stdc++.h>
using
namespace
std;
// Function returning vector
vector<
int
> getValues()
{
vector<
int
> v;
// Vector defined locally
for
(
int
i = 0; i < 10; i++)
// Inserting values in Vector
v.push_back(i + 1);
return
v;
// returning pointer to array
}
// main function
int
main()
{
vector<
int
> get;
get = getValues();
// Call function to get v
// Output Values of vector
for
(
auto
it : get)
cout << it <<
" "
;
return
0;
}
输出 :
1 2 3 4 5 6 7 8 9 10
- 数组不能直接复制或分配,而向量可以直接复制或分配。
#include <bits/stdc++.h>
using
namespace
std;
// main function
int
main()
{
vector<
int
> v;
// Vector defined locally
for
(
int
i = 0; i < 10; i++)
v.push_back(i + 1);
vector<
int
> get;
get = v;
// Copying vector v into vector get
cout <<
"vector get:"
;
for
(
auto
it : get)
cout << it <<
" "
;
int
arr[10];
for
(
int
i = 0; i < 10; i++)
// Putting Values in array
arr[i] = i + 1;
int
copyArr[10];
copyArr = arr;
// Error
return
0;
}
输出 :
vector get: 1 2 3 4 5 6 7 8 9 10 error: invalid array assignment copyArr=arr;
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END