小编Jav*_*ier的帖子

从文件生成直方图

如果输入文件每行包含一个单独的数字,我怎样才能计算该文件中项目发生的次数?

cat input.txt
1
2
1
3
1
0
Run Code Online (Sandbox Code Playgroud)

期望的输出(=> [1,3,1,1]):

cat output.txt
0 1
1 3
2 1
3 1
Run Code Online (Sandbox Code Playgroud)

如果解决方案也可以扩展为浮动数字,那将会很棒.

bash shell awk file

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

模板可能不是'虚拟'

鉴于下面的代码,编译器显示一条指向该代码的消息error: templates may not be ‘virtual’.有没有人有关于如何解决这个bug的建议?

template < class FOO_TYPE>
class CFoo{
    public:
        ...
        template < class BAR_TYPE >
        virtual void doSomething( const CBar<BAR_TYPE> &); // here's the error
        ...
        virtual ~CFoo();
    protected:
        MyClass < FOO_TYPE > * m_pClass;
};

template < class FOO_TYPE >
template < class BAR_TYPE >
void CFoo<FOO_TYPE>::doSomething( const CBar<BAR_TYPE> & refBar ){
    ...
}
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism virtual templates

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

如何在MATLAB中调整三维条形分组和y轴标记?

我有一个像这样的3D图:

替代文字

在图的y轴上,每组三个条指的是相同的参数:x1,x2,x3.我想在每个三个条形组的y轴上有一个间距,这样就可以更清楚地看出这些条形是指相同的参数.同时,我想在每个三个条形组的y轴上放置一个标签.例如,需要以下y轴的标签布局:

x1 x2 x3   x1 x2 x3   x1 x2 x3
  grid1     grid2      grid3
Run Code Online (Sandbox Code Playgroud)

任何建议都非常欢迎!我用来绘制条形图的代码如下:

Z = rand(9,5);
h = bar3(Z);
[r c] = size(Z);

zdata = [];
for i = 1:c
    zdata = [];
    for j = 1:r
        zdata = [zdata; ones(6,4)*Z(j,i)];
    end
set(h(i),'Cdata',zdata)
end
colormap
colorbar
set(gca,'YTickLabel',['x1';'x2';'x3';'x1';'x2';'x3';'x1';'x2';'x3']);
view([-64 44]);
Run Code Online (Sandbox Code Playgroud)

matlab plot label bar-chart

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

从非模板参数化方法返回模板类型

要定义模板化类,我考虑三个不同的文件.声明位于.h文件中,方法实现位于.cpp文件中,显式实例化包含在.inc文件中(通过在.cpp末尾添加一行,例如:)#include "bar-impl.inc".

现在,这是我的问题.我有两个模板类,比如说:Foo<S>Bar<T>.在Bar<T>类的内部,我有一个返回模板类型的方法FooType*(例如,我希望它具有我想要的显式实例化Foo<float>*)

template<class T>
class Bar{
 ....
 template <class FooType>
 FooType* doSomething(int);
 ....
};
Run Code Online (Sandbox Code Playgroud)

由于编译器不知道是什么FooType*,我试图doSomethingbar-impl.inc文件中显式实例化该方法.

//bar-impl.inc
template class Foo<float> * Bar<float>::doSomething(int);
Run Code Online (Sandbox Code Playgroud)

但是,它没有用,我得到一个错误: no matching function for call to ‘Bar<float>::doSomething(int&)’ make: *** [main] Error 1

有人知道是否可以这样做?

c++ templates

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

非均匀分布随机数组

我需要在[0,1]之间生成随机浮点数的向量,使得它们的和等于1并且非均匀地分布.是否有任何python函数生成这样的向量?

最好的祝愿

python random

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

从列表中逐步删除元素

我有一个浮点数列表,我想在给定的索引范围内逐步删除一组元素,sth.喜欢:

for j in range(beginIndex, endIndex+1):
   print ("remove [%d] => val: %g" % (j, myList[j]))
   del myList[j]
Run Code Online (Sandbox Code Playgroud)

但是,由于我在同一个列表上进行迭代,因此新列表的索引(范围)不再有效.有没有人对如何正确删除元素有一些建议?

最好的祝愿

python list

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

使用模板类作为方法参数

我有两个模板类:Class1 <S>和Class2 <T>.在Class2 <T>中,有一个方法,它具有指向Class1 <S>对象的指针作为参数.我应该重新定义class2 <T>到class2 <S,T>?或者还有其他最佳解决方案吗?问题是,我可能有新方法将其他模板类的对象作为参数引用.因此,我会避免某事.喜欢:class2 <S,T,U ...>

template < class S >
class Class1{
    public:
        ...
    private:
        ...
};

template < class T >
class Class2{
    public:
        ...
        Class2<T> * doSomething(Class1<S> * );
        ...
    private:
        ...
};

template < class S, class T >
class Class2{
    public:
        ...
        Class2<T> * doSomething(Class1<S> * );
        ...
    private:
        ...
};
Run Code Online (Sandbox Code Playgroud)

c++ templates parameter-passing

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

类构造函数,析构函数和运算符重载的实用函数

不久之前,我在一个网站上发现了一些实用函数的代码示例,这些函数在使用时creating,destructing对象甚至在重载某些函数时使用operators.更确切地说,主要使用以下成员函数:init,copy,set和destroy.

  • init成员函数用于所有的私有成员初始化.它主要被称为内部constructors,例如defaultparameter constructor.
  • 所述copy成员函数是用来做一个deep copy作为传递的对象的const reference.它被称为内部reference constructor和过载operator =.
  • set 成员函数主要allocates内存为private members需要它.
  • 最后,destroy成员函数用于releasing分配的内存.例如,它被称为内部destructor.

我想得到你的意见,并知道这是一个很好的编程实践吗?哪些好处或缺点是什么?欢迎任何意见和建议!下面,我将说明如何为CMatrix<T>类定义这些成员函数.

matrix.h

template < class T >
class CMatrix{

    CMatrix(){ this->initMatrix(); }

    CMatrix(int nRows, int nCols, int nChannels){
        this->initComplexMatrix();
        this->setComplexMatrix(nRows, nCols, nChannels);
    }

    CMatrix(const CMatrix<T> & refMatrix){
        this->initComplexMatrix();
        this->copyComplexMatrix(refMatrix);
    }

    CMatrix<T> …
Run Code Online (Sandbox Code Playgroud)

c++ constructor destructor operator-overloading

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

显式实例化模板方法中的编译错误

我有两个不同的模板类.其中一个具有成员函数,该函数返回指向另一个模板类的对象的指针.目前,我无法编译下面的代码,我们非常欢迎任何建议.

main.cpp中

#include <stdio.h>
#include <stdlib.h>
#include <foo.h>
#include <bar.h>

int main(int argc, char **argv){
    ...
    int nParam;
    ...

    CFoo<float> * pFoo = NULL;
    pFoo = new CFoo<float>();

    CBar<float> * pBar = NULL;
    pBar = pFoo->doSomething(nParam); // error: no matching function for call to ‘CFoo<float>::doSomething(int)’

    ...

    delete pFoo;
    delete pBar;

    return (0);
}
Run Code Online (Sandbox Code Playgroud)

foo.h中

#include <bar.h>

template < class FOO_TYPE >
class CFoo{

    public:

        ...

        template < class BAR_TYPE >
        CBar<BAR_TYPE> * doSomething(int);
        ...
};
Run Code Online (Sandbox Code Playgroud)

Foo.cpp中

template < class FOO_TYPE …
Run Code Online (Sandbox Code Playgroud)

c++ templates instantiation

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