C ++: modifying elements (CODE) and vector elements in a vector

Source: Internet
Author: User

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:

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.