小编Lun*_*oul的帖子

C++ STL - 为什么forward_list没有size()方法?

我一直在使用C++ 11 forward_list作为快速插入的容器,没有太多的内存开销,因为它是一个单链表.

在意识到forward_list没有size()方法之后,我对这背后的推理感到有点困惑.它是否只能维护一个私有字段来跟踪插入和删除的节点,从而实现O(1)size()操作?

stl c++11 forward-list

14
推荐指数
2
解决办法
5006
查看次数

可变参数模板递归返回类型推导编译错误

为什么以下代码无法编译?

template <typename T>
T sum(T t){
    return t;
}

template <typename T, typename ...U>
auto sum(T t, U... u) -> decltype(t + sum(u...)) {
    return t + sum(u...);
}

int main() {
    sum(1, 1.5, 2);
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

error: no matching function for call to ‘sum(int, double, int)’
 sum(1, 1.5, 2);
Run Code Online (Sandbox Code Playgroud)

实现此功能的好方法是什么?

c++ variadic-templates c++11 return-type-deduction

7
推荐指数
1
解决办法
847
查看次数

在C++中从函数返回引用的推荐方法

我一直在使用这种方法从C++中的函数返回引用.但是,我怀疑有更好的模式来执行这样的操作.另外,我猜这种方法意味着内存泄漏.

class A {};
A& return_instance_of_A(){
    A* result = new A();
    return *result;
}
Run Code Online (Sandbox Code Playgroud)

使用shared_ptr会是更好的选择吗?

c++ memory-leaks memory-management

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

在类定义中涉及private unique_ptr时,在C++中引用初始化

如何解释下面的编译错误?

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

class A{
    unique_ptr<vector<short>> v;
public:
    A(){
        v = unique_ptr<vector<short>>(new vector<short>());
        cout << "A()" << endl;
    }
    A(const A& a) : A(){
        cout << "A(const A& a)" << endl;
    }
};

int main() {

    A a1; // prints A()
    A a2 = a1; // prints A() then A(const A& a)
    A& a3 = a1; // prints nothing
    a3 = a1; // compile time error: use of deleted function ‘A& A::operator=(const A&) …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks memory-management

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

如何实现可移动的重载而不违反C++中的DRY原则?

虽然在我的一些类中实现方法和运算符重载以利用C++中的右值引用,但我经常编写一些设计不良而违反DRY原则的代码.对于下面的代码片段,什么是更好的替代方案?(这段代码只是为了说明问题)

class matrix_2_2
{
    int _m[2][2];

public:

    matrix_2_2 operator*(const matrix_2_2& m) const &
    {   
        matrix_2_2 res;
        for(int i = 0 ; i < 2 ; i ++)
            for(int j = 0 ; j < 2 ; j++)
                for(int k = 0 ; k < 2 ; k++)
                    res._m[i][j] = (res._m[i][j] + _m[i][k]*m._m[k][j]);

        return res;
    }

    matrix_2_2 operator*(matrix_2_2&& m) &&
    {
        matrix_2_2 res;
        for(int i = 0 ; i < 2 ; i ++)
            for(int j = 0 ; j < …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading rvalue-reference c++11

0
推荐指数
1
解决办法
92
查看次数

R - 从列表列表构造稀疏矩阵

使用python和pandas我可以很容易地从字典对象列表构造一个稀疏的DataFrame.以下代码段显示了如何在pandas中完成此操作:

In [1]: import pandas as pd; (pd.DataFrame([{'a':1, 'b':10}, 
                                            {'d':99, 'c':1},
                                            {'b':1, 'd': 4}])
                                .fillna(0))
Out[1]: 
     a     b    c     d
0  1.0  10.0  0.0   0.0
1  0.0   0.0  1.0  99.0
2  0.0   1.0  0.0   4.0
Run Code Online (Sandbox Code Playgroud)

如果我想在R中轻松重现此行为怎么办?我们假设我有以下变量:

values <- list(list(a = 1, b = 10),
               list(d = 99, c = 1),
               list(b = 1, d = 4))
Run Code Online (Sandbox Code Playgroud)

然后,如何使用R获得在python中获得的相同结果?

python r pandas

0
推荐指数
1
解决办法
92
查看次数