Document directory
- Disadvantages of static Array
- Dynamic Array
- Usage of Vector in C ++ STL
- List usage in C #
Disadvantages of static Array
Array is a very large data structure. the first thing that comes to mind when you store a group of objects is to use it. its advantage is that it is allocated continuously in the memory, so the access through subscript index is very fast. the two methods are simple to use and convenient to modify value assignment.
In C ++, we generally define an array such as int numarr [] = {1, 2, 3}; // or Int numarr [3] = {1, 2, 3 };
In C #, int [] numarr = {1, 2, 3 };
The biggest disadvantage of static arrays is that the size is fixed during definition.(Explicitly specify the size, or the compiler calculates the specific size by assigning the number of elements ). in this way, it is troublesome to store a group of objects with unfixed sizes. A stupid way is to make an estimation in advance and then define an array of relatively large points. this can easily cause a waste of memory. in special cases, the stored data is still out of the array range.
Dynamic Array
A dynamic array is created for the disadvantage of fixed array size. in STL of C ++, it is called vector and in C #, it is called list. Both of them are useful in generic technology.
The main operations involved in dynamic arrays are assignment (ADD), insertion, query, deletion, traversal, and sorting.
Usage of Vector in C ++ STL
Reference header files
# Include <vector>
# Include <algorithm>
Using namespace STD;
Vector <int> VEC; // defines an int-type dynamic array.
VEC. push_back (1); // Add element 1 to the array. I don't know why I don't need to add an add element.
VEC. push_back (2 );
VEC. push_back (4 );
Vector <int>: iterator it = Vec. Begin (); // iterator iterators can be easily understood as pointers pointing to element positions. This indicates the starting position.
VEC. insert (IT + 2, 3); // insert 3 before the third element, that is, insert 3 before 4.
Int num = VEC [2]; // The value of num is 3
Num = Vec. At (2); // The value of num is 3.
//////////////// Three methods for traversing the array //////////////// ////////////////////////////////
For (vector <int>: size_type I = 0; I <Vec. Size (); I ++) // traverses the entire array, method 1
Cout <VEC [I] <Endl;
For (IT = Vec. Begin (); it! = Vec. End (); It ++) // traverses the entire array. method 2
Cout <* It <Endl;
For_each (VEC. Begin (), VEC. End (), show); // traverses the entire array. Method 3
// The function show must be defined as follows:
// Void show (const int num) {cout <num <Endl ;}
//////////////////////////////////////// //////////////////////////////////////// /////////
VEC. pop_back (); // Delete the last element of the array
It = Vec. Begin (); // The value must be assigned again because the previous iterator operation fails.
VEC. er (IT + 1); // Delete the specified location Element
VEC. Clear (); // delete all elements
Sort (VEC. Begin (), VEC. End (); // sort
List usage in C #
In fact, there is a dynamic array named arraylist in C #, but it does not use generic technology. The array can only store object types one by one. You need to perform some type conversion on your own.
Reference namespace
Using system. Collections. Generic
List <int> array = new list <int> ();
Array. Add (1); // Add an element
Array. Add (2 );
Array. Add (4 );
For (INT I = 0; I <array. Count; I ++) // traverses the entire array, method 1
Console. writeline (array [I]);
List <int>. enumerator it = array. getenumerator (); // traverses the entire array, method 2
While (it. movenext ())
Console. writeline (it. Current );
Foreach (INT item in array) // traverses the entire array, method 3
Console. writeline (item );
Array. insert (3, 3 );
Int num = array [2]; // The result is 3.
Array. removeat (2); // Delete the element whose index is 2
Array. Clear (); // clear all elements
Array. Sort (); // sort