我是一名Java初学者,我看到了许多类似HTML格式的Java API文档,例如Java™Platform,Standard Edition 8 API Specification.我没有在任何这些文档中看到搜索选项,所以当我需要找到例如random(),我去索引,选择R并使用浏览器搜索随机.是否有更快的方式,例如全球搜索?
我想将元素添加到空 Numpy 数组中。我事先知道最大数组大小。我找不到直接的方法来实现这一点,所以这是我的解决方法:
N = 1000
a = np.empty([N], dtype=np.int32)
j = 0
for i in range(N):
if f(i):
a[j] = g(i)
j += 1
a.resize(j)
Run Code Online (Sandbox Code Playgroud)
是否有更优雅的编码方式,无需跟踪 中的当前长度j,类似于下面的 C++ 版本的简单性?
const int N = 1000;
vector<int> a;
a.reserve(N);
for (int i=0; i<N; i++)
if (f(i))
a.push_back(g(i));
a.shrink_to_fit();
Run Code Online (Sandbox Code Playgroud)
是的,我读过如何在 Numpy 中就地扩展数组?,但它不涵盖这种特定情况,即事先已知的数组大小限制。
是否有一个简洁的表示法来访问数组的最后一个元素,类似于C++中的std :: vector :: back()?我必须写:
veryLongArrayName.[veryLongArrayName.Length-1]
Run Code Online (Sandbox Code Playgroud)
每一次?
有没有办法避免在这个Julia表达式中创建一个数组:
max((filter(n -> string(n) == reverse(string(n)), [x*y for x = 1:N, y = 1:N])))
Run Code Online (Sandbox Code Playgroud)
并使其行为类似于此Python生成器表达式:
max(x*y for x in range(N+1) for y in range(x, N+1) if str(x*y) == str(x*y)[::-1])
Run Code Online (Sandbox Code Playgroud)
由于数组分配和N*N迭代与Python的N*N/2相比,Julia版本比Python慢2.3倍.
编辑
在Julia中进行了一些实现之后,我得到的最快的循环风格版本是:
function f(N) # 320ms for N=1000 Julia 0.2.0 i686-w64-mingw32
nMax = NaN
for x = 1:N, y = x:N
n = x*y
s = string(n)
s == reverse(s) || continue
nMax < n && (nMax = n)
end
nMax
end
Run Code Online (Sandbox Code Playgroud)
但改进的功能版本并不落后(如果你认为2x更大的域,只有14%慢或明显更快):
function e(N) # 366ms for N=1000 Julia …Run Code Online (Sandbox Code Playgroud) 这个完美的程序在Visual Studio 2013中的调试模式下失败:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void main()
{
vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
for (auto iFrom = v.cbegin(), iTo = iFrom+5; iFrom != v.cend(); iFrom = iTo, iTo += 5)
cout << *max_element(iFrom, iTo) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
与vector iterator + offset out of range断言失败.它失败了,因为iTo > v.cend()这在这里是无害的.调试器测试迭代器的值有什么意义,它没有被解除引用?
顺便说一句,我知道我可以重写上面的循环:
for (auto i = v.cbegin(); i != v.cend(); i += 5)
cout << *max_element(i, …Run Code Online (Sandbox Code Playgroud) 我在某个地方看过这个Scala代码片段:
def toSentiment(sentiment: Int): Sentiment = sentiment match {
case x if x == 0 || x == 1 => Sentiment.NEGATIVE
case 2 => Sentiment.NEUTRAL
case x if x == 3 || x == 4 => Sentiment.POSITIVE
}
Run Code Online (Sandbox Code Playgroud)
有没有办法case更简洁地重写声明?我怀疑必须有一种更简单(更短)的方式表达x if x == 0 || x == 1条件.
顺便说一下,这种形式:
def toSentiment(sentiment: Int): Sentiment = sentiment match {
case 0 => Sentiment.NEGATIVE
case 1 => Sentiment.NEGATIVE
case 2 => Sentiment.NEUTRAL
case 3 => Sentiment.POSITIVE
case 4 => Sentiment.POSITIVE …Run Code Online (Sandbox Code Playgroud) 我在 Windows 10 Home 版本 10.0.16299 32 位、Windows SDK 版本 10.0.17134.12 和 C++/WinRT 版本 1.0.180505.2 上使用 Visual Studio Community 2017 版本 15.7.1。当我尝试使用任何 C++/WinRT 模板创建新项目时,我收到以下错误消息:
Error: this template attempted to load component assembly
'Microsoft.VisualStudio.Universal.TemplateWizards, Version=15.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. For more
information on this problem and how to enable this template,
please see documentation on Customizing Project Templates.
这个问题有简单的解决方法吗?
该程序使用-std=c++20标志构建:
#include <iostream>
using namespace std;
int main() {
auto [n, m] = minmax(3, 4);
cout << n << " " << m << endl;
}
Run Code Online (Sandbox Code Playgroud)
当不使用3 4优化标志时产生预期结果。-Ox带有优化标志,它输出0 0. 我用多个带有-O1,-O2和-O3标志的 gcc 版本进行了尝试。
Clang 13 工作正常,但 clang 10 和 11 输出0 4198864具有优化级别-O2及更高级别。icc 工作正常。这里发生了什么?
代码在这里: https: //godbolt.org/z/Wd4ex8bej
我在这个片段中收到"根据此程序点之前的信息查找不确定类型的对象"错误:
let a = [|"a"; "bb"|]
let n = Array.mapi (fun i x -> (i * x.Length)) a
Run Code Online (Sandbox Code Playgroud)
这有什么问题?当我将光标悬停在其上方时,Visual Studio F#Interactive正确地将x的类型显示为字符串.为什么我要写:
let a = [|"a"; "bb"|]
let n = Array.mapi (fun i (x:string) -> (i * x.Length)) a
Run Code Online (Sandbox Code Playgroud)
编译成功?
是否有一种更简单(更短)的方式将此代码段作为循环编写:
for (auto [a, b]: {pair<int, int>{1, 2}, pair<int, int>{3, 4}})
foo(a, b);
Run Code Online (Sandbox Code Playgroud)
最好使初始化列表尽可能接近这个不编译的表单:
for (auto [a, b]: {{1, 2}, {3, 4}})
foo(a, b);
Run Code Online (Sandbox Code Playgroud)