我想知道我目前正在做的是C++的耻辱,或者它是否正常.
我致力于计算目的的代码.对于某些类,我使用具有虚拟/多态的正常继承方案.但我需要一些类来进行密集计算,并且避免由于虚拟性而产生的开销会很好.
基本上,我想使用没有指针或重定向的这个类:继承只是为了避免许多代码的复制/粘贴(基类的文件大小就像60Ko(这是很多代码)).所以没有虚函数,也没有虚拟析构函数.
我想知道从C++的角度来看它是否完全可以,或者它是否可以产生副作用(相关的类将在程序中使用很多).
非常感谢你.
传统上,要在python中读取填充数组的文件,我使用以下语法
x, y, z = loadtxt("myfile.txt", unpack=True)
Run Code Online (Sandbox Code Playgroud)
它适用于单阵列文件.
现在,我有一个更复杂的文件:
1.5 3.5 2.5 1.6
4
3
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 2
3 4
5 6
Run Code Online (Sandbox Code Playgroud)
我想做的是以下事情:
1.5 3.5 2.5 1.6 - >我想把它们放在三个变量+ 1个标量的数组中
4 - > A = 4,我的第一个数组的行数
3 - > B = 3,我的第二个数组的行数
我的第一个数组A = 4行,我想加载5个变量(比如命令loadtxt("",unpack = True)
1 2 3 4 5
6 7 8 9 10
11 12 13 14 …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的构造函数:
class MyClass
{
template<class TI> MyClass(TI first, TI last);
};
template<class TI> MyClass::MyClass(TI first, TI last)
{
;
}
Run Code Online (Sandbox Code Playgroud)
我想只在TI是一个迭代器时启用这个构造函数(这意味着我认为TI有一个iterator_category).如何在C++ 2011中使用enable_if作为默认模板参数(在声明和定义中)?
非常感谢你.
我有一个类型Type和一个变量tmp:
template<typename Type> myFunction()
{
/* SOMETHING */ tmp = 0;
};
Run Code Online (Sandbox Code Playgroud)
我想声明tmp的Type,如果Type是一个浮点类型和double是否Type为一个整数类型.如何在C++ 11中做到这一点?
以下内容根据标准完善:
// 1 : Deduced template parameter in the normal order
template<typename T, typename T1 = typename std::conditional<std::is_fundamental<T>::value, T, int>::type>
void f(T x);
// 2 : Deduced template parameter in the inverted order
template<typename T1 = typename std::conditional<std::is_fundamental<T>::value, T, int>::type, typename T>
void f(T x);
// 3 : Deduced template parameter before a function pointer
template<typename T>
void f(T x, void(*g)(int, int, T*));
// 4 : Deduced template parameter after a function pointer
template<typename T>
void f(void(*g)(int, int, T*), T …Run Code Online (Sandbox Code Playgroud) 在下面的 :
template<typename Type>
struct MyClass
{
template<typename OtherType> MyClass(const MyClass<OtherType>& x);
template<typename OtherType = Type> void test(const MyClass<OtherType>& x);
};
Run Code Online (Sandbox Code Playgroud)
在函数中,test之间做了什么:
情况1:默认参数优先级:隐式调用转换构造函数MyClass<Type>(const MyClass<OtherType>& x)并被MyClass<Type>::test<Type>(const MyClass<Type>& x)调用。
情况2:推导出的参数是priority:MyClass<Type>::test<Type>(const MyClass<OtherType>& x)被调用。
我认为最好的答案是第二个,但我不确定。您能否向我确认这一点(并且这种情况已由标准明确定义)?
编辑:测试函数由以下方式调用:
MyClass<double> d;
MyClass<unsigned int> ui;
d.test(ui); // <- So the question is : is ui implicitely
// converted to MyClass<double> or not ?
Run Code Online (Sandbox Code Playgroud) c++ templates standards-compliance c++11 template-argument-deduction
考虑以下形式的收敛系列:
sum(((-1)^n)*something)
Run Code Online (Sandbox Code Playgroud)
n迭代索引在哪里(n从哪里1开始infinity).
如果我们实现direclty公式,我们std::pow(-1, n)有一个更"快速"的算法技巧来实现它?
我已经阅读了几个关于用C++显示浮点数的主题,我找不到令人满意的答案.
我的问题是: 如何以科学的格式(尾数/指数)显示C++中浮点数的所有有效数字?
问题是所有数字在基数10中没有相同的有效位数.
例如,a double具有15到17个有效十进制数字精度,但std::numeric_limits<double>::digits10返回15,因此,对于某些数字,我将丢失2个额外十进制数字的精度.
c++ floating-point iostream iomanip floating-point-precision
要制作自定义图例,我目前使用以下内容:
handles, labels = plt.gca().get_legend_handles_labels()
my_artist = plt.Line2D((0,1),(0,0), color = "blue", linestyle = "-", linewidth = 1)
plt.legend([handle for i,handle in enumerate(handles) if i in display]+[my_artist],
[label for i,label in enumerate(labels) if i in display]+["My legend"])
Run Code Online (Sandbox Code Playgroud)
它将在图例框中绘制一条蓝线。我想要一个蓝色的小方块而不是一条线(但比简单的标记大)。怎么做 ?
我目前有这个函数将无符号整数转换为字符串(我需要一个适用于非标准类型的函数__uint128_t):
#include <iostream>
#include <algorithm>
#include <string>
template <typename UnsignedIntegral>
std::string stringify(UnsignedIntegral number, const unsigned int base)
{
static const char zero = '0';
static const char alpha = 'A';
static const char ten = 10;
std::string result;
char remainder = 0;
do {
remainder = number%base;
result += (remainder < ten) ? (zero+remainder) : (alpha+remainder-ten);
number /= base;
} while (number > 0);
std::reverse(std::begin(result), std::end(result));
return result;
}
int main()
{
std::cout<<stringify(126349823, 2)<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法优化这段代码?
c++ ×8
c++11 ×4
templates ×3
optimization ×2
python ×2
arrays ×1
enable-if ×1
file ×1
integer ×1
iomanip ×1
iostream ×1
legend ×1
math ×1
matplotlib ×1
numpy ×1
plot ×1
polymorphism ×1
sfinae ×1
string ×1
template-argument-deduction ×1
type-traits ×1
virtual ×1