该克隆模式被用来做派生类的副本,而不拆毁到基类.不幸的是,clone必须在每个子类中实现(或者使用带有CRTP的mixin).
C++ 11是否有可能decltype使其不必要?
我不认为下面的代码实际上是复制original,而只是指向它的引用.当我试图使用时new decltype(*original),我收到一个错误:
error: new cannot be applied to a reference type.
是clone仍然走在C++ 11的方式吗?或者是否有一些新方法使用RTTI从基类指针复制派生类对象?
#include <iostream>
struct Base
{
virtual void print()
{
std::cout << "Base" << std::endl;
}
};
struct Derived : public Base
{
int val;
Derived() {val=0;}
Derived(int val_param): val(val_param) {}
virtual void print()
{
std::cout << "Derived " << val << std::endl;
}
};
int main() {
Base * original = …Run Code Online (Sandbox Code Playgroud) 在您看来,在C++ 11中打印std::cout使用std::ostream_iterator并避免打印尾随分隔符时,最优雅的方法是什么?
我正在打印的对象具有双向迭代器,但不是随机访问迭代器.
std::list<double> x{1,2,3,4,5,6};
std::copy(x.begin(), std::prev(x.end()),
std::ostream_iterator<int>(std::cout, ",") );
if ( x.size() != 0 )
std::cout << *(--x.end()) << std::endl;
Run Code Online (Sandbox Code Playgroud) 我想在LaTeX中将文本与大图像的中心对齐.不幸的是,文本some text与图像的底部对齐:
\begin{tabular}{cc}
some text & \includegraphics{image_name.eps}
\end{tabular}
Run Code Online (Sandbox Code Playgroud)
我发现一个网站建议使用m{width of the cell}而不是表中c的那一列,但它没有用.
\begin{tabular}{m{1in}c}
some text & \includegraphics{image_name.eps}
\end{tabular}
Run Code Online (Sandbox Code Playgroud)
我相信乳胶专业人员会看到这个并知道该怎么做!这意味着在你教我之前我不能成为一名LaTeX专业人士......
以下代码给出了编译器错误(运行gcc-4.7 -std=c++11):
#include <iostream>
#include <array>
template <typename T, int N>
std::ostream & operator <<(std::ostream & os, const std::array<T, N> & arr) {
int i;
for (i=0; i<N-1; ++i)
os << arr[i] << " ";
os << arr[i];
return os;
}
int main() {
std::array<double, 2> lower{1.0, 1.0};
std::cout << lower << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
tmp6.cpp:在函数'int main()'中:tmp6.cpp:16:16:错误:无法将
'std :: ostream {aka std :: basic_ostream}'左值绑定到
'std :: basic_ostream &&'在包含的文件中
/usr/include/c++/4.7/iostream:40:0,
来自tmp6.cpp:1:/usr/include/c++/4.7/ostream:600:5:错误:初始化'std :: basic_ostream <_CharT的参数1 ,
_Traits>&std :: operator …
是否存在与此python语句等效的C++ 11:
x, y, z = three_value_array
Run Code Online (Sandbox Code Playgroud)
在C++中,你可以这样做:
double x, y, z;
std::array<double, 3> three_value_array;
// assign values to three_value_array
x = three_value_array[0];
y = three_value_array[1];
z = three_value_array[2];
Run Code Online (Sandbox Code Playgroud)
在C++ 11中有更简洁的方法来实现这一点吗?
<math.h>log(1+x)为doubles的计算提供了更准确的方法.
是否有类似的精确计算方式log(1-x)?
我问的原因是因为我试图在对数空间中做一些工作以获得更高的精度(我主要是乘法和求和数非常接近于零).我发现编写一个log( exp(log_of_a) + exp(log_of_b) ) = log( a + b )使用的函数很容易log1p.我试图为差异做一个类似的功能:
log( exp(log_of_a) - exp(log_of_b) ) = log( a - b )
a > b当然,在哪里.
从本质上讲,只要没有log_a或log_b == -inf,函数会简单的返回:
return log( 1 - exp(log_b-log_a) ) + log_a;
Run Code Online (Sandbox Code Playgroud)
在我的log_add功能中,我最终得到了一个log( 1 + ... ),所以我使用了log1p.但我在这里log( 1 - ... ).为了以防万一,我甚至用谷歌搜索log1m,但没有运气......
当参数x在范围内时[-inf, 1),我可以简单地使用log1p(-x)(给出我的断言a > …
这是C++中一个简单的双容器zip函数:
template <typename A, typename B>
std::list<std::pair<A, B> > simple_zip(const std::list<A> & lhs,
const std::list<B> & rhs)
{
std::list<std::pair<A, B> > result;
for (std::pair<typename std::list<A>::const_iterator,
typename std::list<B>::const_iterator> iter
=
std::pair<typename std::list<A>::const_iterator,
typename std::list<B>::const_iterator>(lhs.cbegin(),
rhs.cbegin());
iter.first != lhs.end() && iter.second != rhs.end();
++iter.first, ++iter.second)
{
result.push_back( std::pair<A, B>(*iter.first, *iter.second) );
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
如何将此扩展到具有可变参数模板的任意数量的容器?
我想general_zip接受tuple的listS(每个列表可以包含不同类型的),并返回list的tuple秒.
鉴于log(a)和log(b),我想计算log(a+b)(以数字稳定的方式).
我为此写了一个小函数:
def log_add(logA,logB):
if logA == log(0):
return logB
if logA<logB:
return log_add(logB,logA)
return log( 1 + math.exp(logB-logA) ) + logA
Run Code Online (Sandbox Code Playgroud)
我写了一个程序,这是迄今为止最耗时的代码.显然我可以尝试优化它(例如,消除递归调用).
你知道从和计算的标准math或numpy功能吗?log(a+b)log(a)log(b)
如果没有,您是否知道为此函数创建单个C++挂钩的简单方法?它不是一个复杂的函数(它使用浮点数),正如我所说,它占据了我运行时的大部分时间.
在此先感谢,数值方法忍者!
这是一个用于迭代多维数值范围的简单类:
#include <array>
#include <limits>
template <int N>
class NumericRange
{
public:
// typedef std::vector<double>::const_iterator const_iterator;
NumericRange() {
_lower.fill(std::numeric_limits<double>::quiet_NaN());
_upper.fill(std::numeric_limits<double>::quiet_NaN());
_delta.fill(std::numeric_limits<double>::quiet_NaN());
}
NumericRange(const std::array<double, N> & lower, const std::array<double, N> & upper, const std::array<double, N> & delta):
_lower(lower), _upper(upper), _delta(delta) {
_state.fill(std::numeric_limits<double>::quiet_NaN());
_next_index_to_advance = 0;
}
const std::array<double, N> & get_state() const {
return _state;
}
void start() {
_state = _lower;
}
bool in_range(int index_to_advance = N-1) const {
return ( _state[ index_to_advance ] - _upper[ index_to_advance ] ) …Run Code Online (Sandbox Code Playgroud) 在第二个到最后一个元素之后执行循环并停止的最优雅方式是什么(在C++ 11中)?
注意:我的意思是双向迭代器; 随机访问迭代器是一个微不足道的特例,当然,因为它们有+和-运算符.
std::list<double> x{1,2,3,4,5,6};
for (auto iter = x.begin(); iter != x.end(); ++iter) {
auto iter2 = iter;
++iter2;
if (iter2 == x.end()) break;
std::cout << *iter << std::endl;
}
Run Code Online (Sandbox Code Playgroud)