C ++ primer Study Notes (4.1) array

Source: Internet
Author: User

Chapter 4 arrays and pointers


Modern C ++ programs should try to use vector and iterator types, instead of low-level arrays and pointers. Arrays and pointers use arrays and pointers within the class implementation only when speed is emphasized.

However, the C ++ program still uses arrays and pointers in large quantities for a long period of time, and the C ++ programmer must master it.


An array consists of the type name, identifier, and dimension. The dimension of the array must be a constant expression greater than or equal to one.

This constant expression includes a literal value constant, an enumerated constant, or an integer const object initialized using a constant expression. None of the non-const variables and the variables whose values are not known before running !! The variable is not allowed (the value is known only when the variable is running, no matter whether the value is assigned on the Code )!!

Array Initialization:

Const unsigned arrays_size = 3;

Int Ia [array_size] = {0, 1, 2}; // display Initialization

If Initialization is not displayed, the array is the same as the variable:

1. In the function body, the initialization is 0. In the function body, no Initialization is performed.

2. class type: Call the constructor. If there is no constructor, initialization must be displayed.

Int Ia [] = {0, 1, 2, 3]; // This can also be done. The dimension does not have to be written, and the compiler automatically determines

Const unsigned arrays_size = 5;

Int Ia [] = {, 2}; // then, Ia becomes}


Special Character Array

Char CA1 [] = {'C', '+', '+'};/no null dimension is 3

Char Cal [] = {"C ++"}; // null Terminator added automatically the dimension is 4

Char Cal [3] = {"C ++"}; // compilation Error

Directly copying or assigning values to arrays is not allowed, but some compilers allow this.

Exercise 4.5

Disadvantages of listing arrays instead of vectors:

Compared with the vector type, arrays have the following Disadvantages:

(1) The length of the array is fixed, and the array does not provide the size operation to obtain its capacity;

(2) The push_back operation for automatically adding elements is not provided;

Therefore, the programmer cannot know the length of a given array when running the program. If you need to change the length of an array, the programmer can create only one large enough array, copy all elements of the original array to the new array.

Compared with vector, arrays with built-in arrays are more error-prone and difficult to debug.


Array Operation:

Index Summary of subscript operations:

String uses string: size_type, and vector uses vector <int >:: size_type;

Bitset and array use size_t

The subscript is very different from the dimension. Although both are in [], the subscript can be a variable, but the dimension cannot.

You cannot assign values to arrays directly. arrays cannot be of the vector type, and can be copied by another vector type variable.

However, you can assign values to the Array Using the subscript operation:

# Include <iostream> using namespace STD; const size_t array_size = 10; int Ia [array_size]; int main () {for (size_t Index = 0; index <array_size; index ++) {Ia [Index] = index; // assign the value to each element of the program cout <Ia [Index] <"\ t ";} return 0 ;}

The programmer needs to check the lower mark value of the array by himself. If the value is out of the border, there will be a slow "overflow error ".

Exercise 4.7

// Write code to assign an array to another array # include <iostream> using namespace STD; int main () {const size_t array_size = 6; int ia1 [] = {0, 1, 2, 3, 4, 5}; int ia2 [array_size]; for (size_t Index = 0; index <array_size; index ++) {ia2 [Index] = ia1 [Index]} return 0 ;}

Implement with vector:

# Include <iostream> # include <vector> using namespace STD; int main () {vector <int> vec1 (10, 1 ); // initialize to 10 1 vectors <int> vec2 (vec1); For (vector <int >:: size_type Index = 0; index <vec1.size (); index ++) {cout <vec1 [Index] <"\ t";} return 0 ;}

Of course, you can also do this:

# Include <iostream> # include <vector> using namespace STD; int main () {vector <int> vec1 (10, 1 ); // initialize to 10 1 vectors <int> vec2 (vec1); For (vector <int >:: iterator iter = vec1.begin (); ITER <vec1.end (); ITER ++) {cout <* ITER <"\ t";} return 0 ;}

Exercise 4.8

// Determine whether two arrays are equal # include <iostream> using namespace STD; int main () {const size_t array_size = 5; int ia1 [array_size], ia2 [array_size]; size_t IX; cout <"enter" <array_size <"numbers for array1:"; for (IX = 0; ix! = Array_size; ++ IX) {CIN> ia1 [ix];} cout <"enter" <array_size <"numbers for array2 :"; for (IX = 0; ix! = Array_size; ++ IX) {CIN> ia2 [ix] ;}for (IX = 0; ix! = Array_size; ++ IX) {If (ia1 [ix]! = Ia2 [ix]) {cout <"they are different arrays! "<Endl; return 0 ;}} cout <" they are same arrays! "; Return 0 ;}

// Determine whether two vectors are equal # include <iostream> # include <vector> using namespace STD; int main () {vector <int> vec1, vec2; int val; cout <"enter a vector-1 to end" <Endl; CIN> val; while (Val! =-1) {vec1.push _ back (VAL); CIN> Val ;}cout <"enter another vector" <"-1 to end" <Endl; cin> val; while (Val! =-1) {vec2.push _ back (VAL); CIN> val;} If (vec2.size ()! = Vec1.size () {cout <"vec1 is not equal to vec2! "<Endl; return 0;} else if (vec1.size () = 0 & vec2.size () = 0) {cout <" vec1 is equal to vec2! "<Endl; return 0;} else if (vec1.size ()! = 0 & vec2.size () = 0) {cout <"vec1 is not equal to vec2! "<Endl; return 0;} else if (vec1.size () = 0 & vec2.size ()! = 0) {cout <"vec1 is not equal to vec2! "<Endl; return 0;} else {vector <int>: iterator iter1, iter2; iter1 = vec1.begin (); iter2 = vec2.begin (); For (; iter1! = Vec1.end (); iter1 ++, iter2 ++) {If (* iter1! = * Iter2) {cout <"vec1 is not equal to vec2! "<Endl; return 0 ;}}cout <" vec1 is equal to vec2! "<Endl; return 0 ;}

Exercise 4.9

Write a program to define an array with 10 int elements, with the position as the value

#include <iostream>#include<vector>using namespace std;int main(){       const unsigned array_size=10;int array1[array_size];    size_t index=0;    for(;index<=array_size;index++){    array1[index]=index;    }return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.