C ++ 0513 vector array and (ii)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/baiyibin0530/article/details/90166169

Const vector of operator overloading

F void (const Vector & CV)
{
    Double D = CV [. 1]; // error, but should be correct
    cv [1] = 2.0; // error (the present case)
}
The reason is that our vector :: operator [] () could potentially alter vector objects. Even though it actually has not changed
, the compiler will think this is a mistake, because we forgot to tell the compiler.
The solution is to re-define a const member function.

Vector class
{
    // ...
    Double & operator [] (n-int); // for non-const Vector
    Double operator [] (n-int) const; // Vector for const
};


Array

Now, you may find that we obviously prefer to use vector instead of an array.
If you can choose to use std :: vector as possible.

Pointer to the array elements

vector<int> x(100);
vector<int> y(100);

... //
X = Y; // copy 100 int

Array initialization

char ac [] = "Beorn" ; // array of six characters
NOTE: The value of 0 is not a character char '0' or any other letter or number.
The purpose of this is to end 0 end of the string is positioned helper functions

int strlen(const char* p)
{
    int n = 0;
    while(p[n]) ++n;
    return n;
}


 

Guess you like

Origin blog.csdn.net/baiyibin0530/article/details/90166169