Implementing my own array from a template class in C ++

I am trying to implement an array class in C ++ and trying to implement the doContain method, which checks if a particular element is in the array or not. I was wondering if something like this would work or even be a good way to do this:

    T *array;
    int size;

    public:
    array(int length=50) {
        size=length;
        array= new T[length];
    }

    bool doesContain(const T &obj) {
        bool bFlag = false;
        for (int i = 0; i < size; ++i) {
            if (obj == array[i]) {
                bFlag = true;
             }
        }
        return bFlag;
     }
+4
source share
1 answer

If you want to have a method that checks if an object is in an array, yes, this will work. Of course, the == operator is valid.

I recommend simply doing "return true" when you find a match, and "return false" at the bottom.

bool doesContain(const T &obj) {
    for (int i = 0; i < size; ++i) {
        if (obj == array[i]) {
            return true;
         }
    }
    return false;
 }
+5
source

Source: https://habr.com/ru/post/1608927/


All Articles