Is it safe to call std :: equal on a potentially shorter input if I know that a difference in input size will be detected

While doing some nw programming, I came across the following dilemma:

I am doing something like:

static const string my_ip_prefix = "111.222.233";

//going through list of IPs where one might have prefix my_ip_prefix

if (equal(my_ip_prefix .begin(), my_ip_prefix .end(), ip_list[i].begin())))
{
//
}

If I know that the IP addresses from ip_listmay be shorter than my_ip_prefix, but in this case they differ from my_ip_prefixat least one position in them, is it safe to call equal? Example: is it safe to call it using ip"10.20.30.4"

Does Aka perform a standard check of sequential checks, starting from the front and break;at std::equal?

It might seem obvious that A is yes, but maybe ISO ppl wanted to provide implementation options for parallelization ...

+4
3

++ 03 std::equal() , , , .

++ 14 std::equal(), .


IP- uint32_t , :

auto ip_prefix = ::inet_addr("111.222.233.0");
auto ip_mask = ::inet_addr("255.255.255.0");

bool compare(in_addr_t a, in_addr_t b, in_addr_t mask) {
    return (a & mask) == (b & mask);
}

int main() {
    std::cout << compare(ip_prefix, ::inet_addr("1.1.1.1"), ip_mask) << '\n';
    std::cout << compare(ip_prefix, ::inet_addr("111.222.233.3"), ip_mask) << '\n';
}
+4

cppreference std:: , :

[...], last2 first2 + (last1 - first1)

, ip_list[i] . ++ 11, 25.2.11 Equal :

template<class InputIterator1, class InputIterator2>
  bool equal(InputIterator1 first1, InputIterator1 last1,
             InputIterator2 first2);

: true, [first1, last1] : * == * (first2 + (i - first1)), [...]

++ 14 , , , ++ 11:

template<class InputIterator1, class InputIterator2>
  bool equal(InputIterator1 first1, InputIterator1 last1,
             InputIterator2 first2, InputIterator2 last2);

:

last1 - first1!= last2 - first2, false. [...]

+5

- . , . std::string, ( a size()), , :

if ( ip_list[i].size() >= my_ip_prefix
        && std::equal( my_ip_prefix.begin(), my_ip_prefix.end(), ip_list[i].begin() ) ) {
    //  ...
}
+2

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


All Articles