我发现它binary_function已从C++ 11中删除.我想知道为什么.
C++ 98:
template <class T> struct less : binary_function <T,T,bool> {
bool operator() (const T& x, const T& y) const {return x<y;}
};
Run Code Online (Sandbox Code Playgroud)
C++ 11:
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
Run Code Online (Sandbox Code Playgroud)
MODIFIED ------------------------------------------------- ---------------------------
template<class arg,class result>
struct unary_function
{
typedef arg argument_type;
typedef result result_type;
};
Run Code Online (Sandbox Code Playgroud)
例如,如果我们想在C++ 98中为函数编写适配器,
template <class T> struct even : …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个一元函子,它将取消引用它的参数并返回结果.当然我可以写一个,它似乎应该已经存在.
所以给出代码:
const auto vals = { 0, 1, 2, 3 };
vector<const int*> test(size(vals), nullptr);
iota(begin(test), end(test), data(vals));
transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; });
Run Code Online (Sandbox Code Playgroud)
我希望有一个我可以用而不是lambda的仿函数.这样的事情存在,还是我需要使用lambda?
我通过 vcpkg 安装了 boost-program-options 版本 1.78。当我编译时clang++,-std=c++20出现以下错误。当我用 编译时不会发生这种情况g++。据此,从 C++11 开始,这已被弃用。 std::unary_function
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/program_options/variables_map.hpp:12:
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/any.hpp:20:
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/type_index.hpp:29:
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/type_index/stl_type_index.hpp:47:
/home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:132:33: warning: 'unary_function<const std::error_category *, unsigned long>' is deprecated [-Wdeprecated-declarations]
struct hash_base : std::unary_function<T, std::size_t> {};
^
/home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:692:18: note: in instantiation of template class 'boost::hash_detail::hash_base<const std::error_category *>' requested here
: public boost::hash_detail::hash_base<T*>
^
/home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:420:24: note: in instantiation of template class 'boost::hash<const std::error_category *>' …Run Code Online (Sandbox Code Playgroud) 传递函数对象的以下小程序有什么问题?
#include <iostream>
#include <functional>
void foo(const std::unary_function<const std::string&, void>& fct) {
const std::string str = "test";
fct(str); // error
}
class MyFct : public std::unary_function<const std::string&, void> {
public:
void operator()(const std::string& str) const {
std::cout << str << std::endl;
}
};
int main(int argc, char** argv){
MyFct f;
foo(f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在第6行收到以下错误:
no match for call to
`(const std::unary_function<const std::string&, void>) (const std::string&)'
Run Code Online (Sandbox Code Playgroud) int bar(int *arr, size_t n)
{
int sum = 0, i;
for (i = n; i > 0; i--)
{
sum += !arr[i - 1];
}
return ~sum + 1;
}
Run Code Online (Sandbox Code Playgroud)
我看过这段代码,但不太明白sum += !arr[i - 1];:!(NOT)应用于数组指针的作用是什么?另外,〜之前有sum什么作用?
我想找出在调用括在括号中的SQL函数之前包含连字符时,SQL语法背后的逻辑是什么。
这是SQL:
IF (@StartDate > @EndDate)
BEGIN
SET @EndDate = @StartDate
SET @StartDate = @EndDate
END
DECLARE @nonworkingweekdays int
--now deal with public holidays
SELECT @nonworkingweekdays = count("Date") from
(
select distinct
(
CASE datepart(weekday,date)
WHEN 1 THEN null --ignore sundays
WHEN 7 THEN null --ignore saturdays
else "Date"
END
) AS "date"
from publicholidays
) nonworkingweekdays
WHERE
"Date" is not null and
"Date" between @StartDate and DATEADD(day, -1, @EndDate)
RETURN
CASE WHEN @StartDate <= @EndDate
THEN
dbo.FullWeekDays(@StartDate, @EndDate) - …Run Code Online (Sandbox Code Playgroud)