我想Maybe从D中的Haskell 实现,只是为了它的地狱.这是我到目前为止所得到的,但并不是那么好.任何想法如何改进它?
class Maybe(a = int){ } //problem 1: works only with ints
class Just(alias a) : Maybe!(typeof(a)){ }
class Nothing : Maybe!(){ }
Maybe!int doSomething(in int k){
if(k < 10)
return new Just!3; //problem 2: can't say 'Just!k'
else
return new Nothing;
}
Run Code Online (Sandbox Code Playgroud)
Haskell也许定义:
data Maybe a = Nothing | Just a
Run Code Online (Sandbox Code Playgroud) 我写了这个正则表达式:
(.+?)\s*{?\s*(.+?;)\s*}?\s*
Run Code Online (Sandbox Code Playgroud)
哪个测试正常:https : //regex101.com/r/gD2eN7/1
但是当我尝试用 C++ 构建它时,我得到了一个运行时错误。
temp2.exe 中 0x7714C52F 处的未处理异常:Microsoft C++ 异常:
内存位置 0x003BF2EC 处的 std::regex_error。
const auto input = "if (KnR)\n\tfoo();\nif (spaces) {\n foo();\n}\nif (allman)\n{\n\tfoo();\n}\nif (horstmann)\n{\tfoo();\n}\nif (pico)\n{\tfoo(); }\nif (whitesmiths)\n\t{\n\tfoo();\n\t}"s;
cout << input << endl;
cout << regex_replace(input, regex("(.+?)\\s*{?\\s*(.+?;)\\s*}?\\s*"), "$1 {\n\t$2\n}\n") << endl;
Run Code Online (Sandbox Code Playgroud)
我是否使用了 C++ 不支持的功能?
$colors = array(
'r' => 'a',
'g' => 'b',
'b' => 'c'
);
list($v, $k) = each($colors);
echo $v . " " . $k
Run Code Online (Sandbox Code Playgroud)
现在上面的打印ra我很惊讶,因为我认为'list'构造只适用于数值数组.它为什么有效?
什么时候调用__construct,什么时候调用索引?还有其他差异吗?
什么放在__construct?什么是最好的做法,我应该把$ this->加载调用......?还有什么?
class Site extends CI_Controller {
public function __construct() {
parent::__construct();
echo 'Hello World2';
}
public function index() {
echo 'Hello World1';
}
}
Run Code Online (Sandbox Code Playgroud) 我想构建一个Pandas Dataframe,所有行应该等于df2的列名:
df1 = pd.Dataframe( ???, index=df2.index, columns=df2.columns)
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不起作用:
df1 = pd.Dataframe( np.repeat(df2.columns, df2.shape[0]) , index=df2.index, columns=df2.columns)
Run Code Online (Sandbox Code Playgroud) 我是C++的新手,我需要对构造函数做一些澄清,我的问题是:
请解释如何完成,或为什么不能.我需要对此有更深入的了解.
我正在阅读"C编译器设计"一书.在基础部分,我发现lexer的ac片段是这样的 -
static int Lookahead = -1;
int match(token)
int token;
{
if(Lookahead == -1)
Lookahead = lex();
return token == Lookahead;
}
void advance(){
Lookahead = lex();
}
Run Code Online (Sandbox Code Playgroud)
我对如何在gnu gcc上编译匹配函数感到困惑.所以我写了一个看起来像的函数
int a(token)
int token;
{
printf("Value of token is %d", token);
}
int main()
{
printf("Hello world!\n");
a(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出 -
你好,世界!令牌的值为1
但我没有得到功能声明背后的原因.以这种方式声明功能有什么好处?而令牌的价值如何为1?为什么它不是编译错误?它是C中的某种函数声明吗?
谢谢你查看我的问题.任何形式的帮助都会很棒.
首先,我真的检查了是否已经有人问过一个问题,但我找不到任何问题。错误消息不应该欺骗你我的情况有点不同我猜或者我只是错过了一些东西。
当我处理一个玩具 C++ 代码时,我遇到了一个奇怪的错误。程序输出说有双重空闲情况,但我看不到发生此错误的地方。代码可能看的有点长,对此我深表歉意。
我现在正在工作Linux Distribution,我正在使用g++ 9.1.0. 我检查了我的代码并寻找错误的部分。
即使我固定的一些代码的一部分,我的问题没有得到解决,当我发表评论或者除了Foo{1, "Hello World"};或者vec.push_back(std::move(Foo{}));,我不为什么得到它。
class Foo
{
public:
Foo()
: val{nullptr}, str{nullptr}
{
std::cout << "You are in empty constructor\n";
}
Foo(int the_val, const char *the_str)
: val{new int}, str{new char[std::strlen(the_str + 1)]}
{
*val = the_val;
std::cout << *val << '\n';
std::strcpy(str, the_str);
std::cout << str << '\n';
}
~Foo()
{
if (val) {
delete val;
} else {
std::cout << "val is empty\n"; …Run Code Online (Sandbox Code Playgroud) c++ move-constructor construct copy-assignment move-assignment-operator
我在Java方面不是很全面,这就是为什么我问这个问题可能非常愚蠢.尽管如此,我试图弄清楚如何忽略类的默认构造方法,并使用带参数的构造方法.例如,像这样:
public class Name {
String firstName, lastName;
public Name()
{
String dialog = JOptionPane.showInputDialog("First and Last name: ");
Scanner inName = new Scanner(dialog);
firstName = inName.next();
lastName = inName.nextLine();
}
public Name(String newFirst, String newLast)
{
firstName = newFirst;
lastName = newLast;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个名为Student的类,它扩展了名称:
public class Student extends Name
{
public Student(String firstName, String lastName)
{
firstName = firstName;
lastName = lastName;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,Name类中的第一个构造方法会提示用户输入其名称,但是说我已经知道用户的名称并将其存储在某些变量中,如何创建一个新的Student()对象(实际上是一个名称)( )object)没有调用第一个默认构造函数,而是调用它:
Student student1 = new Student(firstName, lastName);
Run Code Online (Sandbox Code Playgroud)
我理解为什么以下行会调用默认的构造方法:
Student student1 = new Student(); …Run Code Online (Sandbox Code Playgroud) 我希望能够在父构造函数中设置私有属性的值,并在子构造函数或方法中调用该值.
例如:
<?php
abstract class MainClass
{
private $prop_1;
private $prop_2;
function __construct()
{
$this->prop_2 = 'this is the "prop_2" property';
}
}
class SubClass extends MainClass
{
function __construct()
{
parent::__construct();
$this->prop_1 = 'this is the "prop_1" property';
}
public function GetBothProperties()
{
return array($this->prop_1, $this->prop_2);
}
}
$subclass = new SubClass();
print_r($subclass->GetBothProperties());
?>
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[0] => this is the "prop_1" property
[1] =>
)
Run Code Online (Sandbox Code Playgroud)
但是,如果我prop_2改为protected,输出将是:
Array
(
[0] => this is the …Run Code Online (Sandbox Code Playgroud) construct ×10
c++ ×3
php ×3
inheritance ×2
c ×1
c++11 ×1
codeigniter ×1
controller ×1
d ×1
dataframe ×1
function ×1
haskell ×1
java ×1
list ×1
pandas ×1
private ×1
properties ×1
python ×1
regex ×1
repeat ×1