来自 C++ 的有关 Scala 的问题程序员(结构和 stl)
我在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不想初始化它?为了效率?恐怕 JVM 不会让你在变量中根据最初的内容添加随机垃圾。因此,既然您无论如何都必须初始化它,为什么不指定您的“未初始化”值是什么?
(如果稍后需要查看该值是否已初始化,您可以使用
Double.NaN
并检查point.isNaN
。)您可以使用
_
作为默认初始化,但除非您在通用代码中使用它:否则您只是模糊了该值的实际设置。 (或者你宣布“我真的不在乎这里发生什么,它可以是任何东西”——这可能很有用。)
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?
(You could use
Double.NaN
and check forpoint.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: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.)
我刚刚找到了 intialistion 的答案:
Puh Scala 有很多语法需要习惯:-)
有人有 lower_bound 等效项的解决方案吗?
I just found the answer to the intialistion:
Puh Scala has a lot of syntx to get used to :-)
Anyone have a solution for the lower_bound equivalent ?
很难有效地翻译,因为你在伪代码后面留下了很多未知的东西,但我提倡这样的做法:
如果你能更充分地充实你的示例代码,我将能够给出更好的翻译。
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:
If you can flesh out your sample code a bit more fully, I'll be able to give a better translation.