我正在编写一个脚本,涉及打印数组散列的哈希内容.
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代码时,我经常遇到这样的情况,即我在文件顶部有一个大型变量集群,它们在脚本中充当"全局"变量.我一直在所有国会大写字母中写下这些"全局"变量,以区别于其他人,但我最近一直在研究一些包含许多模块的非常大的脚本,并且非常希望学习一种最小化"全局"使用的好方法变量并使我的代码尽可能安全.
我经常遇到的情况是有两个共享大量变量的子程序:
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) 我已经看到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) 通常,如果您使用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++语言允许我编写一个模板函数,该函数将调用传递给函数的对象上的方法.
这里的问题是当我这样做时,我的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++标准模板库包含列表对象的排序成员函数,但由于某种原因它不包含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) 请考虑以下代码结构:
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更多的依赖关系,并且很快就会成为一个链接噩梦.
第一种认为,解决这个问题是"很简单,我会只是链接libone.a与libtwo.a当我编译libone.a!
事实证明它并不像我希望的那么简单......编译libone.a时无法链接libtwo.a.编译它们时,静态库不会链接到任何内容,而是在将库编译为可执行文件时必须链接所有依赖项.
例如,要编译main.cpp依赖于静态库,而静态库又依赖于另一个静态库,则必须链接这两个库.总是.
g++ -o main main.cpp -lone -ltwo
另一个想法是尝试编译libone为链接到的动态库libtwo.a.
奇怪的是,这才有效!编译和链接libone.so主程序后,只需要关心libone.so,不再需要了解libtwo.a.
g++ -o main main.cpp -lone
成功!
经过这个练习后,仍然缺少一件.我似乎无法弄清楚为什么静态库不能在其他库中链接,但动态库可以.事实上,动态库 …
尝试将值插入到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++ ×6
perl ×2
algorithm ×1
arrays ×1
c++11 ×1
hash ×1
interface ×1
linker ×1
list ×1
oop ×1
optimization ×1
pimpl-idiom ×1
scoping ×1
shared-ptr ×1
stl ×1
templates ×1
unique-ptr ×1
variables ×1
vector ×1
weak-ptr ×1