C ++: modifying elements (CODE) and vector elements in a vector
C ++: modifying elements in a vector
# Include <iostream> # include <windows. h> # include <math. h >#include <cstdio >#include <cstring >#include <vector> using namespace std; void vector_out (vector <int> & vector_in) {for (unsigned int I = 0; I <vector_in.size (); I ++) {if (I = vector_in.size ()-1) {cout <vector_in [I] <endl ;} else cout <vector_in [I] <',' ;}} int main () {string s ("a2cdefghijklmn"); vector <int> abc (10 ); if (abc. begin ()! = Abc. end () {for (unsigned int I = 0; I <abc. size (); I ++) {abc [I] = I + 1 ;}cout <"Raw data:" <endl; vector_out (abc ); auto & val2 = abc. back (); // val2 is the reference val2 = 2 pointing to the last element; cout <"variable is the reference type, after modification:" <endl; vector_out (abc ); auto val3 = abc. back (); // only abc. A copy of back () val3 = 10; cout <"variable is not a reference and the modification is invalid:" <endl; vector_out (abc);} cin. get (); return 0 ;}
Running result: