我有以下功能:
F(0) = 0
F(1) = 1
F(2) = 2
F(2*n) = F(n) + F(n+1) + n , n > 1
F(2*n+1) = F(n-1) + F(n) + 1, n >= 1
Run Code Online (Sandbox Code Playgroud)
我得到一个数字n < 10^25
,我必须表明存在一个值a
,如F(a)=n
.由于函数的定义方式,可能存在n
这样的F(a)=F(b)=n where a < b
情况,在这种情况下我必须返回b
而不是a
到目前为止我所拥有的是:
F(2*n) >= F(2*n+1) for any n
,所以我首先在F(2*n)中搜索它,如果我在那里找不到它,我搜索F(2*n + 1)我想我有算法实际找到我需要的东西,但我不能足够快地计算F(n).
更确切的说,做std::equal_to<float>()(float a,float b)
或std::equal_to<double>()(double a,double b)
没有的要好一些漂浮平等喜欢abs(diff) < EPSILON
或只是a==b
?
使用C++编程,也许我的思绪仍然停留在90年代,无法绕过一些概念请帮助我.
所以正在使用gtkmm进行开发,并遇到了一些多线程示例,就像这样
class ExampleWorker {
public:
void do_work(ExampleWindow* caller);
private:
mutable std::mutex m_Mutex;
};
ExampleWindow::ExampleWindow(){
ExampleWorker m_Worker;
std::thread* m_WorkerThread;
void ExampleWindow::on_start_button_clicked() {
if (blablabla){
blablabla
} else {
// Start a new worker thread.
m_WorkerThread = new std::thread(/////////////lambda function
[this] // <==== let this this be "this-A"
{
m_Worker.do_work(this); // let this this be "this-B"
}///////////////end of lambda function /////////////
);
}
Run Code Online (Sandbox Code Playgroud)
我理解的主要问题是lambda部分.所以首先lambda概念对我来说是个新鲜事,在C++中查找"捕获"的概念却没有找到太多,所以我最好的猜测是,在这种情况下,它允许我"捕获"" [this]"根据文档返回的内容,似乎是[this] captures the this pointer by value
指向作为线程指针的工作者的指针,因为工作程序包含互斥锁.
但是我不明白的是,对我来说,"this-B"似乎表明"this"正在将ExampleWindow对象本身传递给函数,正如Worker类所定义的那样,它应该将调用者传递给它; 请问"this-A"似乎能够引用工作线程本身.问题,这是否意味着lambda函数捕获[this]在这种情况下,取代正常的"this",它引用调用对象,如"this-B"?
更多关于lambda的事情,我一直努力理解的另一个例子来自boost asio.
//definition
void async_read_some(
const …
Run Code Online (Sandbox Code Playgroud)