小编kmi*_*las的帖子

如何直接将复数分配给变量?

使用complex类和库,如何为变量分配复数?

我知道我可以在第一次实例化复数时设置该值.
我也明白我可以将一个实例化的复数分配给另一个.
如何直接为变量分配复数?

参考:http:
//www.cplusplus.com/reference/complex/complex/operators/

例:

#include <iostream>
#include <complex>

int main() {
    complex<double> a(1.2,3.4), b;
    cout << a; //-> (1.2,3.4)
    b = a;
    cout << b; //-> (1.2,3.4)
    b = (1.2,3.4);
    cout << b; //-> (3.4,0) <-- what the heck is this??
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ variable-assignment complex-numbers

7
推荐指数
2
解决办法
4448
查看次数

如何从派生类调用重载的父cout友元类?

想象一下如下设置.如何cout从派生类中调用基类cout?我可以使用该getBrand()方法,但我觉得我应该能够直接访问基类的cout友元函数.

我砍了一下,然后尝试this.BrandBrand.没运气.

class Brand {
public:
    Brand(std::string brand):brand_(brand) {};
    friend std::ostream & operator << (std::ostream & out, const Brand & b) {
        out << b.brand_ << ' ';
        return out;
    }   
    std::string getBrand()const { return brand_; }     
private:
    std::string brand_;
}

class Cheese : public Brand {
public:
    Cheese(std::string brand, std::string type):Brand(brand), type_(type) {};
    friend std::ostream & operator << (std::ostream & out, const Cheese & c) {
        out …
Run Code Online (Sandbox Code Playgroud)

c++ oop inheritance class c++11

6
推荐指数
1
解决办法
111
查看次数

Safari Mobile:如何防止页面加载时出现白色闪烁?

当 HTML5 页面在 Safari 移动版中加载时,在内容显示之前会出现瞬间的白色闪烁。有点像旧的“无样式内容的闪光(FOUC)”问题,但有一个白色的屏幕......称之为“白色的闪光(FOW)”问题。

还有其他人看过这个吗?我怎样才能摆脱它?我已经尝试了我能想到的一切:

  1. 将正文背景颜色设置为黑色,位于我的第一个样式表的顶部。

  2. 将主体背景颜色设置为黑色,位于头部上方,并带有单独的样式标签:

    <style type="text/css">
        body {background: black }
    </style>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在我的绝望中,我什至诉诸于向标签添加(喘息!)内联样式:

    <body style="background: black">
    
    Run Code Online (Sandbox Code Playgroud)

似乎什么都不起作用...每次加载页面时,我都会看到令人讨厌的白色闪光。

有任何想法吗?谢谢,基思:^)

html safari mobile ios fouc

5
推荐指数
0
解决办法
814
查看次数

使用statusBarOrientation的iPad设备方向

(Xcode 6,Swift,iOS8)

我试图找到iPad的设备方向,它似乎是不必要的复杂.我哪里错了?

起初,我尝试使用interfaceOrientation我的UIViewController,但它不可靠并且已弃用.Apple建议使用statusBarOrientation属性.(1)

statusBarOrientation属于类型UIApplication.我试图像这样创建一个实例:

var o: UIApplication
Run Code Online (Sandbox Code Playgroud)

然后测试:

if (o.statusBarOrientation.isLandscape) { ... }
Run Code Online (Sandbox Code Playgroud)

但收到错误

Variable 'o' used before being initialized.
Run Code Online (Sandbox Code Playgroud)

这对我毫无意义.为什么我要将它初始化为一个值?我会覆盖我想要的价值!?

然后我尝试按照文档中的建议创建一个变量:

var statusBarOrientation: UIInterfaceOrientation
Run Code Online (Sandbox Code Playgroud)

但是跟踪:

statusBarOrientation =(UIKit.UIInterfaceOrientation)(0xa0)

所以我尝试将UIApplication子类化,并通过我的子类访问该属性.

从这里开始,更加复杂.似乎"每个应用程序必须只有一个UIApplication实例(或UIApplication的子类)." (2)

这导致我发表以下帖子,这似乎Main在Swift中创建了一个例程?!
使用Swift子类UIApplication

同样,我的目标是获取当前的设备方向,并在条件下进行测试.Pseudocoded:

var o = (get device orientation)
if (o == portrait ) { ... } else { ... }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我在杂草中...任何帮助都将非常感激.

编辑:我确实设法让它与以下工作类似:

var o = UIApplication.sharedApplication().statusBarOrientation;
if (o.isLandscape) { ...
Run Code Online (Sandbox Code Playgroud)

但是,在初始加载时,当设备旋转到横向时,o.isLandscape报告为false.

  1. 搜索"UIViewController"的文档,并查看"配置视图旋转设置".在Discussion"不要使用此属性来通知布局决策.而是使用UIApplication类参考中statusBarOrientation描述的属性 …

xcode orientation ipad ios swift

5
推荐指数
2
解决办法
5210
查看次数

为什么 Python 3 换行符 \n 不适用于打印和子进程 Popen 标准输出?

Python 3.7.1:grep使用 subprocess.Popen调用以从日志文件中提取错误。打印到屏幕时,不处理换行符 \n。

例子

a = subprocess.Popen(["grep", "ERR", "errors.log"], stdout=subprocess.PIPE)
print(a.stdout.read())
Run Code Online (Sandbox Code Playgroud)

输出

ERR 1 ret: 31113\nERR 2 ret: 35523\nERR 3 ret: 3810 (etc.)
Run Code Online (Sandbox Code Playgroud)

无法想象为什么不处理换行符。我希望这样:

ERR 1 ret: 31113
ERR 2 ret: 35523
ERR 3 ret: 3810
(etc.)
Run Code Online (Sandbox Code Playgroud)

一直在梳理“网络”以获得答案,但没有运气。谢谢:^)

参考:

我如何在 Python 中指定一个新行?
Python Popen grep

printing grep line-breaks carriage-return python-3.x

5
推荐指数
1
解决办法
1797
查看次数

如何手动删除类的实例?

如何手动删除类的实例?

例:

#include <iostream>
#include <cstring>

class Cheese {
private:
    string brand;
    float cost;
public:
    Cheese(); // Default constructor
    Cheese(string brand, float cost); // Parametrized constructor
    Cheese(const Cheese & rhs); // Copy construtor
    ~Cheese(); // Destructor
    // etc... other useful stuff follows
}

int main() {
    Cheese cheddar("Cabot Clothbound", 8.99);
    Cheese swiss("Jarlsberg", 4.99);

    whack swiss; 
    // fairly certain that "whack" is not a keyword,
    // but I am trying to make a point. Trash this instance!

    Cheese swiss("Gruyère",5.99);
    // re-instantiate …
Run Code Online (Sandbox Code Playgroud)

c++ destructor class kill instance

4
推荐指数
2
解决办法
2万
查看次数

如何在 DataDog 中的时间序列上设置线条颜色

我正在设置一个时间序列来监控标准级别的系统问题:严重、错误、警告等。我想将颜色设置如下:

  • 关键:红色
  • 错误:橙色
  • 警告:黄​​色

我似乎无法做到这一点。有一个颜色选择下拉菜单,但选项不是颜色......它们是主题,例如“经典”、“酷”、“暖”等。

如何设置时间序列中线条的颜色?

datadog

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

如何在类构造函数中创建新值并分配给私有unique_ptr?

如何在类的构造函数中创建new并为私有unique_ptr赋值?Tyvm:^)基思

我尽力而为:

#include <iostream>
#include <memory>

class A {
public:
    A() {};
    A(int);
    void print();
private:
    std::unique_ptr<int> int_ptr_;
};
A::A(int a) {
    int_ptr_ = new int(a);
}
void A::print() {
    std::cout << *int_ptr_ << std::endl;
}
int main() {
    A a(10);
    a.print();
    std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译结果:

smartPointer2.1.cpp:13:11: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<int>’ and ‘int*’)
  int_ptr_ = new int(a);
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers unique-ptr c++11

3
推荐指数
1
解决办法
1979
查看次数

创建一个shared_ptr向量的向量

尝试创建shared_ptr到int的向量.

我哪里错了?谢谢.基思:^)

#include <iostream>
#include <vector>
#include <memory>

int main() {
    std::vector<std::shared_ptr<int> > w;
    std::vector<std::shared_ptr<int> >::iterator it_w;
    w.push_back(new int(7));

    std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译结果:

pickledegg> g++ -std=c++11 -o shared_ptr shared_ptr.cpp
shared_ptr.cpp:29:4: error: no matching member function for call to 'push_back'
        w.push_back(new int(7));
        ~~^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:697:36: note: 
      candidate function not viable: no known conversion from 'int *' to 'const value_type' (aka
      'const std::__1::shared_ptr<int>') for 1st argument
    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
                                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:699:36: note: 
      candidate function not viable: no known conversion from 'int …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers shared-ptr c++11

3
推荐指数
1
解决办法
1917
查看次数

删除链表中的节点 - 是否需要任何形式的垃圾收集?

[Python 3.8]

删除链表中的next节点时,是否可以简单地更改节点?

在这里,我们“删除”节点 1,只需更改指针即可。

来自 C++ 世界,这让我有点紧张。由于没有对节点 1 的引用,节点 1 的内存会自动回收吗?节点 1 到底发生了什么?

[Sentinel] -> [Node 0] -> [Node 1] -> [Node 2] -> [Node 3] -> None
Run Code Online (Sandbox Code Playgroud)

[Sentinel] -> [Node 0] -? [Node 1] -?-> [Node 2] -> [Node 3] -> None
                        ?-----------?
Run Code Online (Sandbox Code Playgroud)

这是合法的吗?

最小的、完整的、可验证的例子

def delete(self, val):
    n = self.sentinel
    while n.next != None:
        if n.next.data == val:
            n.next = n.next.next  # reassign pointer - no del, free, delete, or the like.
            return …
Run Code Online (Sandbox Code Playgroud)

python linked-list python-3.x

3
推荐指数
1
解决办法
196
查看次数