所以我有一个任务是使用指针编写一个带有可变数量参数的函数“mult”。而这个函数必须计算浮点数的乘积。
我遵循了我们的大学给我们的指南,但我的产品仍然为零。我发现问题是要相乘的每个其他数字都是零。
#include <iostream>
using namespace std;
int mult(int k,...){
int* p = &k;
int m = 1;
for(; k != 0; k--){
m *= *(++p);
}
return m;
}
int main(){
float res1 = mult(11,45,10,9,8,7,6,5,4,3,2,2);
float res2 = mult(7,12,23,0.3,0.6,1,2);
float res3 = mult(3,0.6,-12,-0.9);
cout << "Your results are:\n"
<<res1<<"\n"
<<res2<<"\n"
<<res3<<"\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下是指南中的示例:
void Print_stor(int k, ...)
{
int n=k;
int a[n];
int *p = &k;
for ( ; k!=0;k--)
a[k-1]=*(++p);
for(int i=n-1; i>=0; i--)
printf("%i ", …Run Code Online (Sandbox Code Playgroud) 我想测试一个正则表达式在 Java 1.8.0_241 中是否有效
public static boolean isRegExpValid(String regExp) {
try {
Pattern.compile(regExp);
return true;
} catch (PatternSyntaxException e) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我正在测试三位数的正确正则表达式和不正确的正则表达式。
@Test
public void testValidRegexp() {
assertTrue(isRegExpValid("\\d{3}"));
}
@Test
public void testInvalidRegexp() {
assertFalse(isRegExpValid("{3}"));
}
Run Code Online (Sandbox Code Playgroud)
为什么我的第二次测试testInvalidRegexp失败了?isRegExpValid("{3}")应该返回false,但返回true。
我正在尝试使用 bouncycastle 在我的 java 程序中实现 curve25519,这是我想出的代码:
package crypto;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.ec.CustomNamedCurves;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
public class Curve {
public KeyPair generateKeyPair() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
X9ECParameters ecP = CustomNamedCurves.getByName("curve25519");
ECParameterSpec ecSpec = new ECParameterSpec(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
keyGen.initialize(ecSpec);
return keyGen.generateKeyPair();
}
}
Run Code Online (Sandbox Code Playgroud)
而我的主要方法:
package crypto;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Test_Curve {
public static …Run Code Online (Sandbox Code Playgroud) 我想散列一个具有两个私有成员的类,例如:
foo.h
class Foo {
private:
std::string a;
std::string b;
public:
Foo (std::string a, std::string b);
bool operator==(const Foo& other) const;
bool operator!=(const Foo& other) const;
std::size_t operator()(const Foo& ) const;
};
namespace std {
template <> struct hash<Foo> {
std::size_t operator()(const Foo& cp) const;
};
}
Run Code Online (Sandbox Code Playgroud)
文件
Foo::Foo (std::string _a, std::string _b) {
this->a = _a;
this->b = _b;
}
bool Foo::operator== (const Foo& other) const {
return this->a == other.a && this->b == other.b;
}
bool Foo::operator!= (const …Run Code Online (Sandbox Code Playgroud) 我必须使用物理公式来实现一些模拟。在一个公式中,有许多变量。我想用 100 个样本改变这些变量。对于每个样本,我必须使用所有组合进行计算。简化的 for 循环更好地解释了我想要做什么:
set.seed(3)
a = rnorm(100)
b = rnorm(100)
c = rnorm(100)
d = rnorm(100)
f = rnorm(100)
for (a in 1:length(a)) {
for (b in 1:length(b)) {
for (c in 1:length(c)) {
for (d in 1:length(d)) {
for (f in 1:length(f)) {
value = a + b / c * d - f # for illustrative purposes only
# ....
# ... then I append the value to a vector etc.
}
}
}
}
} …Run Code Online (Sandbox Code Playgroud) 与此相关的帖子太多了,我认为参考它们无济于事,但是如果人们发现特别有用的帖子,将它们添加到这里会很棒。
ArrayList<Integer> readIntegersFrom(Scanner scnr) {
ArrayList<Integer> lst = new ArrayList<Integer>();
while (scnr.hasNext()) {
lst.add(scnr.nextInt());
}
return lst;
}
Run Code Online (Sandbox Code Playgroud)
整数出现在 4 个地方:
到现在为止还挺好。但是,我的程序将这个方法复制了四次(我敢肯定,还会有更多),每个都有不同的类型参数和 Scanner 方法。(例如,ArrayList<Double>和scnr.nextDouble()。)
我不怀疑我混淆了我使用过的许多其他语言的想法和技术,但是有什么方法可以概括这种方法,所以我可以告诉它我想要一个 ArrayList,比如说,Double,它应该使用 Scanner .nextDouble()?这个问题的两个部分是 (a) 传达 ArrayList 的类型参数和 (b) 传达要使用的 Scanner 方法。
我会满足于指定或推导类型参数的单一方法,并有一个枚举参数来告诉扫描器使用哪种方法,尽管这两个方法是直接连接的。
例如,以下代码在编译时返回错误和警告int,更改为%d
警告:格式
%s需要 type 的char *参数,但参数 2 具有类型int
void stringd() {
char *s = "Hello";
printf("derefernced s is %s", *s);
}
Run Code Online (Sandbox Code Playgroud) 哈希函数非常有用且用途广泛。通常,它们用于将一个空间映射到一个更小的空间。当然,这意味着两个对象可能会散列到相同的值(碰撞),但这是因为您正在减少空间(鸽笼原理)。函数的效率很大程度上取决于哈希空间的大小。
令人惊讶的是,许多 Java hashCode 函数正在使用乘法来生成新对象的哈希码,如下所示(create-a-hashcode-method-java)
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
Run Code Online (Sandbox Code Playgroud)
如果我们想在同一范围内混合两个哈希码,xor 应该比加法好得多,我认为传统上是这样使用的。如果我们想增加空间,移动一些字节然后异或仍然是有意义的。我想乘以 31 几乎与将一个哈希值移动 1 然后添加相同,但它的效率应该低得多......
虽然这是推荐的方法,但我想我错过了一些东西。所以我的问题是为什么会这样?
笔记:
我有一个函数,它接受一个数组,并返回一个对象,该对象的键是使用数组中的值设置的。
简化示例:
// arr will be an array of arrays of type [string] *or* [string, string]
function foo(arr) {
const output = {}
arr.forEach(value => {
// if this value has length 2, use the second string as the key
const key = value.length === 1 ? value[0] : value[1]
// always use the first string as the value
const value = value[0]
output[key] = value
})
return output
}
Run Code Online (Sandbox Code Playgroud)
例如,运行foo([['a', 'orange'], ['b'], ['c', 'apple'])将产生{ orange: 'a', …
这是C++ lambda 的寿命是否有限的后续问题?。
在我之前的问题中,尝试在 lambda 超出范围后使用它会导致崩溃。虽然这并不奇怪,但我认为我使用了 lambda 存储位置的错误思维模型,这让我认为它永远存在,即使它超出了范围。
也许是因为我经常使用微控制器,其中代码从非易失性闪存执行,这与数据存储器(例如堆栈)完全分开。但我想象创建 lambda 相当于显式编写一个独立函数,但语法更方便。IE:
void SaySomething()
{
cout << "Hello" << endl;
}
int main()
{
AcceptFunction([](){cout << "Hello" << endl;}); // I thought that this
AcceptFunction(SaySomething); // was the same as this
}
Run Code Online (Sandbox Code Playgroud)
我想象的是 lambda 编译为与其他非 lambda 代码一起静态存在的实际代码,并AcceptFunction传递一个指向该代码的指针。在这种情况下,该代码将始终存在于内存中,即使 lambda 超出范围也是如此。
但我的程序崩溃了,所以这不可能是真的。那么该代码在哪里?它在堆栈上吗?如果是这样,在具有闪存的架构(代码无法从堆栈中运行)上,行为是否会有所不同?
更新
请注意,这个问题是关于 lambda 的物理存储位置。我不希望行为有所不同,也不希望 lambda 的析构函数不被调用。
java ×4
c++ ×3
c++11 ×2
arguments ×1
bouncycastle ×1
c ×1
for-loop ×1
function ×1
generics ×1
hash ×1
hashcode ×1
lambda ×1
loops ×1
nested-loops ×1
public-key ×1
r ×1
regex ×1
stdhash ×1
stdmap ×1
stdset ×1
typescript ×1
unit-testing ×1
variables ×1