小编Vin*_*ent的帖子

Python:读取二进制文件中的数组

我目前正在尝试使用以下技术读取python的fortran文件

with open(myfile, "rb") as f:
    for i in range (0, n):
        s = struct.unpack('=f', f.read(4))
        mylist.append(s[0])
Run Code Online (Sandbox Code Playgroud)

但是对于大型阵列来说它非常慢.有没有办法一次读取整个循环的内容并将其放到mylist中以避免逐个转换/追加每个项目?

非常感谢你.

python arrays binaryfiles

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

C++转换为更精确的类型并失去准确性?

考虑两种计​​算方式:

  1. 双精度数据
    - >应用具有双精度临时值的函数
    - >返回结果
  2. 双精度数据
    - >强制转换为long double
    - >应用具有长双精度临时值的函数
    - >强制转换为double
    - >返回结果

与第一个解决方案相比,第二个解决方案能否提供不太准确的结果?如果是,在什么情况下是?

c++ floating-point precision

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

QWidget透明背景(但不是孩子)

我有一个 QWidget,其布局中包含 aQPixmap和 a 。QComboxBox我想将小部件的背景设置为透明(但我想正常显示 和QPixmapQComboBox。我怎么做?

qt background qwidget

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

根据类型返回值

请考虑以下示例

template<class Type = void> class MyClass
{
    public:
        double getValue()
        {
            // if "Type == void" return _x, if "Type != void" return _y
            return (/* SOMETHING */) ? (_x) : (_y);
        }
    protected:
        double _x;
        static const double _y;
}; 
Run Code Online (Sandbox Code Playgroud)

可能是什么/* SOMETHING */条件?

我想_x在模板参数为void时返回,_y如果没有则返回.怎么做 ?

c++ templates type-traits c++11

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

eof位:读取失败之前或之后?

我目前正在为一些IO操作编写一个类.某些函数返回IO操作是否成功.如果我正在读取文件,我想知道是否应该返回std::ifstream::good()!std::ifstream::fail()指示IO操作是否成功.

差异来自于这eof一点,我不确定我是否正确理解它.

假设我有一个包含4个字节的二进制文件(1个整数).

假设我读了这个整数.

我的问题是:eof在此操作之后或下一次IO操作之后将设置标志(将失败)?

如果在此操作之后直接设置,如果我的读取函数返回std::ifstream::good(),则结果将是false(但是正确读取了整数).

你可以在设置eof位时向我解释一下,在我的函数结束时应该返回什么?

c++ io stream ifstream eof

3
推荐指数
2
解决办法
2723
查看次数

函数指针的类型(f)(类型)和类型(*f)(类型)之间的区别?

这两个函数原型有什么区别?

void apply1(double(f)(double));
void apply2(double(*f)(double));
Run Code Online (Sandbox Code Playgroud)

如果目标是将提供的函数应用于数组,那么与其他版本相比,版本是否更快?

编辑:实施的一个例子:

#include <iostream>
#include <vector>
#include <cmath>

// First version
template<typename Type> void apply1(std::vector<Type>& v, Type(f)(Type))
{
    for (unsigned int i = 0; i < v.size(); ++i) {
        v[i] = f(v[i]);
    }
}

// Second version
template<typename Type> void apply2(std::vector<Type>& v, Type(*f)(Type))
{
    for (unsigned int i = 0; i < v.size(); ++i) {
        v[i] = f(v[i]);
    }
}

// Main
int main()
{
   std::vector<double> v = {1., 2., 3., 4., 5.};
   apply1(v, std::sin); …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers

3
推荐指数
2
解决办法
500
查看次数

构造和初始化列表:编译器做什么?

我对C++中的构造函数有一些疑问.对于每个问题(从(1)到(4)),我想知道行为是否完全定义了标准.

A)第一个是关于成员的初始化:

class Class
{
    public:
        Class() 
        : _x(0)
        , _y(_x)
        , _z(_y) // <- (1) Can I initialize a member with other members ?
        {
            ;
        }

    protected:
        int _x;
        int _y;
        int _z;
};
Run Code Online (Sandbox Code Playgroud)

B)编译器为每个类添加了哪些函数?

template<typename T> class Class
{
    public:
        template<typename T0>
        Class(const Class<T0>& other)
        {
            std::cout<<"My copy constructor"<<std::endl;
            _x = other._x;   
        }

        template<typename T0 = T>
        Class (const T0 var = 0)
        {
            std::cout<<"My normal constructor"<<std::endl;
            _x = var;
        }

    public:
        T _x;
};

// (2) …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-construction constructor standards-compliance c++11

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

将函数作为参数传递的好习惯:复制,引用,const引用?

可能重复:
模板传递值或const引用或...?

对于将函数作为参数的函数,下面的优良做法是什么:

template<class Function> void test1(Function f);
template<class Function> void test2(Function& f);
template<class Function> void test3(const Function& f);
Run Code Online (Sandbox Code Playgroud)

其中传递的函数可以是仿函数,std ::函数,函数指针或lambda函数.

c++ lambda functor c++11

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

在C++中获取运算符的返回类型?

考虑以下功能:

template<class T1, class T2, class T3 = /* SOMETHING */> 
T3 f(const T1& x, const T2& y);
Run Code Online (Sandbox Code Playgroud)

我希望T3等于返回类型T1+T2.如何用C++ 11做到这一点?

注意:我不想要结果std::common_type<T1, T2>::type,我想要真正的类型T1+T2,考虑到operator+可以是非成员函数或者可以是成员函数T1.

c++ return-type type-traits operator-keyword c++11

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

默认模板类型可以是通用引用吗?

在下面,是&&一个普遍的参考?

template <class Function = std::greater<int> > void f(Function&& f = Function());
Run Code Online (Sandbox Code Playgroud)

c++ templates reference c++11 universal-reference

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