当前位置:网站首页>C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

C / C + + Programming Notes: array part! Understand the key knowledge of C language in three minutes

2021-01-23 21:35:15 An Sheng

C or C ++ The array in is a collection of items stored in contiguous memory locations , You can use the index of an array to randomly access elements . They are used to store elements of similar types , Because all elements must have the same data type . They can be used to store collections of raw data types , For example, any particular type of int,float,double,char etc. .

 

in addition , Use C or C ++ Can be derived from an array of data types , For example, structure , Pointer, etc . Here's a picturesque representation of an array .

 

Why do we need arrays ?

When there are fewer objects , You can use ordinary variables (v1,v2,v3,..), But if you want to store a large number of instances , It's hard to manage them with ordinary variables . The idea of arrays is to represent many instances in one variable .

C / C ++ Array declaration in :

 

Be careful : In the diagram above int a [3] = {[0…1] = 3};  since GCC 2.5 rise , That statement is out of date

We can declare arrays in many ways . By specifying its type and size , Initialize it or both .

Specify the size of the array by declaring

int arr1[10];

int n = 10;

int arr2[n];

Array declaration by initializing elements

int arr[] = { 10, 20, 30, 40 }

Array declaration by specifying size and initialization elements

int arr[6] = { 10, 20, 30, 40 }

 

C / C ++ The advantages of arrays in :

Random access to elements using array indexes .

Use fewer lines of code , Because it creates a single array of multiple elements .

Easy access to all elements .

Single cycle use , Traversing arrays becomes easy .

Sorting becomes easy , Because it can be done by writing fewer lines of code .

C / C ++ The disadvantages of arrays in :

Allows you to enter a fixed number of elements determined at the time of declaration . Unlike linked lists ,C Arrays in languages are not dynamic .

Insertion and deletion of elements can be expensive , Because you need to manage elements based on the new memory allocation .

About C / C ++ The fact of arrays in :

Accessing array elements :

Use integer index Accessing array elements . Array index from 0 Start , Until the size of the array minus 1.

 

 

example :

C Language :

 

C++:

 

No index

Cross border inspection : for example , stay C / C ++ There is no index out of bounds check in , The following programs can be compiled normally , But the runtime can produce unexpected output .  

C Language :

 

C++:

 

Elements are stored in contiguous memory locations

C Language :

 

C++:

 

Another way to traverse arrays

C ++:

 

Arrays and pointers

Arrays and pointers are two different things ( We can use sizeof Inspection ). The confusion occurs because the array name indicates the address of the first element , And arrays are always passed as pointers ( Even if we use square brackets ).

C ++ What is the vector in ?

C ++ The vector in is STL A class representing an array in . The advantage of vectors over regular arrays is , 

When we declare a vector , We don't need to pass size as an extra parameter , Vector support dynamic size ( We don't have to specify the size of the vector in the first place ). We can also adjust the size of the vector .

Vectors have many built-in functions , For example, deleting elements, etc .

In addition, if you want to better improve your programming ability , Learn from good examples C Language C++ Programming ! Overtaking in curve , One step at a time ! I may be able to help you here ~

C Language C++ Programming learning communication circle ,QQ Group 1090842465 Click to enter 】 WeChat official account :C Language programming learning base

Share ( Source code 、 Project practice video 、 Project notes , Introduction to Basics )

Welcome to change careers and learn programming partners , Using more information to learn and grow faster than thinking about it yourself !

Programming learning book sharing :

Programming learning video sharing :

版权声明
本文为[An Sheng]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123213441836w.html

随机推荐