C/C++ 或任何编程语言中的数组是存储在连续内存位置的类似数据项的集合,并且可以使用数组的索引随机访问元素。它们可用于存储原始数据类型的集合,例如任何特定类型的 int、float、double、char 等。此外,C/C++ 中的数组可以存储派生数据类型,例如结构、指针等。下面是数组的图片表示。
为什么我们需要数组?
当我们有少量对象时,我们可以使用普通变量(v1、v2、v3、..),但是如果我们要存储大量实例,使用普通变量管理它们就变得困难了。数组的想法是在一个变量中表示许多实例。
C/C++ 中的数组声明:
注意:在上图中 int a[3]={[0…1]=3}; 这种声明自 GCC 2.5 以来已经过时
我们可以通过多种方式声明数组。它可以通过指定它的类型和大小、初始化它或两者兼而有之来完成。
通过指定大小声明数组
通过初始化元素声明数组
通过指定大小和初始化元素来声明数组
C/C++ 中数组的优点:
输出
无索引
越界检查:C/C++ 中没有索引越界检查,例如,以下程序编译良好,但运行时可能会产生意外输出。
输出
警告:
输出
输出
// Array declaration by specifying size
int arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
// Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 }
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as
// 0. above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
- 使用数组索引随机访问元素。
- 使用更少的代码行,因为它创建了多个元素的单个数组。
- 轻松访问所有元素。
- 使用单个循环遍历数组变得很容易。
- 排序变得容易,因为它可以通过编写更少的代码来完成。
- 允许输入在声明时确定的固定数量的元素。与链表不同,C 中的数组不是动态的。
- 元素的插入和删除可能代价高昂,因为需要根据新的内存分配来管理元素。
- 访问数组元素: 使用整数索引访问数组元素。数组索引从 0 开始,直到数组大小减 1。
- 数组的名称也是指向数组第一个元素的指针。
#include <stdio.h>
int main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3 / 2] = 2; // this is same as arr[1] = 2
arr[3] = arr[0];
printf("%d %d %d %d", arr[0],
arr[1], arr[2], arr[3]);
return 0;
}
// This C program compiles fine
// as index out of bound
// is not checked in C.
#include <stdio.h>
int main()
{
int arr[2];
printf("%d ", arr[3]);
printf("%d ", arr[-2]);
return 0;
}
-449684907 4195777
在 C 中,初始化具有比指定大小更多的元素的数组不是编译器错误。例如,下面的程序编译得很好并且只显示警告。
#include <stdio.h>
int main()
{
// Array declaration by initializing it
// with more elements than specified size.
int arr[2] = { 10, 20, 30, 40, 50 };
return 0;
}
prog.c: In function 'main': prog.c:7:25: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 50 }; ^ prog.c:7:25: note: (near initialization for 'arr') prog.c:7:29: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 50 }; ^ prog.c:7:29: note: (near initialization for 'arr') prog.c:7:33: warning: excess elements in array initializer int arr[2] = { 10, 20, 30, 40, 50 }; ^ prog.c:7:33: note: (near initialization for 'arr')
- 注意:该程序不会在 C++ 中编译。如果我们将上述程序保存为 .cpp,程序会生成编译器错误“error: too many initializers for 'int [2]'”。
// C program to demonstrate that
// array elements are stored
// contiguous locations
#include <stdio.h>
int main()
{
// an array of 10 integers.
// If arr[0] is stored at
// address x, then arr[1] is
// stored at x + sizeof(int)
// arr[2] is stored at x +
// sizeof(int) + sizeof(int)
// and so on.
int arr[5], i;
printf("Size of integer in this compiler is %lun",
sizeof(int));
for (i = 0; i < 5; i++)
// The use of '&' before a variable name, yields
// address of variable.
printf("Address arr[%d] is %pn", i, &arr[i]);
return 0;
}
Size of integer in this compiler is 4 Address arr[0] is 0x7ffe75c32210 Address arr[1] is 0x7ffe75c32214 Address arr[2] is 0x7ffe75c32218 Address arr[3] is 0x7ffe75c3221c Address arr[4] is 0x7ffe75c32220遍历数组的另一种方法
#include<bits/stdc++.h>
using namespace std;
int main()
{
int arr[6]={11,12,13,14,15,16};
// Way -1
for(int i=0;i<6;i++)
cout<<arr[i]<<" ";
cout<<endl;
// Way 2
cout<<"By Other Method:"<<endl;
for(int i=0;i<6;i++)
cout<<i[arr]<<" ";
cout<<endl;
return 0;
}
11 12 13 14 15 16 By Other Method: 11 12 13 14 15 16数组与指针 数组和指针是两个不同的东西(我们可以通过应用 sizeof 来检查)。发生混淆是因为数组名称表示第一个元素的地址,并且数组总是作为指针传递(即使我们使用方括号)。 C++中的向量是什么? C++ 中的向量是 STL 中表示数组的类。向量相对于普通数组的优点是,
- 当我们声明向量时,我们不需要传递大小作为额外参数,即向量支持动态大小(我们不必最初指定向量的大小)。我们还可以调整向量的大小。
- 向量具有许多内置功能,例如删除元素等。