好吧,所以我写了一个类似stl的算法cartesian_product
.对于那些不知道的人来说,笛卡尔积是两组中每一对可能的元素.所以笛卡尔乘积{1, 2, 3}
和{10, 20, 30}
是
{(1,10), (1,20), (1,30), (2,10), (2,20), (2,30), (3,10), (3,20), (3,30)}
所以功能看起来像
template <typename InIt1, typename InIt2, typename OutIt>
void
cartesian_product(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt out)
{
for (; first1 != last1; ++first1)
for (InIt2 it = first2; it != last2; ++it)
*out++ = std::make_pair(*first1, *it);
}
Run Code Online (Sandbox Code Playgroud)
没有模板typedef,所以我创建了一个traits类来保存输出迭代器来自的类型:
template <typename ObjA, typename ObjB, template <typename> class Container>
struct cartesian_product_traits
{
typedef Container<std::pair<ObjA, ObjB> > type;
};
Run Code Online (Sandbox Code Playgroud)
那么我可以说: …
所以,我有一个宏.
// swap_specialize.hpp
#include <algorithm>
#ifndef STD_SWAP_SPECIALIZE
#define STD_SWAP_SPECIALIZE( CLASSNAME ) \
namespace std { \
template<> inline \
void swap( CLASSNAME & lhs, CLASSNAME & rhs ) \
{ lhs.swap(rhs); } }
#endif
Run Code Online (Sandbox Code Playgroud)
那我就上课了
// c.hpp
#include <vector>
#include "swap_specialize.hpp"
class C
{
public:
C();
void swap(C& rhs)
{
data_.swap(rhs.data_);
}
C& operator=(C rhs)
{
rhs.swap(*this);
return *this;
}
private:
std::vector<int> data_;
}
STD_SWAP_SPECIALIZE(C)
Run Code Online (Sandbox Code Playgroud)
这样做风格上是不好的?这是代码味道吗?或者这是一个好的做法?
我正在研究roguelike并使用GA来生成关卡.我的问题是,每一代GA都应该有多少级别?而且,它应该有多少代?最好是每代人都有几个级别,有很多代,或者反过来?
int LinkedList::DoStuff()
{
Node *Current = next_;
while ( Current != NULL )
{
Current = Current->next_;
length_++;
}
// At the last iteration we have reached the end/tail/last node
return length_;
}
Run Code Online (Sandbox Code Playgroud)
除了最后一个节点之外没有其他节点.我怎样才能穿过前端的尾端?
我有一些简单的Perl代码:
#!/usr/bin/perl
use strict; # not in the OP, recommended
use warnings; # not in the OP, recommended
my $val = 1;
for ( 1 .. 100 ) {
$val = ($val * $val + 1) % 8051;
print ($val / 8050) . " \n";
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,输出是:
bash-3.2$ perl ./rand.pl
0.0002484472049689440.000621118012422360.003229813664596270.08409937888198760.92
... <snipped for brevity> ...
2919250.9284472049689440.3526708074534160.1081987577639750.2295652173913040.1839
751552795030.433540372670807bash-3.2$
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
所以,我正在写一个C++程序,它可以让我控制整个世界.我写完了最后的翻译单元,但是我收到了一个错误:
error C3848: expression having type 'const `anonymous-namespace'::ElementAccumulator<T,BinaryFunction>' would lose some const-volatile qualifiers in order to call 'void `anonymous-namespace'::ElementAccumulator<T,BinaryFunction>::operator ()(const point::Point &,const int &)'
with
[
T=SideCounter,
BinaryFunction=std::plus<int>
]
c:\program files (x86)\microsoft visual studio 9.0\vc\include\functional(324) : while compiling class template member function 'void std::binder2nd<_Fn2>::operator ()(point::Point &) const'
with
[
_Fn2=`anonymous-namespace'::ElementAccumulator<SideCounter,std::plus<int>>
]
c:\users\****\documents\visual studio 2008\projects\TAKE_OVER_THE_WORLD\grid_divider.cpp(361) : see reference to class template instantiation 'std::binder2nd<_Fn2>' being compiled
with
[
_Fn2=`anonymous-namespace'::ElementAccumulator<SideCounter,std::plus<int>>
]
Run Code Online (Sandbox Code Playgroud)
我查看了它的规格,binder2nd
它是:它采用了const
AdaptibleBinaryFunction.
所以,我想,这不是什么大不了的事.我刚用过boost::bind
,对吧?
错误!现在我的接管世界程序需要很长时间才能编译(bind
在一个实例化很多的模板中使用)!按照这个速度,我的克星将首先接管世界!我不能让这种情况发生 - …
我有一个可变维度矩阵,X.我想要一个能在一个维度上得到X的前半部分的函数.IE,我想要这样的东西:
function x = variableSubmatrix(x, d)
if d == 1
switch ndims(x)
case 1
x = x(1:end/2);
case 2
x = x(1:end/2, :);
case 3
x = x(1:end/2, :, :);
(...)
end
elseif d == 2
switch ndims(x)
case 2
x = x(:, 1:end/2);
case 3
x = x(:, 1:end/2, :);
(...)
end
elseif (...)
end
end
Run Code Online (Sandbox Code Playgroud)
我不太清楚如何做到这一点.我需要快速,因为这将在计算中多次使用.
我正在尝试写一个非常简单的声明,如果我的博客的用户没有gravatar,将会出现随机选择的图像.它现在的工作方式是这样的:
<?php echo get_avatar( $comment, $size = '78', $default = '/images/noavatar2.gif' ); ?>
Run Code Online (Sandbox Code Playgroud)
这会给我一个随机数:
echo(rand(1,10)
Run Code Online (Sandbox Code Playgroud)
我想回应"noavatar"和".gif"之间的随机数,但我似乎无法弄清楚如何去做.任何帮助,将不胜感激.
我开始学习Java了.实际语言不是问题,因为我对C++非常有经验,但显然部署非常不同.我使用Netbeans 7.1.1并创建了一个JApplet并使用"web start"选项生成一个小测试网页.它在我的计算机上运行得非常好,但是在没有安装JDK的任何计算机上它根本不起作用.实际上,它会重定向到chrome上的JRE下载页面.有诀窍吗?我真的不知道我在这里做什么.
我可能需要发布更多信息,但老实说我不知道那是什么,所以如果我需要发布更多信息,请添加评论.
编辑:出于某种原因,它确实需要JDK.当然,我不会尝试在没有JRE的PC上运行它.在没有JDK的PC上试用它http://dl.dropbox.com/u/416909/wossname/wossname.html