小编tjw*_*992的帖子

在Perl中,"$ a"和"$ b"在sort()函数之外有什么特殊用途吗?

sort()几天我问了一个关于在Perl 函数中使用"$ a"和"$ b"的问题:

Perl的"sort()"函数究竟是什么"$ a"和"$ b"?

我现在有一个跟进问题.是"$ a"和"$ b"仅使用sort()或是否有任何其他Perl函数利用这些特殊的全局变量?

或者即使没有其他功能使用它们,是否还有其他情况sort()你会使用"$ a"或"$ b"?

编辑:

澄清:

简而言之,问题是"$ a"和"$ b"可以被其他东西使用sort()吗?

我只是想知道他们可以使用的其他情况.我从未见过其他任何东西使用的"$ a"或"$ b",并且想知道除了之外还有其他任何特殊用途sort().

perl global-variables lexical-scope

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

使用Scalar :: Util的弱化会导致无效的引用问题吗?

有关背景信息,请参阅此相关问题.

注意:当我说"无效引用"时,我指的是指向无数据的引用.


假设我们有以下包含循环引用的数据结构:

       +-----------------------------------------------------+
       |                                                     |
       +-->+============+    +==========+                    |
           [ Reference ----->[ Blessed  ]                    |
$parent -->+============+    [ Hash     ]                    |
                             [          ]   +==========+     |
                             [ children --->[ Array    ]     |
                             [          ]   [          ]     |
                             +==========+   [ 0: ---------+  |
                                            [          ]  |  |
                                            +==========+  |  |
                                                          |  |
       +--------------------------------------------------+  |
       |                                                     |
       +-->+============+    +==========+                    |
           [ Reference ----->[ Blessed  ]                    |
$child --->+============+    [ Hash     ]                    |
                             [          ]                    |
                             [ parent: ----------------------+
                             [          ] …
Run Code Online (Sandbox Code Playgroud)

oop perl memory-management reference cyclic-reference

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

有条件返回不同类型的 C++ 函数

我正在尝试编写一个模板函数,该函数将根据传入的字符串返回不同的类型。

template<typename T>
T test(string type)
{
    int   integer   = 42;
    float floateger = 42.42;

    if (type == "int")
        return integer;
    if (type == "float")
        return floateger;
}

int main()
{

    int integer = test("int");

    cout << "INTEGER: " << integer << endl;
}
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我收到以下错误:

错误:没有匹配的函数可用于调用“test(const char [4])”

我怎样才能实现这样的事情呢?


我的最终目标是编写一个函数,该函数将根据传递给它的字符串返回不同类的对象。我知道这可能根本不是正确的方法。做这样的事情的正确方法是什么?

c++ templates function

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

为什么我不能从这个模板函数返回引用?

我直接从一本书(Sams每天一小时教你自己C++)中拿出这个例子:

// Get the maximum of two values
template <typename objectType>
objectType& GetMax(const objectType& value1, const objectType& value2)
{
    if (value1 > value2)
        return value1;
    else
        return value2;
}
Run Code Online (Sandbox Code Playgroud)

本质上,它是一个非常详细的模板函数,用于查找任何类型的2个值之间的最大值.

我试图使用如下函数:

// Test the Max function
int x_int = 25;
int y_int = 40;
int max_int = GetMax(x_int, y_int);
cout << "max_int: " << max_int << endl;

double x_double = 1.1;
double y_double = 1.001;
double max_double = GetMax(x_double, y_double);
cout << "max_double: " << max_double << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ templates reference return-type

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

将所有参数作为Perl中的字符串传递给子例程

我正在尝试编写一个函数,它可以获取所有参数并将它们打印为完全符合输入的字符串.

例如,使用以下功能:

test('arg1' => $arg1, 'arg2' => $arg2);
Run Code Online (Sandbox Code Playgroud)

我想获得该函数内的以下字符串格式化EXACTLY所见如下:

"'arg1' => $arg1, 'arg2' => $arg2"
Run Code Online (Sandbox Code Playgroud)

我想这样做,所以我可以打印所有参数,就像输入它们进行调试/测试一样.

string perl arguments parameter-passing subroutine

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

Perl DateTime"is_dst()"方法无法正常工作

我正在尝试使用模块中的is_dst()方法DateTime来确定是否正在进行夏令时转换.

我开始写一个非常基本的例子,我确信它会起作用,但出于某种原因,我得到了意想不到的结果.


例:

#!/usr/bin/perl

use strict;
use warnings;
use DateTime;

my $dt1 = DateTime->new(
    'year'  => 2015,
    'month' => 3   ,
    'day'   => 8   ,
    'hour'  => 3   ,
);

my $dt2 = DateTime->new(
    'year'  => 2015,
    'month' => 3   ,
    'day'   => 7   ,
    'hour'  => 3   ,
);

print $dt1."\n";
print $dt2."\n";
print $dt1->is_dst()."\n";
print $dt2->is_dst()."\n";
Run Code Online (Sandbox Code Playgroud)

我开始的日期是我知道的夏令时过渡期:2015年3月8日星期日.我之所以选择凌晨3点,是因为我知道在那个时间点已经发生了夏令时过渡.

然后我在夏令时过渡之前就知道了一个日期:2015年3月7日星期六也是凌晨3点.

然后我打印两个日期及其相应的DST标志.

输出:

2015-03-08T03:00:00
2015-03-07T03:00:00
0
0 …
Run Code Online (Sandbox Code Playgroud)

perl datetime dst

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

使用 GMock 模拟具有相同数量输入参数的重载方法

我有一个类定义了我需要模拟的重载方法。问题是,这两个重载都只接受一个参数,并且 GMock 似乎认为该调用是不明确的。

这是一个演示该问题的小示例(为了演示而过于简单化):

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <iostream>
#include <string>

using ::testing::_;

class MyClass
{
public:
    virtual ~MyClass() = default;

    virtual void PrintValue(const std::string& value) const
    {
        std::cout << value << std::endl;
    }

    virtual void PrintValue(const int& value) const
    {
        std::cout << value << std::endl;
    }
};

class MyClassMock : public MyClass
{
public:
    MOCK_CONST_METHOD1(PrintValue, void(const std::string&));
    MOCK_CONST_METHOD1(PrintValue, void(const int&));
};

TEST(MyTest, MyTest)
{
    MyClassMock mock;
    EXPECT_CALL(mock, PrintValue(_)).Times(1);
    mock.PrintValue(42);
}

int main(int argc, char* argv[])
{
    ::testing::InitGoogleMock(&argc, argv); …
Run Code Online (Sandbox Code Playgroud)

c++ overloading googletest ambiguous googlemock

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

将大型模块划分为多个文件

道歉,看起来我的原始问题无法正确解释我在做什么以及我想要实现什么.这是一个更新的问题.

这可能是最简单的问题,但我无法在任何地方找到答案.

我有一个很大的Perl模块(比如ABC.pm),当我们添加新功能时,它会不断增长.大多数这些功能(几乎90%)发送请求和处理响应.以下是一个此类请求的代码.

sub UserDeleteRequest
{
    my ($self, $inputParam) = @_;
    my $config = $self->getConfig();
    return $self->_doRequest (REQUEST => 'UserDeleteRequest',
                              PARAM => $inputParam));
}
Run Code Online (Sandbox Code Playgroud)

与此类似,编写了其他函数,并在我们添加新请求时不断增长.

拥有大文件变得难以维护.所以,我正在寻找一些最佳实践来使这更容易.我想到的一个想法是将这个大模块拆分成多个文件(如何??)

perl

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

Python的运行速度明显快于C ++?这里似乎有些问题

在将其转换为C ++之前,我一直在使用Python进行一些快速原型制作,发现在某些情况下,Python代码的运行速度明显快于C ++代码!

考虑一下用Python和C ++编写的简单循环:


蟒蛇:

import numpy as np
import datetime

N = 16777216
f_s = 8000.0
t_s = 1/f_s

y = np.empty(N)

start = datetime.datetime.now()

for n in range(0,N):
    y[n] = np.sin(2*np.pi*1000*n*t_s) + 0.5*np.sin(2*np.pi*2000*n*t_s + 3*np.pi/4)

stop = datetime.datetime.now()
duration = stop - start

print("duration ", duration.microseconds, " microseconds")
Run Code Online (Sandbox Code Playgroud)

输出:

持续时间842000微秒


C ++:

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

int main() {
    int N = 16777216;
    int f_s = 8000;
    double t_s = 1.0 / f_s; …
Run Code Online (Sandbox Code Playgroud)

c++ python performance

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

成员变量如何与专门的类模板一起使用?

我正在尝试编写一个非常简单的专用类模板,它具有一个成员变量,并且可以在特殊情况下以不同的方式打印该成员变量。我知道这个例子没什么用,但它很好地说明了这个问题。

当专门化类模板时,类的专门化似乎不共享相同的成员变量,因此以下代码将无法编译...

#include <iostream>
#include <string>

// Class template
template <typename T>
struct S
{
    S(const T& t)
        : t(t)
    {}

    void print()
    {
        std::cout << t << std::endl;
    }
private:
    T t;
};

// Specialization
template <>
struct S<std::string>
{
    void print()
    {
        // ERROR: "t" is not defined in this context
        std::cout << "string: " << t << std::endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

这表明我需要为每个专业化编写一个单独的构造函数,并为每个专业化有一个单独的成员变量t,如果我有很多专业化,它会很快成为大量重复的代码和工作。

如果我说的是真的,那么在专门的类模板中完全使用成员变量是不好的做法吗?是否有任何替代方法可以减少代码重复?

c++ templates template-specialization member-variables

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