34th class overloading of array operators

Source: Internet
Author: User

1. compatibility of string classes

(1) The String class maximizes the compatibility of C strings

(2) You can use a string object in the same way that you use C strings , such as

string " ABCEDEFG " ; char c = s[i];

The "Programming Experiment" uses the string class in C mode

#include <iostream>#include<string>using namespacestd;intMain () {strings ="a1b2c3d4e"; intn =0;  for(intI=0; I<s.length (); i++)    {        if(IsDigit (S[i]))//use a String object (that is, an array) in C-strings{n++; }} cout<< n << Endl;//4    return 0;}

2. How objects of the class support subscript access to arrays

(1) Array access is a built-in operator in C/s + +

(2) The primitive meaning of array accessors is array access and pointer operations

a[n]←→* (A + N) ←→* (n + a) ←→n[a]

Review of "instance analysis" pointers and arrays

#include <iostream>#include<string>using namespacestd;intMain () {inta[5] = {0};  for(intI=0; i<5; i++) {A[i]=i; }         for(intI=0; i<5; i++)    {       //pointer-mode access array, less intuitive! //while subscript access is used, you can hide the manipulation of pointerscout << * (A + i) << Endl;//cout<< A[i] << Endl;} cout<<Endl;  for(intI=0; i<5; i++) {I[a]= i +Ten;//A[i] = i + ten;    }         for(intI=0; i<5; i++) {cout<< * (i + a) << Endl;//cout<< A[i] << Endl;    }    return 0;}

3. Overloaded array access operators:[]

(1) can only be overloaded with member functions of a class

(2) overloaded functions can and can only use one parameter

(3) Multiple overloaded functions can be defined with different parameters

(4) You can hide the operation of the pointer

"Programming Experiment" overloaded array access operators

The perfection of the "Programming Experiment" array class

4. Summary

(1) String class Max program compatible with C string usage

(2) overload of an array accessor allows the object to emulate the behavior of an array

(3) Array accessors can be overloaded only by member functions of the class

(4) Overloaded functions can and can only use one parameter

34th class overloading of array operators

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.