我只是想知道我应该使用std::size_tfor循环和东西而不是int?例如:
#include <cstdint>
int main()
{
for (std::size_t i = 0; i < 10; ++i) {
// std::size_t OK here? Or should I use, say, unsigned int instead?
}
}Run Code Online (Sandbox Code Playgroud)
一般来说,何时使用的最佳做法是什么std::size_t?
有一种漂亮的方法可以根据几个属性对对象数组进行排序:
var data = _.sortBy(array_of_objects, ['type', 'name']);Run Code Online (Sandbox Code Playgroud)
但是,这仅适用于升序排序.是否有一些方便的方法来定义每列的方向?例如
var data = _.sortBy(array_of_objects, [{'type': 'asc'}, {'name': 'desc'}]);Run Code Online (Sandbox Code Playgroud) 我正在尝试实施加权随机数.我现在只是把头靠在墙上,无法解决这个问题.
在我的项目(德州扑克手牌范围,主观全权证券分析)中,我正在使用Boost的随机函数.所以,假设我想选择1到3之间的随机数(所以要么是1,2或3).Boost的mersenne twister发电机就像这样的魅力.但是,我希望选择加权,例如:
1 (weight: 90)
2 (weight: 56)
3 (weight: 4)Run Code Online (Sandbox Code Playgroud)
Boost是否具有某种功能?
干杯,
我知道你可以用下面的公式得到组合的数量(没有重复,顺序并不重要):
// Choose r from n n! / r!(n - r)!
但是,我不知道如何在C++中实现它,因为例如
n = 52 n! = 8,0658175170943878571660636856404e+67
即使是unsigned __int64(或unsigned long long),这个数字也太大了.是否有一些解决方法来实现公式而没有任何第三方"bigint" - 库?
我有一个64位整数,我需要在8 x 8区域内旋转90度(最好是直接位操作).我无法弄清楚任何方便的算法.例如,这个:
// 0xD000000000000000 = 1101000000000000000000000000000000000000000000000000000000000000
1 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
旋转后成为:
// 0x101000100000000 = 0000000100000001000000000000000100000000000000000000000000000000
0 0 …Run Code Online (Sandbox Code Playgroud) 如何在同一个表上更新后更新触发器中的表列?
这是触发器:
CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)
现在当我更新表格时
UPDATE products_score SET votes_1 = 5 WHERE id = 0
这不起作用,因为我得到以下内容:
#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
那么我怎么能让这个工作呢?
我刚刚在Javascript中遇到了关于预增量的'功能'.在我使用的所有其他语言中,它就像我认为的那样.例如在C++中:
#include <iostream>
int main()
{
int i = 0;
i += ++i;
std::cout << i << std::endl; // Outputs 2.
}Run Code Online (Sandbox Code Playgroud)
因此,++i不会复制变量,因此输出为2.
在PHP中相同:
<?php
$i = 0;
$i += ++$i;
echo $i; // Outputs 2.
Run Code Online (Sandbox Code Playgroud)
但是,在Javascript中:
var i = 0;
i += ++i;
console.log(i); // Outputs 1.
Run Code Online (Sandbox Code Playgroud)
所以它看起来像在Javascript中,它复制i并且不引用变量.这是故意的,如果是,为什么?
我无法弄清楚这一点.我需要一个抽象模板基类,如下所示:
template <class T> class Dendrite
{
public:
Dendrite()
{
}
virtual ~Dendrite()
{
}
virtual void Get(std::vector<T> &o) = 0;
protected:
std::vector<T> _data;
};
Run Code Online (Sandbox Code Playgroud)
现在,我从中得出了指定Dendrite的确切用法.
现在问题.
如何创建一个没有特定类型的基类指针向量,我想通过稍后向其推送元素来指定它?就像是:
class Foo
{
public:
...
private:
std::vector<Dendrite *> _inputs; //!< Unfortunately, this doesn't work...
//! Now I could later on push elements to this vector like
//!
//! _inputs.push_back(new DeriveFromDendrite<double>()) and
//! _inputs.push_back(new DeriveFromDendrite<int>()).
};
Run Code Online (Sandbox Code Playgroud)
这可能还是我错过了一些非常基本的东西?
在Windows环境中,使用WinAPI的关键部分或其他东西是Boost的作用域互斥体吗?