小编Aus*_*yde的帖子

电话号码中的字母和数字的排列

对于我的计算机科学课,我们需要编写一个程序(用C++编写),它接受字符输入并根据手机上的拨号盘输出可能的排列,留下非数字字符.

例如,输入2个输出2,A,B,C.输入23个输出23,A3,B3,C3,2D,2E,2F,AD,AE,AF,BD,BE,BF等......

为该程序提供的应用程序是查找给定电话号码的"虚荣"电话号码的排列.

目前,我编写的程序甚至没有编译,我担心我使用的算法不正确:

#include <iostream>
#include <multimap.h>
#include <vector>
using namespace std;

// Prototypes
void initLetterMap(multimap<char,char> &lmap);
void showPermutations(const vector<string> &perms);
vector<string> getPermutations(const string &phoneNumber,const multimap<char,char> &lmap);
vector<char> getLetters(char digit, const multimap<char,char> &lmap);


// Declarations
void initLetterMap(multimap<char,char> &lmap) {
    lmap.insert(pair<char,char>('1','1'));
    lmap.insert(pair<char,char>('2','2'));
    lmap.insert(pair<char,char>('2','A'));
    lmap.insert(pair<char,char>('2','B'));
    lmap.insert(pair<char,char>('2','C'));
    lmap.insert(pair<char,char>('3','3'));
    lmap.insert(pair<char,char>('3','D'));
    lmap.insert(pair<char,char>('3','E'));
    lmap.insert(pair<char,char>('3','F'));
    // ...
}

vector<char> getLetters(char digit, const multimap<char,char> &lmap) {
    multimap<char,char>::iterator it;
    pair<multimap<char,char>::iterator,multimap<char,char>::iterator> range;
    vector<char> result;

    if (isdigit(digit)) {
        range = lmap.equal_range(digit);
        for (it=range.first;it!=range.second;++it) {
            result.push_back((*it).second);
        }
    } else {
        result.insert(result.end(),digit); …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm computer-science

5
推荐指数
2
解决办法
2329
查看次数

将"this"转换为引用指针

假设我有一个结构

struct Foo {
    void bar () {
       do_baz(this);
    }
    /* See edit below
    void do_baz(Foo*& pFoo) {
       pFoo->p_sub_foo = new Foo; // for example
    }
    */

    Foo* p_sub_foo;
}
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会告诉我

temp.cpp: In member function ‘void Foo::bar()’:
temp.cpp:3: error: no matching function for call to ‘Foo::do_baz(Foo* const)’
temp.cpp:5: note: candidates are: void Foo::do_baz(Foo*&)
Run Code Online (Sandbox Code Playgroud)

所以,我怎么转换什么似乎是一个const Foo*Foo*&

编辑:我没有用一个很好的例子.do_baz应该读

void do_baz(Foo*& pFoo) {
    if (pFoo == NULL) {
        pFoo = new Foo;
        return;
    }
    //other stuff …
Run Code Online (Sandbox Code Playgroud)

c++ pointers reference

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

回调作为静态成员数组的一部分

最近在我正在开发的项目中,我需要将回调存储在静态成员数组中,如下所示:

class Example {
    private static $_callbacks = array(
        'foo'=>array('Example','do_foo'),
        'bar'=>array('Example','do_bar')
    );
    private static function do_foo() { }
    private static function do_bar() { }
}
Run Code Online (Sandbox Code Playgroud)

为了调用它们,我尝试了明显的(甚至是天真的)语法(在Example类中):

public static function do_callbacks() {
    self::$_callbacks['foo']();
    self::$_callbacks['bar']();
}
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,这不起作用,导致我正在访问一个未定义的变量的通知,以及一个致命错误,指出self::$_callbacks['foo']需要可调用.

然后,我试过call_user_func:

public static function do_callbacks() {
    call_user_func(self::$_callbacks['foo']);
    call_user_func(self::$_callbacks['bar']);
}
Run Code Online (Sandbox Code Playgroud)

它奏效了!

我的问题是:

为什么我需要call_user_func用作中间人,而不是直接打电话给他们?

php static callback

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

在PHP中将UpperCasedCamelCase爆炸为Upper Cased Camel Case

现在,我正在用分裂,切片和内爆实现这个:

$exploded = implode(' ',array_slice(preg_split('/(?=[A-Z])/','ThisIsATest'),1));
//$exploded = "This Is A Test"
Run Code Online (Sandbox Code Playgroud)

更漂亮的版本:

$capital_split = preg_split('/(?=[A-Z])/','ThisIsATest');
$blank_first_ignored = array_slice($capital_split,1);
$exploded = implode(' ',$blank_first_ignored);
Run Code Online (Sandbox Code Playgroud)

但是,问题是当你有输入时'SometimesPDFFilesHappen',我的实现将(错误地)解释为'Sometimes P D F Files Happen'.

我怎样(简单地)让我的脚本压缩'P D F''PDF'

我应该分开的资格是从第一个首都开始,到最后一个结束,以适应下一个词.

是的,我知道存在一些含糊不清的内容,例如'ThisIsAPDFTest',会被解释为'This Is APDF Test'.但是,我无法想到一种"聪明"的方法来避免这种情况,因此这是一种可接受的妥协.

php regex text-processing

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

分配给任意深度的数组索引?

如果我有一个数组对应于另一个数组中的连续递归键,那么将值赋给该"路径"的最佳方法是什么(如果你想称之为)?

例如:

$some_array = array();
$path = array('a','b','c');
set_value($some_array,$path,'some value');
Run Code Online (Sandbox Code Playgroud)

现在,$some_array应该是平等的

array(
  'a' => array(
    'b' => array(
      'c' => 'some value'
)))
Run Code Online (Sandbox Code Playgroud)

目前,我正在使用以下内容:

function set_value(&$dest,$path,$value) {
  $addr = "\$dest['" . implode("']['", $path) . "']";
  eval("$addr = \$value;");
}
Run Code Online (Sandbox Code Playgroud)

显然,这是一种非常天真的方法并且存在安全风险,那么你将如何做呢?

php arrays

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

如何使用PHP-YAML的自定义标记回调?

根据官方文档,有一种方法可以为自定义YAML标记提供回调:

mixed yaml_parse ( string $input [, int $pos = 0 [, int &$ndocs [, array $callbacks ]]] )
Run Code Online (Sandbox Code Playgroud)

回调
YAML节点的内容处理程序.YAML标记的关联数组=>回调映射.

但是,即使在扩展源中,似乎也没有关于该主题的其他文档!

我创建了这个脚本作为测试:

<?php

$yaml =<<<YAML
---
prop: !custom val
YAML;

print_r(yaml_parse($yaml,0,$n,array(
  YAML_STR_TAG => function () {
    echo "YAML_STR_TAG\n";
    $args = func_get_args();
    print_r($args);
    return 'x';
  },
  '!custom' => function () {
    echo "!custom\n";
    $args = func_get_args();
    print_r($args);
    return 'y';
  }
)));
Run Code Online (Sandbox Code Playgroud)

我得到了这个输出:

$ php yaml.php
YAML_STR_TAG
Array
(
    [0] => prop
    [1] => tag:yaml.org,2002:str
    [2] => 1
) …
Run Code Online (Sandbox Code Playgroud)

php yaml

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

需要一些使用Java Generics的帮助

我正在尝试建立一个系统来响应我的应用程序中发生的事件,类似于Observer模式.在我的系统中,EventProducers触发事件并EventConsumer响应这些事件,并且两者通过中央集线器连接:

就目前而言,我会忽略EventProducer,专注于EventHubEventConsumer:

interface EventConsumer<E extends Event> {
    void respondToEvent(E event);
}

class EventHub {
    private HashMap<Class</*event type*/>, HashSet<EventConsumer</*event type*/>>> subscriptions;
    public <E extends Event> void fireEvent(E event) {
        /* For every consumer in the set corresponding to the event type {
            consumer.respondToEvent(event);
        } */
    }
    public <E extends Event> void subscribeToEvent(EventConsumer<E> consumer) {
        /* Insert consumer into the set corresponding to E */
    }
}
Run Code Online (Sandbox Code Playgroud)

问题在于声明HashMap:我希望能够做类似的事情 …

java generics

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

将std :: ifstream读取到行向量

我如何在每个行都是单个数字的文件中读取,然后将该数字输出到行向量中?

例如:file.txt包含:

314
159
265
123
456
Run Code Online (Sandbox Code Playgroud)

我试过这个实现:

vector<int> ifstream_lines(ifstream& fs) {
    vector<int> out;
    int temp;
    getline(fs,temp);
    while (!fs.eof()) {
        out.push_back(temp);
        getline(fs,temp);
    }
    fs.seekg(0,ios::beg);
    fs.clear();
    return out;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试编译时,我会遇到如下错误:

error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline
(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : 
could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
Run Code Online (Sandbox Code Playgroud)

所以,显然,出了点问题.有没有比我想要的更优雅的解决方案?(假设像Boost这样的第三方库不可用)

谢谢!

c++ file-io

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

实现默认构造函数

我试图在C++中实现一个DateTime类:

class DateTime {
public:
    DateTime();
    DateTime(time_t ticks);
    DateTime(int day, int month, int year);
    DateTime(int day, int month, int year, int hour, int minute, int second);
    //...

private:
    time_t ticks;
    int day;
    int month;
    //...
}
Run Code Online (Sandbox Code Playgroud)

然后在申请中:

DateTime date1; //default constructor
Run Code Online (Sandbox Code Playgroud)

我知道c ++需要一个默认的构造函数,但是在这种情况下我该如何实现呢?

它应该将所有属性设置为0吗?这会让所有其他方法都有效,但看起来并不直观......

它应该只是让所有属性都未初始化吗?这将使它的方法都不起作用,但它似乎比0更直观,因为你还没有做任何事情.

它应该设置内部,bool initialized=false然后所有方法检查之前操作吗?

我对这一点不太确定.这样做有"标准"的方法吗?

c++ implementation constructor

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

奇数重复符号错误

对于一个学校项目,该课程被要求编写一个String类来模仿STL string类.

我已经编写了所有代码,但链接器似乎被我的一个运营商所接受.

有三个文件,String.h,String.cpp,和test2.cpp

我的Makefile样子

CC=gcc
CXX=g++
CXXFLAGS+=-Wall -Wextra
LDLIBS+=-lstdc++

all:test2

test2:test2.o String.o
test2.o:test2.cpp String.h
String.o:String.cpp String.h
Run Code Online (Sandbox Code Playgroud)

make 输出以下内容:

g++ -Wall -Wextra   -c -o test2.o test2.cpp
g++ -Wall -Wextra   -c -o String.o String.cpp
g++   test2.o String.o  -lstdc++ -o test2
ld: duplicate symbol operator==(String const&, char const*)in String.o and test2.o
collect2: ld returned 1 exit status
make: *** [test2] Error 1
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为我定义的唯一地方operator ==String.h:

#ifndef MY_STRING_H
#define MY_STRING_H …
Run Code Online (Sandbox Code Playgroud)

c++ debugging

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