来自 C++ 的有关 Scala 的问题程序员(结构和 stl)

发布于 2024-10-08 00:51:53 字数 904 浏览 1 评论 0原文

我在将 C++ 数据结构转换为 Scala 时遇到问题。 Scala 与 C++ 确实不同,但我很喜欢它。 我有以下 C++ 代码片段:

struct Output
{
    double point;
    double solution[6];
};

struct Coeff
{
    double rcont1[6];
    double rcont2[6];
    double rcont3[6];
    double rcont4[6];
    double rcont5[6];
    double rcont6[6];
};
std::list<Output> output;
std::list<Coeff> coeff;

我现在用数据填充 while 循环中的列表

while(n<nmax) {
    if step successfull
         Output out;
         out.point = some values;
         out.solution[0] = some value;
         output.push_back(out);
}

我尝试在 Scala 中创建一个简单的类来保存数据。

class Output
{
    var point: Double
    var solution: Array[Double] = new Array(6)
}

但这不起作用,因为点尚未初始化。有办法解决这个问题吗?我只想定义变量但不初始化它。

又是一件快事。我正在寻找与 stl::lower_bound 等效的东西。

是在已排序的容器中找到正确的位置插入元素以维持顺序。

感谢您帮助 Scala 初学者

I am having problems translating C++ data structures to Scala. Scala is really different from C++, but I like a lot of it.
I have the following Code fragment in C++:

struct Output
{
    double point;
    double solution[6];
};

struct Coeff
{
    double rcont1[6];
    double rcont2[6];
    double rcont3[6];
    double rcont4[6];
    double rcont5[6];
    double rcont6[6];
};
std::list<Output> output;
std::list<Coeff> coeff;

I now fill the list in a while loop with data

while(n<nmax) {
    if step successfull
         Output out;
         out.point = some values;
         out.solution[0] = some value;
         output.push_back(out);
}

I tried creating a simple class in Scala to hold the data.

class Output
{
    var point: Double
    var solution: Array[Double] = new Array(6)
}

But this doens't work since point is not initialized. Is there a way around this? I just want to define the variable but not initialize it.

Another quick thing. I am looking for an equivalent to stl::lower_bound.

Is finds the right position to insert an element in an sorted container to maintain the order.

Thanks for helping a Scala beginner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧笙踏歌 2024-10-15 00:51:53

为什么不想初始化它?为了效率?恐怕 JVM 不会让你在变量中根据最初的内容添加随机垃圾。因此,既然您无论如何都必须初始化它,为什么不指定您的“未初始化”值是什么?

class Output {
  var point = 0.0
  var solution = new Array[Double](6)
}

(如果稍后需要查看该值是否已初始化,您可以使用 Double.NaN 并检查 point.isNaN。)

您可以使用 _ 作为默认初始化,但除非您在通用代码中使用它:

class Holder[T] {
  var held: T = _
}

否则您只是模糊了该值的实际设置。 (或者你宣布“我真的不在乎这里发生什么,它可以是任何东西”——这可能很有用。)

Why don't you want to initialize it? For efficiency? I'm afraid that the JVM doesn't let you get away with having random junk in your variables based on whatever was there originally. So since you have to initialize it anyway, why not specify what your "uninitialized" value is?

class Output {
  var point = 0.0
  var solution = new Array[Double](6)
}

(You could use Double.NaN and check for point.isNaN if you later need to see whether the value has been initialized or not.)

You could use _ as the default initialization, but unless you use it in generic code:

class Holder[T] {
  var held: T = _
}

then you're just obscuring what the value really will be set to. (Or you are announcing "I really don't care what goes here, it could be anything"--which could be useful.)

月亮坠入山谷 2024-10-15 00:51:53

我刚刚找到了 intialistion 的答案:

class Output
{
    var point: Double = _
    var solution: Array[Double] = Array(6)
}

Puh Scala 有很多语法需要习惯:-)

有人有 lower_bound 等效项的解决方案吗?

I just found the answer to the intialistion:

class Output
{
    var point: Double = _
    var solution: Array[Double] = Array(6)
}

Puh Scala has a lot of syntx to get used to :-)

Anyone have a solution for the lower_bound equivalent ?

梦一生花开无言 2024-10-15 00:51:53

很难有效地翻译,因为你在伪代码后面留下了很多未知的东西,但我提倡这样的做法:

// type alias
type Coeff = Seq[Seq[Double]]

// parameters passed to a case class become member fields
case class Output (point: Double, solution: Seq[Double])

val outputs = (0 to nmax) map { n =>
    Output(generatePoint(n), generateSolution(n))
}

如果你能更充分地充实你的示例代码,我将能够给出更好的翻译。

It's hard to translate effectively, as you've left a lot of unknowns hidden behind pseudo code, but I'd advocate something along these lines:

// type alias
type Coeff = Seq[Seq[Double]]

// parameters passed to a case class become member fields
case class Output (point: Double, solution: Seq[Double])

val outputs = (0 to nmax) map { n =>
    Output(generatePoint(n), generateSolution(n))
}

If you can flesh out your sample code a bit more fully, I'll be able to give a better translation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文