如果我知道在输入大小内检测到差异,那么在可能更短的输入上调用std :: equal是否安全

NoS*_*tAl 4 c++ stl

在做一些新的编程时,我偶然发现了以下困境:

我做的事情如下:

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())))
{
//
}
Run Code Online (Sandbox Code Playgroud)

如果我知道IP ip_list可以比my_ip_prefix短,但是在那种情况下,它们与my_ip_prefix至少一个位置不同,是否可以安全地调用?示例:使用ip调用它是否安全"10.20.30.4"

阿卡做标准规定的顺序检查从前面开始break;std::equal

似乎很明显A是肯定的,但也许ISO ppl想要给出并行化的选项实现......

Sha*_*our 5

如果我们查看std :: equalcppreference条目,它会说:

[...]其中last2表示first2 +(last1 - first1)

这意味着ip_list[i]至少需要这么长时间.这符合C++ 11标准草案,在25.2.11 Equal部分中说:

template<class InputIterator1, class InputIterator2>
  bool equal(InputIterator1 first1, InputIterator1 last1,
             InputIterator2 first2);
Run Code Online (Sandbox Code Playgroud)

返回:如果对于[first1,last1]范围内的每个迭代器i,则返回以下相应条件:*i ==*(first2 +(i - first1)),[...]

C++ 14中,你有一个版本,它接受第二个输入的结束迭代器,与C++ 11相同的部分:

template<class InputIterator1, class InputIterator2>
  bool equal(InputIterator1 first1, InputIterator1 last1,
             InputIterator2 first2, InputIterator2 last2);
Run Code Online (Sandbox Code Playgroud)

它说:

如果last1 - first1!= last2 - first2,则返回false.[...]