C + + subscript operation considerations

Source: Internet
Author: User

In C + +, subscript operations do not add elements, and for any use of subscript operations, such as string types, vector types, and so on, must be existing elements to be indexed with subscript operators. If the type is empty, pass

When the subscript operation is assigned, no element is added and does not work at all, and the validation code is as follows:

1#include <iostream>2#include <vector>3 using namespacestd;4 intMain ()5 {6      stringRes;7res[0] ='H';8res[1] ='e';9res[2] ='L';Tenres[3] ='L'; Oneres[4] ='o'; Acout << Res <<"\ n"; -      return 0; -}

The result of the final output is null. If you want to do this, the code looks like this:

1#include <iostream>2#include <vector>3 using namespacestd;4 intMain ()5 {6      stringRes5,"');7res[0] ='H';8res[1] ='e';9res[2] ='L';Tenres[3] ='L'; Oneres[4] ='o'; Acout << Res <<"\ n"; -      return 0; -}

The principle is that existing elements use subscript operators to make sense.

C + + subscript operation considerations

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.