我是一名C++初学者,我刚刚编写了这个简单的程序:
#include <iostream>
using namespace std;
int readNumber()
{
cout << "Insert a number: ";
int x;
cin >> x;
return x;
}
void writeAnswer(int x)
{
cout << "The sum is: " << x << endl;
}
int main()
{
int x = readNumber();
int y = readNumber();
int z = x + y;
writeAnswer(z);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么输出是这样的:
Insert a number: 3
Insert a number: 4
The sum is: 7
Run Code Online (Sandbox Code Playgroud)
而不是像:
Insert a number: 3Insert a number: 4The …Run Code Online (Sandbox Code Playgroud) 我在 Julia 用户组上找到了一些讨论,但它们对我来说太技术性了。
我想知道在两者之间进行选择的标准是什么。
我正在关注 JuliaBox 教程,但它没有解释太多。谢谢
我使用下面的示例来测试和学习如何从子窗口小部件调用父函数。当您单击 时TestButton,countRefresh将调用该函数,并且变量count会增加 1。现在,每次单击按钮时都会更改颜色(蓝色或红色)。
问题:假设我希望颜色根据count变量周围的某些逻辑进行更改,如何count从小TestButton部件内访问变量?例如,如果count是三的倍数,则按钮应为红色,否则为蓝色。
我读到了有关 InheritedWidgets 的内容,但似乎变量在 InheritedWidgets 中必须是final的(如果我在int count = 0;收到“此类被标记为不可变”消息错误之前没有放置final)。但根据这个例子,count每次单击按钮时我都需要更改。InheritedWidgets 的替代方案是什么?
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
static const String id = 'test';
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
int count = 0;
Color color = Colors.red;
void refreshCount() {
setState(() {
count += 1;
});
}
@override
Widget build(BuildContext context) {
return …Run Code Online (Sandbox Code Playgroud) 在Julia中,vec将多维数组重新整形为一维数组.但是它不适用于数组数组或元组数组.使用数组理解的一部分,还有另一种方法来展平数组/元组的数组吗?还是数组/元组的数组/元组数组?要么 ...
这是制作带有圆角的容器的方法:
Container(decoration: BoxDecoration(borderRadius: BorderRadius.circular(10),),
Run Code Online (Sandbox Code Playgroud)
但是有没有办法像下图那样制作倒圆角?我在网上找不到任何东西
将程序从Python翻译成Julia后,我非常不满意:
我认为原因是我不明白内存分配是如何工作的(autodidact在这里,没有CS背景).我会在这里发布我的代码但是它太长而且太具体了,除了我之外它对任何人都没有好处.因此我做了一些实验,现在我有一些问题.
考虑这个简单script.jl:
function main()
@time begin
a = [1,2,3]
end
end
main()
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:
$ julia script.jl
0.000004 seconds (1 allocation: 96 bytes)
Run Code Online (Sandbox Code Playgroud)
1.为什么96字节?当我设置时a = []我得到64个字节(为什么空数组的重量如此之多?).96字节 - 64字节= 32字节.但是a是一个Array{Int64,1}.3*64位= 3*8字节= 24字节!= 32字节.
2.即使我设置了,为什么我会得到96个字节a = [1,2,3,4]?
3.运行时为什么我得到937.500 KB:
function main()
@time begin
for _ in 1:10000
a = [1,2,3]
end
end
end
main()
Run Code Online (Sandbox Code Playgroud)
而不是960.000 KB?
4.为什么,例如,filter()如此低效?看看这个:
check(n::Int64) = n % 2 == …Run Code Online (Sandbox Code Playgroud) 我尝试导入Data.Numbers.Primes
import Data.Numbers.Primes
Run Code Online (Sandbox Code Playgroud)
runhaskell给了我:
5.hs:1:8:
Could not find module `Data.Numbers.Primes'
Use -v to see a list of the files searched for.
Run Code Online (Sandbox Code Playgroud)
ghci给了我:
<no location info>:
Could not find module `Data.Numbers.Primes'
It is not a module in the current program, or in any known package.
Run Code Online (Sandbox Code Playgroud)
我尝试通过cabal安装Data.Numbers.Primes,但我得到了:
cabal update
...
cabal install Data
cabal: There is no package named 'Data'.
You may need to run 'cabal update' to get the latest list of available
packages.
cabal install Data.Numbers.Primes
cabal: The file does …Run Code Online (Sandbox Code Playgroud) 假设我有 4x4 多维数组 A:
A = collect(reshape(1:16, 4, 4))
4×4 Array{Int64,2}:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Run Code Online (Sandbox Code Playgroud)
我想逐行迭代(即[1, 5, 9, 13]首先,然后[2, 6, 10, 14],然后......)。
我该怎么做?现在我想出了以下几点:
`for row in 1:size(A, 1)
println(A[row, :])
# do something
end`
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一种更“pythonic”的方式来做到这一点:有点for line in A: for element in line: ....
我也知道 CartesianRange,但我希望在每次迭代时都有一个类似数组的行。
如果我有一个数字数组:
a = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
并打印它,我得到
[1,2,3]
Run Code Online (Sandbox Code Playgroud)
但如果我有一个元组数组:
b = [(1,2),(3,)]
Run Code Online (Sandbox Code Playgroud)
当我打印它时我得到:
Tuple{Int64,Vararg{Int64}}[(1,2),(3,)]
Run Code Online (Sandbox Code Playgroud)
如何避免打印类型?