C++ vector vs. array

本文详细对比了C++中Vector与Array的特性,包括动态与静态分配、大小确定性、内存管理、复制与赋值操作等。通过具体实例说明了Vector在灵活性和安全性上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 vectorarrayothers 
1template class (c++ only)built-in language construct (both c/c++) 
2dynamic arrays with list interface

statically or dynamically implemented with primitive data type

see ex.1
3resizablefixed sizesee ex.2
4automatically deallocated from heap memoryhave to be explicitly deallocatedsee ex.3
5size can be determined in O(1) size cannot be determined of dynamically defined 
6maintains variables keeping track of the size of container all the timeseperate parameter is also passed to the functionsee ex.4
7reallocation is done implicitly when vectors become larger than its capacityno reallocation is done implicitly 
8can be returned from a functioncannot be returned unless dynamically allocated from a functionsee ex.5 and 6
9can be copied or assigned directlycannot be copied or assigned directlysee ex.7

 

primitive data type is basic type or built-in type.

#include <bits/stdc++.h> 
using namespace std; 

/*ex.1*/
int main() 
{ 
	int array[100]; // Static Implementation 
	int* arr = new int[100]; // Dynamic Implementation 
	vector<int> v; // Vector's Implementation 
	return 0; 
} 
#include <bits/stdc++.h> 
using namespace std; 


/*ex.2*/
int main() 
{ 
	int array[100]; // Static Implementation 

	cout << "Size of Array " << sizeof(array) / sizeof(array[0]) << "\n"; 

	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() << "\n"; 

	// Output Values of vector 
	for (auto it : v) 
		cout << it << " "; 

	v.erase(v.begin() + 2); // Remove 3rd element 

	cout << "\nSize of vector After removal=" << v.size() << "\n"; 

	// Output Values of vector 
	for (auto it : v) 
		cout << it << " "; 

	return 0; 
} 
#include <bits/stdc++.h> 
using namespace std; 

/*ex.3*/
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; 
} 
#include <bits/stdc++.h> 
using namespace std; 

/*ex.4*/
int main() 
{ 
	int* arr = new int[100]; // Dynamic Implementation 

	cout << "Size of array= "; 
	cout << sizeof(arr) / sizeof(*arr) << "\n"; // Pointer cannot be used to get size of 
	// block pointed by it 
	return 0; 
} 
// Program to demonstrate arrays cannot be returned 
#include <bits/stdc++.h> 
using namespace std; 

/*ex.5*/
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; 
} 
// Program to demonstrate vector can be returned 
#include <bits/stdc++.h> 
using namespace std; 

/*ex.6*/
// 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; 
} 
#include <bits/stdc++.h> 
using namespace std; 

/*ex.7*/
// 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:\n"; 
	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; 
} 

revised from: https://www.geeksforgeeks.org/advantages-of-vector-over-array-in-c/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值