小编tjw*_*992的帖子

在Perl中打印数组哈希散列的有效方法

我正在编写一个脚本,涉及打印数组散列的哈希内容.

ex(伪代码):

my %hash = ();
$hash{key1}{key2} = ['value1', 'value2', 'value3', . . .];
Run Code Online (Sandbox Code Playgroud)

要么

$hash{key1}{key2} = @array_of_values;
Run Code Online (Sandbox Code Playgroud)

基本上我希望能够为任意数量的键组合执行此操作,并能够循环遍历所有可能的键/值对(或者可能更正确地表示为键,键/数组对,因为每个值实际上是一个数组值和每个数组有2个与之关联的键)并以下列格式打印输出:

"key1,key2,value1,value2,value3,... \n"

例如:

#!/usr/bin/perl

use strict;
use warnings;

# initialize hash
my %hash = ();
my $string1 = "string1";
my $string2 = "string2";

# push strings onto arrays stored in hash
push @{$hash{a}{b}}, $string1;
push @{$hash{a}{b}}, $string2;
push @{$hash{c}{d}}, $string2;
push @{$hash{c}{d}}, $string1;

# print elements of hash
# (want to do this as a loop for all possible key/value pairs) …
Run Code Online (Sandbox Code Playgroud)

perl hash data-structures

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

在Perl中避免全局变量("正确"的方式是什么?)

在编写Perl代码时,我经常遇到这样的情况,即我在文件顶部有一个大型变量集群,它们在脚本中充当"全局"变量.我一直在所有国会大写字母中写下这些"全局"变量,以区别于其他人,但我最近一直在研究一些包含许多模块的非常大的脚本,并且非常希望学习一种最小化"全局"使用的好方法变量并使我的代码尽可能安全.

我经常遇到的情况是有两个共享大量变量的子程序:

my $var1, $var2, $var3, $var4, $var5 . . .
sub sub1 {
    # uses $var1, $var2, $var3 . . .
    # may even make changes to some of these variables
}

sub sub2 {
    # also uses $var1, $var2, $var3 . . .
    # may change $var1, $var2, $var3
}

sub sub3 {
    # doesn't use $var1, $var2, $var3
    # still has access to change them
}
Run Code Online (Sandbox Code Playgroud)

将所有这些变量传递到每个子例程中并返回4或5个变量看起来非常难看并且很快就难以跟踪所有变量,但是如果我将这些变量保持全局,那么我遇到了sub3可能编辑它们的问题它不应该.

我知道我可以使用"{}"对范围变量进行操作,但我个人认为这看起来很丑陋:

{
    my $var1, $var2, $var3, $var4, $var5 . . …
Run Code Online (Sandbox Code Playgroud)

variables perl global-variables code-organization scoping

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

使用PIMPL习语,实现是否应始终是该类的私有成员?

我已经看到PIMPL习语以两种不同的方式实现.一种方法是始终使实现成为类的私有成员.这确保了无论如何都无法从类外部访问实现.

例如:

example.h文件

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <memory>

class Example {
public:
    void doSomething();
private:
    struct Impl;
    std::shared_ptr<Impl> pimpl_;
};

#endif /* EXAMPLE_H */
Run Code Online (Sandbox Code Playgroud)

Example.cpp

#include "Example.h"
#include <iostream>

struct Example::Impl {
    void doSomething() {
        std::cout << "Hello World!" << std::endl;
    }
};

void Example::doSomething() {
    pimpl_->doSomething();
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "Example.h"

int main() {
    Example ex;
    ex.doSomething();
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

我看到的另一种方法是使用它自己的.h文件和自己的.cpp文件使实现成为一个完全独立的类.

例如:

example.h文件

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <memory>

struct ExampleImpl;

class Example { …
Run Code Online (Sandbox Code Playgroud)

c++ oop optimization pimpl-idiom dependency-management

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

指代您不拥有的“ std :: unique_ptr”(使用原始指针?)

通常,如果您使用a std::shared_ptr指向对象,并且要创建另一个指向该对象的不共享所有权的指针,则会创建一个std::weak_ptr

// Create a shared pointer to own the object
std::shared_ptr<int> p = std::make_shared<int>(42);

// Create a weak pointer (that does not own the object)
std::weak_ptr<int> q(p);

// Use the weak pointer some time later
if (std::shared_ptr ptr = q.lock()) {
  // use *ptr
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,涉及到该如何处理std::unique_ptr

使用唯一指针可确保当前资源由其std::unique_ptr自身专有。但是,如果我想创建一个指向不拥有该资源的资源的指针,该怎么办?我不能使用a,std::weak_ptr因为弱指针旨在与的引用计数一起使用std::shared_ptr。我会在这里使用原始指针吗?还是有更好的选择?

// Create a unique pointer to own the object
std::unique_ptr<int> p = std::make_unique<int>(42);

// Create a non-owning pointer to the same …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr weak-ptr unique-ptr c++11

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

通过模板函数调用未知类型的方法

C++语言允许我编写一个模板函数,该函数将调用传递给函数的对象上的方法.

这里的问题是当我这样做时,我的IDE(NetBeans 8.2)会抱怨它无法解决该方法.这是有道理的,因为在我尝试编译代码之前,用于"T"的类型是未知的,但它给我一个警告的事实使我有点担心这样做是不是错误的编程实践.

请考虑以下代码:

struct S1 {
    void method() const {
        cout << "Method called from S1!" << endl;
    }
};

struct S2 {
    void method() const {
        cout << "Method called from S2!" << endl;
    }
};

template <typename T>
void call_method(const T& object) {

    // IDE reports a warning "can't resolve the template based identifier method."
    object.method();
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

S1 obj1;
S2 obj2;

call_method(obj1);
call_method(obj2);
Run Code Online (Sandbox Code Playgroud)

这段代码将编译并正常工作,但IDE总是会抱怨.这可以吗?或者是否有一个更好的设计,我应该知道,以获得相同的预期结果.

期望的结果是编写一个可以使用S1或S2对象的函数,而无需关心它是哪一个,只要它们提供包含"method()"的接口即可.

假设我无法访问S1和S2的源代码,因此无法对其进行更改.(例如,我不能使它们从公共基类继承并使用动态多态而不是模板函数来实现相同的效果).

c++ templates interface generic-programming

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

为什么C++标准模板库不包含向量的"排序"成员函数?

我注意到C++标准模板库包含列表对象的排序成员函数,但由于某种原因它不包含vector的成员函数.

这看起来很草率和不一致......

这是一个演示样本:

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

// Template function to print the contents of a container
template <typename Container>
void PrintContainer(const Container& container)
{
    cout << "{ ";
    Container::const_iterator i;
    for (i = container.begin(); i != container.end(); i++)
    {
        cout << *i << ' ';
    }
    cout << "}" << endl;
}

// Main
int main()
{
    vector<int> v;
    list<int> l;

    // Initialize the vector and the list in descending order
    for …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stl list vector

-2
推荐指数
2
解决办法
296
查看次数

为什么动态库可以链接到其他库,但静态库不能?

请考虑以下代码结构:

main.cpp -> depends on libone.a -> depends on libtwo.a

假设main.cpp仅使用函数libone.a.所以现实地,程序员写作main.cpp真的只关心libone.a.在这一点上,他们甚至不知道libone.a有依赖libtwo.a.

他们尝试按如下方式编译代码并获取链接器错误:

g++ -o main main.cpp -lone

在此输入图像描述 - 错误!未定义的符号!

这成为一个问题,因为libone.a依赖于libtwo.a,任何使用的人都libone.a必须知道这种依赖关系...正如你可以想象的那样,FAR可能会出现这个问题,而不是单个库的FAR更多的依赖关系,并且很快就会成为一个链接噩梦.


尝试1解决此问题:

第一种认为,解决这个问题是"很简单,我会只是链接libone.alibtwo.a当我编译libone.a!

事实证明它并不像我希望的那么简单......编译libone.a时无法链接libtwo.a.编译它们时,静态库不会链接到任何内容,而是在将库编译为可执行文件时必须链接所有依赖项.

例如,要编译main.cpp依赖于静态库,而静态库又依赖于另一个静态库,则必须链接这两个库.总是.

g++ -o main main.cpp -lone -ltwo


尝试2解决此问题:

另一个想法是尝试编译libone为链接到的动态库libtwo.a.

奇怪的是,这才有效!编译和链接libone.so主程序后,只需要关心libone.so,不再需要了解libtwo.a.

g++ -o main main.cpp -lone

成功!


经过这个练习后,仍然缺少一件.我似乎无法弄清楚为什么静态库不能在其他库中链接,但动态库可以.事实上,动态库 …

c++ linker dynamic-linking static-linking

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

硬编码2D数组值

尝试将值插入到2D数组中,但输出不是给出我的值,而是随机字母

int myArr[8][2] = {700,730,760,790,810,840,910,1000}{0.011,0.035,0.105,0.343,0.789,2.17,20,145};
cout  << myArr << endl;
system("Pause");
Run Code Online (Sandbox Code Playgroud)

我应该如何调整代码,还是更容易使用文本文件并插入?

c++ arrays

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