小编Gis*_*wok的帖子

c ++中隐式构造的变量

我正在掌握c ++并且有一种语言功能我特别难以理解.

我习惯于明确地声明和初始化变量,但在c ++中,我们有时似乎声明并隐式地构造变量.

例如,在这个片段中,rdev似乎是隐式构造的(因为它随后用于构造default_random_engine);

random_device rdev;
default_random_engine gen(rdev());
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这里发生了什么吗?除了一个简单的声明之外,我该怎么说int myInt;呢?

c++

9
推荐指数
3
解决办法
625
查看次数

在 R ggplot 中控制矩形 geom_ribbon

我正在使用 ggplot,并试图将一个简单矩形形式的功能区添加到我拥有的条形图中。这个想法是显示低于某个值的截止值。

条形图很好,但我不能完全正确地使用功能区 - 我希望它显示得更宽一些,但它似乎仅限于条形图数据的宽度。

我尝试使用xminandxmax但这不会增加阴影区域的宽度。

有没有办法明确控制 的宽度geom_ribbon

# Where df is a data frame containing the data to plot
library(cowplot)

ggplot(df, aes(x=treatments, y=propNotEliminated)) +
  geom_ribbon(aes(xmin=0, xmax=21, ymin=0, ymax=20)) +  # the xmin and xmax don't do what I'd expect
  geom_bar(stat="identity", fill="white", colour="black", size=1) +
  theme_cowplot()
Run Code Online (Sandbox Code Playgroud)

结果图

r ggplot2

4
推荐指数
1
解决办法
785
查看次数

在C++中转换通过引用传递的变量

我有一些代码通过引用传递变量,但不会导致变量在调用代码中更新,正如我所期望的那样;

// Interface classes
class Animal{};

class Car{
public:
    virtual void testDrive(Animal &animal) = 0;
};


// A specific implementation
class Bear : public Animal{
public:
    int testthing = 0;
};

void Ferrari::testDrive(Animal &animal){
    Bear b = dynamic_cast<Bear &>(animal);

    b.testthing = 1;
}


// Use those classes, doesn't need to know about Bear or Ferrari
int main()
{
    // Set up myCar and myAnimal

    myCar.testDrive(myAnimal)  // ** but myAnimal is unchanged! **
}
Run Code Online (Sandbox Code Playgroud)

我实际上已经能够通过传递指针来使这个工作(myAnimal更新testthing = 1),但我有兴趣知道这里发生了什么. …

c++

-4
推荐指数
1
解决办法
658
查看次数

标签 统计

c++ ×2

ggplot2 ×1

r ×1