1. assign an array value to another array and rewrite the code to a vector.
# Include <iostream> using namespace STD; int main () {size_t array_size = 7; int ia1 [] = {0, 1, 2, 3, 4, 5, 6}; int ia2 [array_size]; // copy the array in C ++. You can only create a new array of sufficient size, and then copy for (size_t IX = 0; ix! = Array_size; ix ++) {ia2 [ix] = ia1 [ix]; cout <ia2 [ix] <";}return 0 ;}
Vector implementation
# Include <iostream> # include <vector> using namespace STD; int main () {vector <int> ivec1 (); vector <int> ivec2; // use the iterator to access elements in the vector; For (vector <int>: iterator iter = ivec1.begin (); iter! = Ivec1.end (); ++ ITER) {ivec2.push _ back (* ITER); cout <* ITER <"" ;}return 0 ;}
2. Write a program to determine whether two arrays are equal, and then write a similar program to compare two vectors.
Two Arrays:
# Include <iostream> # include <vector> using namespace STD; int main () {const int arr_size = 6; size_t IX; int ia1 [arr_size], ia2 [arr_size]; cout <"Enter the value of array 1" <Endl; For (IX = 0; ix! = Arr_size; ix ++) CIN> ia1 [ix]; cout <"Enter the value of array 2" <Endl; For (IX = 0; ix! = Arr_size; ix ++) CIN> ia2 [ix]; for (IX = 0; ix! = Arr_size; ix ++) {If (ia1 [ix]! = Ia2 [ix]) {cout <"array 1 and array 2 are not equal" <Endl; return 0 ;}} cout <"array 1 and array 2 are equal" <Endl; return 0 ;}
Two vectors:
# Include <iostream> # include <vector> using namespace STD; int main () {vector <int> ivec1, ivec2; int ival; // read the cout value of two vector elements <"Enter the value of vec1 and end with-1:" <Endl; CIN> ival; while (ival! =-1) {ivec1.push _ back (ival); CIN> ival;} cout <"Enter the value of vec2, end with-1:" <Endl; cin> ival; while (ival! =-1) {ivec2.push _ back (ival); CIN> ival;} vector <int >:: iterator iter1 = ivec1.begin (); vector <int> :: iterator iter2 = ivec2.begin (); // determines whether two vector elements are equal if (ivec1.size ()! = Ivec2.size () // The length is not equal to {cout <"vec1 is not equal to vec2" <Endl;} else if (ivec1.size () = 0) // The length is 0 equal {cout <"vec1 = vec2" <Endl ;} else {// The two vectors are of the same length and are not 0 while (* iter1 = * iter2 & iter1! = Ivec1.end () & iter2! = Ivec2.end () {++ iter1; ++ iter2;} If (iter1 = ivec1.end ()) // all elements are equal. cout <"vec1 = vec2" <Endl; else cout <"vec1 = vec2" <Endl;} return 0 ;}