我用C++ 11很好的新生成器和发行版生成随机值.在一个函数中,它就像一个魅力,看起来像这样:
void foo() {
mt19937 generator;
uniform_int_distribution<unsigned> distribution;
auto dice = bind(distribution, generator);
// dice() will now give a random unsigned value
}
Run Code Online (Sandbox Code Playgroud)
但是,如何将所有三个对象作为数据成员放在一个类中呢?我可以简单地编写generator和distribution作为数据成员,但是如何在dice不知道(或想知道)其确切类型的情况下创建数据成员?这令人惊讶
class X {
mt19937 generator;
uniform_int_distribution<unsigned> distribution;
decltype(bind(distribution, generator)) dice;
};
Run Code Online (Sandbox Code Playgroud)
error C2660: 'bind' : function does not take 2 arguments在Visual Studio 2013中产生错误.
我想找出在 Linux 上用 C++ 编写的函数的执行时间。我发现了很多与此相关的帖子。我尝试了此链接计时器方法中提到的所有方法来计算时间。以下是我的函数的执行时间结果:
time() : 0 seconds
clock() : 0.01 seconds
gettimeofday() : 0.002869 seconds
rdtsc() : 0.00262336 seconds
clock_gettime() : 0.00672151 seconds
chrono : 0.002841 seconds
Run Code Online (Sandbox Code Playgroud)
请帮助我哪种方法的读数可靠,因为所有结果的读数都不同。我读到您的操作系统正在不同的任务之间切换,因此不能期望读数非常准确。有没有一种方法可以计算 CPU 在我的函数上花费的时间。我听说过分析工具的使用,但还没有找到任何函数的示例。请指导我。
警告:
src/BoardRep.h:49:12: warning: ‘BoardRep::BoardRep::Row::<anonymous struct>::a’
is too small to hold all values of ‘enum class BoardRep::Piece’
[enabled by default]
Piece a:2;
^
Run Code Online (Sandbox Code Playgroud)
枚举:
enum class Piece: unsigned char {
EMPTY,
WHITE,
BLACK
};
Run Code Online (Sandbox Code Playgroud)
使用:
union Row {
struct {
Piece a:2;
Piece b:2;
Piece c:2;
Piece d:2;
Piece e:2;
Piece f:2;
Piece g:2;
Piece h:2;
};
unsigned short raw;
};
Run Code Online (Sandbox Code Playgroud)
如果enum我同意GCC,它可能不得不截断,但这是因为enums与整数和预处理器定义并没有真正分开.然而,一个enum class更强大.如果它不够强大,不能假设所有Piece取整数值都在0到2之间,则警告是有意义的.否则海湾合作委员会将不必要地挑剔,可能值得邮寄清单说"看,这是一个愚蠢的警告"
您可以在2位数据中存储4个不同的值,我只需要3个不同的值,因此任何长度为4或更小的枚举应该很好地适合给定的2位(并且我的枚举确实"派生"(更好的术语?)来自无符号类型).如果我有5个或更多那么我会期待一个警告.
我试图了解 R 中的 s3 类系统。
文档说我需要为我想要创建的方法创建一个通用函数。
假设我想foo为 class创建一个方法XYZ。
有了 R 的所有包,我怎么能确定我没有覆盖以前创建的泛型方法?
总结是一个不好的例子,因为每个人都可能知道它已经存在,但是我的泛型可能已经定义在我已经加载或我将加载的包中。
假设我的数据是两列,一列是“Condition”,一列是“Stars”
food <- data.frame(Condition = c("A", "B", "A", "B", "A"), Stars=c('good','meh','meh','meh','good'))
Run Code Online (Sandbox Code Playgroud)
如何制作按“条件”分组的“星”频率的条形图?
现在我有
q <- ggplot(food, aes(x=Stars))
q + geom_bar(aes(y=..count../sum(..count..)))
Run Code Online (Sandbox Code Playgroud)
如何制作由“条件”分组的四个条形图?
例如。“条件 A”的“好”为 0.66,“Meh”为 0.33
我有一个如下形式的数据框:
df = {'col_1': [5,4,np.nan,np.nan,1,0,1,2,np.nan,np.nan,5],
'col_2': [5,4,3,2,np.nan,np.nan,np.nan,np.nan,3,4,5]}
df = pd.DataFrame(df)
Run Code Online (Sandbox Code Playgroud)
我想“插值”但在任一侧取最小值以获得所需的结果:
df_desired = {'col_1': [5,4,1,1,1,0,1,2,2,2,5],
'col_2': [5,4,3,2,2,2,2,2,3,4,5]}
df_desired = pd.DataFrame(df_desired)
Run Code Online (Sandbox Code Playgroud)
有谁知道这样做的好方法吗?谢谢!
以这个 Lambda 为例:
final List<String> badKeys = pivMap.entrySet().stream()
.filter(entry -> StringUtils.trimToNull(entry.getValue()) == null || entry.getValue().equals("{}") || entry.getValue().equals("{ }"))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我们要确保 Lambda 变量上有一个显式类型:
final List<String> badKeys = pivMap.entrySet().stream()
.filter((final Map.Entry<String, String> entry) -> StringUtils.trimToNull(entry.getValue()) == null || entry.getValue().equals("{}") || entry.getValue().equals("{ }"))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
有没有办法使用 puppycrawl checkstyle 来检查上面的 lambda 表达式是否存在类型?在这种情况下,变量的类型声明是:(final Map.Entry<String, String> entry)
昨天我问了这个问题(有一些很好的答案),它非常相似,但与我现在遇到的问题略有不同。假设我有以下内容pd.DataFrame(dict):
eff_timestamp val id begin_timestamp end_timestamp
0 2021-01-01 00:00:00 -0.710230 1 2021-01-01 02:00:00 2021-01-01 05:30:00
1 2021-01-01 01:00:00 0.121464 1 2021-01-01 02:00:00 2021-01-01 05:30:00
2 2021-01-01 02:00:00 -0.156328 1 2021-01-01 02:00:00 2021-01-01 05:30:00
3 2021-01-01 03:00:00 0.788685 1 2021-01-01 02:00:00 2021-01-01 05:30:00
4 2021-01-01 04:00:00 0.505210 1 2021-01-01 02:00:00 2021-01-01 05:30:00
5 2021-01-01 05:00:00 -0.738344 1 2021-01-01 02:00:00 2021-01-01 05:30:00
6 2021-01-01 06:00:00 0.266910 1 2021-01-01 02:00:00 2021-01-01 05:30:00
7 2021-01-01 07:00:00 …Run Code Online (Sandbox Code Playgroud) 在std::ranges::transformGCC的stdlibc++的实现中,为什么for循环迭代器增量有强制转换(void)?
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2, ++__result)
Run Code Online (Sandbox Code Playgroud) 有谁知道如何绘制与其相邻的缩放区域的地图,类似于:
可以在这里找到一个接近的答案:
但该解决方案不显示哪个区域被放大!我想要像上图这样的东西,其中缩放区域也由那些黄线指示。这在 R 中可能吗?最好是使用 ggplot?