小编Sas*_*yal的帖子

为什么此C ++折叠表达式有效?

cppreference上,我看到折叠表达式有四种类型,一元右,一元左,二进制右和二进制左。这种折叠表达式的类型是什么?我很难理解它为什么有效。

    template <typename Res, typename... Ts>
    vector<Res> to_vector(Ts&&... ts) {
        vector<Res> vec;
        (vec.push_back(ts) ...); // *
        return vec;
    }
Run Code Online (Sandbox Code Playgroud)

*行中的“ pack”,“ op”和“ init”的值是什么(如果有)?

该示例摘自Bjarne Stroustrup的A C C ++书的第244页,并且似乎在示例中忘记了逗号,因此引起了我的困惑。

c++ syntax fold-expression

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

如何从插入包拆分数据创建createDataPartition函数?

从文档:

对于自举样本,使用简单的随机采样.

对于其他数据分割,当y是试图平衡分裂内的类分布的因素时,随机采样在y的水平内完成.

对于数字y,样本基于百分位数分组为组,并且在这些子组内进行采样.

对于createDataPartition,百分位数通过groups参数设置.

我不明白为什么需要这种"平衡"的东西.我认为我从表面上理解它,但任何额外的见解都会非常有用.

r subset data-partitioning r-caret

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

具有父指针的二叉搜索树有什么优势?

到目前为止,我一直用左右指针实现二叉搜索树,如:

template<typename T>
struct BSTNode{
    BSTNode* left;
    BSTNode* right;
    T data;
}
Run Code Online (Sandbox Code Playgroud)

我遇到了节点也有父节点指针的实现.你为什么想这么做?有什么权衡取舍?

c++ binary-search-tree

4
推荐指数
2
解决办法
818
查看次数

没有用于调用“std::less&lt;int&gt;::less(const int&amp;, const int&amp;)”的匹配函数

我试着写:

#include <functional>

template<class T, class func = std::less<T>>
class Car {
public:
    void check(const T& x, const T& y) {
        func(x, y);            //.... << problem
    }
};


int main() {
    Car<int> car;
    car.check(6, 6);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在这里的意思是它会识别通常的<for int,但它说明了我标记的位置:

没有用于调用“std::less::less(const int&, const int&)”的匹配函数

但是,如果我创建了一个Car自定义的,func那么它就可以工作了……我该如何解决这个问题?

c++ templates class

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

在C++中为向量容器重载[]运算符时,我为未定义的索引返回什么?

我刚开始用C++实现一个基本的向量容器.它还远未完成,但它看起来像这样:

using namespace std;

typedef unsigned  long long int bigInt;

namespace stl2{
    template<class T>
    class vector{
    private:
        bigInt l;
        bigInt cap;
        T* arr;
    public:
        vector(){
            cap = 0;
            l = 0;
        }

        ~vector(){
            if (cap > 0) delete[] arr;
        }

        vector(bigInt size){
            cap = size;
            l = size;
            arr = new T[size];
        }

        vector(bigInt size, const T& def) : vector(size){
            for (bigInt i = 0; i < size; i++){
                arr[i] = def;
            }
        }

        bigInt size(){
            return l;
        }

        bigInt …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

使用std :: vector时,抽象类类型'Shape'的新表达式无效

所以我有一个Shape抽象基类.

class Shape{
    virtual int getRadius() = 0;
};
Run Code Online (Sandbox Code Playgroud)

还有派生类,Sphere

class Sphere: public Shape {
private:
    int radius;
    int origin = 5;
public:
    Sphere(int radius){
        this->radius = radius;
    }

    int getRadius() {
        return this->radius;
    }
};
Run Code Online (Sandbox Code Playgroud)

在我实例化一个半径为2的球体对象后,我将它推入一个std :: vector对象.但是当我尝试这样做时出现错误:

int main() {
    std::vector<std::shared_ptr<Shape>> shapes;
    Sphere * firstSphere = new Sphere(2);
    shapes.push_back(firstSphere);
    cout << shapes[0]->getRadius() <<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在复制构造函数'std :: vector <_Tp,_Alloc> :: vector(const std :: vector <_Tp,_Alloc>&)':我想要做的是实现多态,因为我将有几个派生自的形状类形状 ABC和我希望能够将它们推入形状矢量容器中,并能够访问它们并调用它们的方法.

我究竟做错了什么?什么是最好的方法呢?这个问题的要点还在于提出实现多态的最佳方法.

Scnerario:1.形状 ABC 2. …

c++ oop polymorphism vector c++11

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