我想写 5 个不同的类,每个类都有许多完全相同的成员函数,除了一个,每个类都是特殊的。我可以写这个避免代码重复吗?
问候, 阿列克谢
下面是我的代码的一个非常缩短的版本,它引发了错误:
template_test.cpp:15:35: error: invalid use of incomplete type ‘class impl_prototype<cl, 1>
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
using namespace std;
template <int cl, int dim>
class impl_prototype {
public:
impl_prototype() {}
int f(int x) { return cl + 2 * g(x); }
int g(int x) { return cl + 1 * x;}
};
template <int cl>
int impl_prototype<cl, 1>::g(int x) { return cl + 3 * x; }
int main ()
{
impl_prototype<0, 0> test_0;
impl_prototype<0, 1> test_1;
cout …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个可以在我的主程序的后台运行并监视某个线程的线程.在某些时候,主程序应该通知线程安全退出.以下是以固定间隔将本地时间写入命令行的最小示例.
#include <cmath>
#include <iostream>
#include <thread>
#include <future>
#include <chrono>
int func(bool & on)
{
while(on) {
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << ctime(&t) << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
int main()
{
bool on = true;
std::future<int> fi = std::async(std::launch::async, func, on);
std::this_thread::sleep_for(std::chrono::seconds(5));
on = false;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当"on"变量未通过引用传递时,此代码将编译并生成预期结果,但线程永远不会终止.一旦变量通过引用传递,我就会收到编译器错误
In file included from /opt/extlib/gcc/5.2.0/gcc/5.2.0/include/c++/5.2.0/thread:39:0,
from background_thread.cpp:3:
/opt/extlib/gcc/5.2.0/gcc/5.2.0/include/c++/5.2.0/functional: In instantiation of ‘struct std::_Bind_simple<int (*(bool))(bool&)>’:
/opt/extlib/gcc/5.2.0/gcc/5.2.0/include/c++/5.2.0/future:1709:67: required from ‘std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = int (&)(bool&); …Run Code Online (Sandbox Code Playgroud) 我正在努力学习熊猫。我找到了几个关于如何构建 Pandas 数据框以及如何添加列的示例,它们运行良好。我想学习根据列的值选择所有行。如果列的值应该小于或大于某个数字,我已经找到了多个关于如何执行选择的示例,这也有效。我的问题是如何进行更一般的选择,我想首先计算列的函数,然后选择函数值大于或小于某个数字的所有行
import names
import numpy as np
import pandas as pd
from datetime import date
import random
def randomBirthday(startyear, endyear):
T1 = date.today().replace(day=1, month=1, year=startyear).toordinal()
T2 = date.today().replace(day=1, month=1, year=endyear).toordinal()
return date.fromordinal(random.randint(T1, T2))
def age(birthday):
today = date.today()
return today.year - birthday.year - ((today.month, today.day) < (birthday.month, birthday.day))
N_PEOPLE = 20
dict_people = { }
dict_people['gender'] = np.array(['male','female'])[np.random.randint(0, 2, N_PEOPLE)]
dict_people['names'] = [names.get_full_name(gender=g) for g in dict_people['gender']]
peopleFrame = pd.DataFrame(dict_people)
# Example 1: Add new columns to the …Run Code Online (Sandbox Code Playgroud) 我有一个相当复杂的对象,
MyNamespace::MyClass::MySubStruct
Run Code Online (Sandbox Code Playgroud)
有一个
enum
{
ONE = 1,
TWO = 2
};
Run Code Online (Sandbox Code Playgroud)
现在我有另一个具有模板参数的类
template <unsigned int x> class Foo;
Run Code Online (Sandbox Code Playgroud)
目前我按如下方式初始化B.
Foo<MyNamespace::MyClass::MySubStruct::ONE> MyFoo
Run Code Online (Sandbox Code Playgroud)
这很好,但它有点太冗长,特别是考虑到我将这个类初始化了大约一百次.
我想写一些类似的东西:
typedef MyNamespace::MyClass::MySubStruct::ONE MyONE
Foo<MyOne> MyFoo
Run Code Online (Sandbox Code Playgroud)
当然,这不会编译,也不会将它声明为类中的const unsigned int.如何优雅地做到这一点?
我对Matlab很新,我觉得使用数组完全不知所措.在Matlab中最有效的以下C++代码实现是什么?
A = std::vector<double>();
for (int i = 0; i < 100; i++) {
if (complicatedBoolFunction(i)) {
A.push_back(i);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:效率我的意思是使用尽可能少的资源来增长阵列A - 也就是说,避免将其复制粘贴到临时内存中