Home > Back-end >  Accommodation for dynamic array
Accommodation for dynamic array

Time:07-17

From this discussion, I have the following code to check if an element exists in an array:

#include <iostream>
#include <vector>

template <typename T, std::size_t N>
bool IsIn(T value, const T(&values)[N])
{
    for (const T& array_value : values)
    {
        if (value == array_value) return true;
    }
    return false;
}

int main() {

    int arr1[] = { 10, 20, 30 };
    bool ee1 = IsIn(10, arr1);
    std::cout << "ee1 = " << (ee1?"true":"false") << "\n";

    return 0;
}

I believe this code is good for array of fixed size (at compile time) only. If the array is dynamically created (the number of elements is not known at compile time), is there any way I can modify the code to accommodate it?

PS: I am aware of vector. However, I am just curious if there is any way to avoid it.

CodePudding user response:

Don't use C-style arrays unless you absolutely need to. Use std::array instead. For dynamic arrays, use std::vector.

You can then use iterators to make your function generic. However, this function already exists, it's called std::find. You can try to implement your own, for learning purposes, or look up an example implementation here: cppreference | find

#include <algorithm>
#include <array>
#include <iostream>
#include <string>
#include <vector>

int main(){
    std::array<int, 3> static_array{1, 2, 3};
    std::vector<int> dynamic_array{3, 4, 5}; 
    std::string str = "Hello World";

    std::array<int, 3>::iterator stat_found;
    if( (stat_found = std::find(static_array.begin(), static_array.end(), 3)) != static_array.end() ){
        std::cout << "Found 3 in static_array at pos: " << stat_found - static_array.begin() << "\n";
    }

    std::vector<int>::iterator dyn_found;
    if( (dyn_found = std::find(dynamic_array.begin(), dynamic_array.end(), 3)) != dynamic_array.end() ){
        std::cout << "Found 3 in dynamic_array at pos: " << dyn_found - dynamic_array.begin() << "\n";
    }

    std::string::iterator str_found;
    if( (str_found = std::find(str.begin(), str.end(), 'W')) != str.end() ){
        std::cout << "Found W in string at pos: " << str_found - str.begin() << "\n";
    }
}

CodePudding user response:

Without changing the body of your method, you can accommodate practically any collection type by abstracting over the collection type as well, i.e.

template <typename T, typename Collection>
bool IsIn(T value, const Collection &values)
{
  /* ... */
}

However, as inifnitezero noted, the standard way of doing this is actually with iterators, and many implementations already exist in the standard library for this.

  • Related