小编use*_*129的帖子

如何使用std :: vector :: emplace_back用于vector <vector <int >>?

  vector<vector<int> > res;
  res.emplace_back({1,2}); // change to res.push_back({1,2}); would work
Run Code Online (Sandbox Code Playgroud)

这给了我错误

main.cpp:61:25: error: no matching function for call to ‘std::vector<std::vector<int> >::emplace_back(<brace-enclosed initializer list>)’
main.cpp:61:25: note: candidate is:
In file included from /usr/include/c++/4.7/vector:70:0,
                 from /usr/include/c++/4.7/bits/random.h:34,
                 from /usr/include/c++/4.7/random:50,
                 from /usr/include/c++/4.7/bits/stl_algo.h:67,
                 from /usr/include/c++/4.7/algorithm:63,
                 from miscalgoc.hpp:1,
                 from main.cpp:1:
/usr/include/c++/4.7/bits/vector.tcc:92:7: note: void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >]
Run Code Online (Sandbox Code Playgroud)

如何使这项工作?另外,为什么这里需要分配器?

c++ vector c++11

11
推荐指数
2
解决办法
6163
查看次数

在c ++中的memoization仿函数包装器

这是我为函数编写的通用memoization包装器.它利用了tuplehash.

template<typename R, typename... Args>
class memofunc{
    typedef R (*func)(Args...);
    func fun_;
    unordered_map<tuple<Args...>, R, tuplehash::hash<tuple<Args...> > > map_;
public:
    memofunc(func fu):fun_(fu){}
    R operator()(Args&&... args){
    auto key = make_tuple(std::forward<Args>(args)...);
    auto q = map_.find(key);
    if(q == map_.end()){
        R res = fun_(std::forward<Args>(args)...);
        map_.insert({key,res});
        return res;
    }else{
        return q->second;
    }
    }
};
Run Code Online (Sandbox Code Playgroud)

Fibonacci数字的使用示例.

long long fibo(long long x){
    static memofunc<long long, long long> memf(fibo);
    // try to replace fibo with this new fibo but doesn't work, why?
    // function<long long(long long)> fibo …
Run Code Online (Sandbox Code Playgroud)

c++ recursion templates memoization c++11

6
推荐指数
1
解决办法
742
查看次数

将一个函数列表应用于参数,clojure

(def ops '(+ - * /))
(map #(% 2 5) ops)
Run Code Online (Sandbox Code Playgroud)

(5 5 5 5)
Run Code Online (Sandbox Code Playgroud)

这对我来说没有意义.为什么这会返回5的列表而不是函数调用的结果?

clojure

6
推荐指数
2
解决办法
1288
查看次数

在 c++11 中按值捕获成员变量的好方法是什么?

struct myclass {
    myclass(){}
    myclass(int qx):z(qx){   }
    std::function<void()> create() {
        auto px = [z](){
            std::cout << z << std::endl;
        };
        return px;
    }
    int z;
};

myclass my;
my.z = 2;
auto func = my.create();
func();
my.z = 3;
func();
Run Code Online (Sandbox Code Playgroud)

这段代码将在 gcc 4.6.3 中编译,它会做正确的事情来制作成员变量的副本z,并且两个 print 都会得到2. 在 gcc 4.8.2 这不再编译..

error: 'this' was not captured for this lambda function
Run Code Online (Sandbox Code Playgroud)

我想知道为什么这个功能被删除了,因为它非常有用。

c++ g++ c++11

6
推荐指数
1
解决办法
2891
查看次数

如何用某种类型创建NULL?

我的代码是

TreeNode *sortedArrayToBST(vector<int> &num) {
    function<TreeNode*(int,int)> func=
        [&func,&num](int s, int e){
            TreeNode* p = NULL;
            if(s>e) return NULL; // change to return p would compile
            int m = (s+e)/2;
            p = new TreeNode(num[m]);
            p->left = func(s,m-1);
            p->right = func(m+1,e);
            return p;
        };
    return func(0,num.size()-1);
}

Solutions.cpp:957:21: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
Solutions.cpp:959:29: error: inconsistent types ‘TreeNode*’ and ‘int’ deduced for lambda return type
Solutions.cpp:959:29: error: invalid conversion from ‘TreeNode*’ to ‘int’ [-fpermissive]
Solutions.cpp:962:12: error: inconsistent types …
Run Code Online (Sandbox Code Playgroud)

c++ g++ c++11

3
推荐指数
1
解决办法
434
查看次数

如何从lambda表达式派生参数?

如何更改模板函数定义以使其起作用?

请考虑以下代码:

#include <iostream>

#include <functional>

using namespace std;

void callthis(function<void()> func){
    func();
}

void callthis(function<void(int)> func, int par){
    func(par);
}


template<typename... Args>
void callthistemp(function<void(Args...)> func, Args&&... args){
    func(std::forward<Args>(args)...);
}

int main(){

callthis([](){cout << "hello " << endl;}); // (1)
callthis([](int x)->void{cout << "hello " << x << endl;},1); //(2)
function<void(int)> xx = [](int x){cout << "hello" << endl;};
callthistemp(xx,1);//(3)

//callthistemp([](int x)->void{cout << "hello" << endl;},1); //(4)
//callthistemp<int>([](int x)->void{cout << "hello" << endl;},1); //(5)
}
Run Code Online (Sandbox Code Playgroud)

前三个案例都运行良好,但最后两个不编译,并给出错误

lambdatemplate.cpp: In function ‘int …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

3
推荐指数
1
解决办法
260
查看次数

标签 统计

c++ ×5

c++11 ×5

g++ ×2

clojure ×1

lambda ×1

memoization ×1

recursion ×1

templates ×1

vector ×1