Skip to main content
format code
Source Link
Basilevs
  • 4.5k
  • 1
  • 20
  • 33

In your case, both FindMaxDistanceVector and FindMaxDistanceList perform analogous operations on std::vectorstd::vector and std::liststd::list, respectively.

By using iterators, your function can operate on any container type that supports iteration

template<typename InputIt1, typename InputIt2>

int FindMaxDistance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool addDebugInfo) { if (std::distance(first1, last1) != std::distance(first2, last2)) { return INT_MIN; // Size mismatch }

template<typename InputIt1, typename InputIt2>

int FindMaxDistance(InputIt1 first1, InputIt1 last1,
                    InputIt2 first2, InputIt2 last2,
                    bool addDebugInfo) {
    if (std::distance(first1, last1) != std::distance(first2, last2)) {
        return INT_MIN; // Size mismatch
    }

    int maxVal = INT_MIN;
    auto it1 = first1;
    auto it2 = first2;
    for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
        if (*it1 + *it2 > maxVal) {
            maxVal = *it1 + *it2;
        }
    }

    if (addDebugInfo) {
        int minVal = INT_MAX;
        it1 = first1;
        it2 = first2;
        for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
            if (std::abs(*it1 - *it2) < minVal) {
                minVal = std::abs(*it1 - *it2);
            }
        }
        LOGGER->INFO(LOGGINGTYPE::Debug, "Minimum distance between the containers = %d", minVal);
    }

    return maxVal;
}

}

In your case, both FindMaxDistanceVector and FindMaxDistanceList perform analogous operations on std::vector and std::list, respectively.

By using iterators, your function can operate on any container type that supports iteration

template<typename InputIt1, typename InputIt2>

int FindMaxDistance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool addDebugInfo) { if (std::distance(first1, last1) != std::distance(first2, last2)) { return INT_MIN; // Size mismatch }

int maxVal = INT_MIN;
auto it1 = first1;
auto it2 = first2;
for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
    if (*it1 + *it2 > maxVal) {
        maxVal = *it1 + *it2;
    }
}

if (addDebugInfo) {
    int minVal = INT_MAX;
    it1 = first1;
    it2 = first2;
    for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
        if (std::abs(*it1 - *it2) < minVal) {
            minVal = std::abs(*it1 - *it2);
        }
    }
    LOGGER->INFO(LOGGINGTYPE::Debug, "Minimum distance between the containers = %d", minVal);
}

return maxVal;

}

In your case, both FindMaxDistanceVector and FindMaxDistanceList perform analogous operations on std::vector and std::list, respectively.

By using iterators, your function can operate on any container type that supports iteration

template<typename InputIt1, typename InputIt2>

int FindMaxDistance(InputIt1 first1, InputIt1 last1,
                    InputIt2 first2, InputIt2 last2,
                    bool addDebugInfo) {
    if (std::distance(first1, last1) != std::distance(first2, last2)) {
        return INT_MIN; // Size mismatch
    }

    int maxVal = INT_MIN;
    auto it1 = first1;
    auto it2 = first2;
    for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
        if (*it1 + *it2 > maxVal) {
            maxVal = *it1 + *it2;
        }
    }

    if (addDebugInfo) {
        int minVal = INT_MAX;
        it1 = first1;
        it2 = first2;
        for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
            if (std::abs(*it1 - *it2) < minVal) {
                minVal = std::abs(*it1 - *it2);
            }
        }
        LOGGER->INFO(LOGGINGTYPE::Debug, "Minimum distance between the containers = %d", minVal);
    }

    return maxVal;
}
Source Link

In your case, both FindMaxDistanceVector and FindMaxDistanceList perform analogous operations on std::vector and std::list, respectively.

By using iterators, your function can operate on any container type that supports iteration

template<typename InputIt1, typename InputIt2>

int FindMaxDistance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool addDebugInfo) { if (std::distance(first1, last1) != std::distance(first2, last2)) { return INT_MIN; // Size mismatch }

int maxVal = INT_MIN;
auto it1 = first1;
auto it2 = first2;
for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
    if (*it1 + *it2 > maxVal) {
        maxVal = *it1 + *it2;
    }
}

if (addDebugInfo) {
    int minVal = INT_MAX;
    it1 = first1;
    it2 = first2;
    for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
        if (std::abs(*it1 - *it2) < minVal) {
            minVal = std::abs(*it1 - *it2);
        }
    }
    LOGGER->INFO(LOGGINGTYPE::Debug, "Minimum distance between the containers = %d", minVal);
}

return maxVal;

}