小编Gar*_*Gar的帖子

不同类的矢量

我有一个带有向量的类,我想用两种类型中的一种填充,由用户选择.让我们调用我的类option1和option2

我想做些什么呢

class storage_class 
{
public:
    storage_class(int sel, int n)
    {
        if(sel == 1)
           for(int i = 0; i < n; i++) 
               my_store.push_back(std::make_unique<option1>());    
        else if(sel == 2)
           for(int i = 0; i < n; i++)
               my_store.push_back(std::make_unique<option2>());
    }

private:
    // Something like this but that actually works
    std::vector<T> my_store;
};
Run Code Online (Sandbox Code Playgroud)

然后我想像这样或者类似的东西使用它,所以不需要根据所选的选项修改这种用法.

int main()
{
    storage_class store(1);

    int n_iterations = 4;

    for(int i = 0; i < n_iterations; i++)
    {
        store.my_store[i]->create_data();
    }
}
Run Code Online (Sandbox Code Playgroud)

类option1和option2将是数学模拟,它将创建数据并将自己的数据存储在作为类成员的向量中.

我想在向量中存储任一选项的多个实例,然后从那里操作它们.我可以使用C++ 17.

c++ templates c++17

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

传递成员函数时替代std :: bind

我有一些我想要基准的功能.我希望能够将它们传递给基准测试功能.以前我已经将一个函数指针和对象的引用传递给了测试函数,就像这样

template<typename T>
void (T::*test_fn)(int, int), T& class_obj, )
Run Code Online (Sandbox Code Playgroud)

目前我有这个

#include <iostream>
#include <functional>
using namespace std::placeholders;

class aClass
{
public:
    void test(int a, int b)
    {
        std::cout << "aClass fn : " << a + b << "\n";
    }

};

class bClass
{
public:
    void test(int a, int b)
    {
        std::cout << "bClass fn : " << a * b << "\n";
    }

};

// Here I want to perform some tests on the member function
// passed in …
Run Code Online (Sandbox Code Playgroud)

c++ c++14

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

ipywidgets 动态创建复选框和选择数据

我想创建一个复选框列表,以便用户可以从数据列表中进行选择。我已经为每条数据创建了复选框,现在我希望勾选这些复选框以将数据添加到列表中。

import ipywidgets as widgets

data = ["data1", "data2", "data3", "data4"]

selected_data = []

checkboxes = [widgets.Checkbox(value=False, description=label) for label in data]

widgets.VBox(children=checkboxes)
Run Code Online (Sandbox Code Playgroud)

我想做一些类似的事情

def add_to_selected(d):
    selected_data.append(d)

checkboxes[0].observe(add_to_selected)
Run Code Online (Sandbox Code Playgroud)

这将向列表添加一个值selected_data。我不知道如何让 VBox 中的复选框表现得像这样。

python ipython jupyter-notebook ipywidgets

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

通过unique_ptr访问std :: array

这只是为了学习目的,我知道我可以使用矢量,但我有

const int N = 1e3;
auto my_arr = std::make_unique<std::array<int, N>>();

// To access it I have to do this - why [0][0] twice?
my_arr.get()[0][0] = 1;

// Getting the size for fun
std::cout << sizeof(my_arr.get()) << "\n"; // outputs 8
std::cout << sizeof(my_arr.get()[0]) << "\n"; // outputs 800000000
std::cout << sizeof(my_arr.get()[0][0]) << "\n"; // outputs 8
Run Code Online (Sandbox Code Playgroud)

我知道.get()返回一个指向托管对象的指针,但我不明白为什么我需要做my_arr.get()[0][0]两次?

c++ c++14

0
推荐指数
2
解决办法
147
查看次数

标签 统计

c++ ×3

c++14 ×2

c++17 ×1

ipython ×1

ipywidgets ×1

jupyter-notebook ×1

python ×1

templates ×1