考虑以下计划:
#include <iostream>
#include <algorithm>
#include <array>
bool greater_than_seven(int i) {
return i > 5;
}
bool divisible_by_five(int x) {
return ((x%5)==0);
}
int main() {
int arr[]{3,6,9,12,15};
std::cout<<"Enter a number you want to search: ";
int num;
std::cin>>num;
auto result(std::find(std::begin(arr),std::end(arr),num));
if(result != std::end(arr))
std::cout<<"arr contains: "<<num<<'\n';
else
std::cout<<"arr doesn't contain: "<<num<<'\n';
for(result=std::find_if(std::begin(arr),std::end(arr),greater_than_seven);result!=std::end(arr);++result)
std::cout<<*result<<' ';
std::cout<<'\n';
std::array<int,4> x{33,66,99,55};
for(result=std::find_if_not(std::begin(x),std::end(x),divisible_by_five);result!=std::end(x);++result)
std::cout<<*result<<'\n';
}
此程序在g++&上编译良好;叮当声++。
查看实时演示
here
(g++5.4.0)
查看实时演示
here
(叮当声++3.8.0)
但它在Microsoft Visual C++编译器上给出了可怕的编译器错误。
查看实时演示
here
(针对x64的Microsoft(R)C/C++优化编译器版本19.00.23506)
Error(s):
source_file.cpp(27): error C2440: '=': cannot convert from 'std::_Array_iterator<_Ty,4>' to 'int *'
with
[
_Ty=int
]
source_file.cpp(27): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
source_file.cpp(27): error C2679: binary '!=': no operator found which takes a right-hand operand of type 'std::_Array_iterator<_Ty,4>' (or there is no acceptable conversion)
with
[
_Ty=int
]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(343): note: could be 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(348): note: or 'bool std::operator !=(std::nullptr_t,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\exception(353): note: or 'bool std::operator !=(const std::exception_ptr &,std::nullptr_t) throw()' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(388): note: or 'bool std::operator !=(const std::error_code &,const std::error_code &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(395): note: or 'bool std::operator !=(const std::error_code &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(402): note: or 'bool std::operator !=(const std::error_condition &,const std::error_code &) noexcept' [found using argument-dependent lookup]
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\system_error(409): note: or 'bool std::operator !=(const std::error_condition &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
source_file.cpp(27): note: while trying to match the argument list '(int *, std::_Array_iterator<_Ty,4>)'
with
[
_Ty=int
]
所以问题是,根据C++标准,这里是哪个编译器?这是VC++编译器中的错误吗?