向量/数组到整数
-(void)userShow{
xVal = new vector<double>();
yVal = new vector<double>();
xyVal = new vector<double>();
xxVal = new vector<double>();
value = new vector<double>();
for(it = xp->begin(); it != xp->end(); ++it){
xVal->push_back(it->y);
xxVal->push_back(it->x);
}
for(it = yp->begin(); it != yp->end(); ++it){
xyVal->push_back(it->x);
yVal->push_back(it->y);
}
for (int i = 0; i < xVal->size(); i++){
int c = (*xVal)[i];
for(int i = 0; xyVal[i] < xxVal[i]; i++){
double value = yVal[c-1] + (yVal[c] - yVal[c-1])*(xxVal[i] - xyVal[c-1])/(xyVal[c] - xyVal[c-1]);
yVal->push_back(value);
}
}
}
我的代码的 double value = ...
部分存在问题。我收到三个错误,指出指向 c
的二进制表达式的无效操作数('vector
应该 int c = (*xVal)[i];
是 double c = (*xVal)[i];
当我尝试使用 double 时,我收到 6 个错误,表示 < code>数组下标不是整数。这意味着我需要将数组转换为整数。如果我使用向量,如何获得数组?只是目前有很多困惑。
不太确定我是否真的需要解释代码应该做什么,但如果有帮助的话。我试图得到它,所以它需要两个向量将向量 x 和 y 分成 x 和 y。然后取 xp 的 y 和 yp 的 y 并将它们放在一起。但因为 xp 和 yp 向量不匹配,我需要使用 for 循环和双值算法来获得一组不错的数字。
-(void)userShow{
xVal = new vector<double>();
yVal = new vector<double>();
xyVal = new vector<double>();
xxVal = new vector<double>();
value = new vector<double>();
for(it = xp->begin(); it != xp->end(); ++it){
xVal->push_back(it->y);
xxVal->push_back(it->x);
}
for(it = yp->begin(); it != yp->end(); ++it){
xyVal->push_back(it->x);
yVal->push_back(it->y);
}
for (int i = 0; i < xVal->size(); i++){
int c = (*xVal)[i];
for(int i = 0; xyVal[i] < xxVal[i]; i++){
double value = yVal[c-1] + (yVal[c] - yVal[c-1])*(xxVal[i] - xyVal[c-1])/(xyVal[c] - xyVal[c-1]);
yVal->push_back(value);
}
}
}
I am having an issue with the double value = ...
part of my code. I get three errors saying invalid operands to binary expression ('vector<double>' and 'vector<double>')
pointing to the c
.
should int c = (*xVal)[i];
be double c = (*xVal)[i];
when i try to use double i get 6 errors saying Array subscript is not an integer
. Which means I need to convert the array into an integer. How am I getting an array if I am using vectors? Just a lot of confusion at the moment.
Not really sure if i really need to explain what the code is supposed to do, but if it helps. I am trying to get it so it take two vectors splits the vectors x and y's into x and y. then take the y of xp and the y of yp and put them together. but because xp and yp vectors do not match i need to use the for loop and the double value algorithm to get a decent set of numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
c
没问题。正如编译器所说,问题确实出在double value = ..
中。你有指针,所以你不能像这样访问数组的元素:它必须是
相同的
xyVal
,xxVal
等。你需要修复整个内部<代码>for循环。但是为什么你像这样分配
向量
......?有什么理由使用new
吗?这太容易出错了。我会使用 just而不是
And then use
.
而不是->
与*
结合使用。就容易多了。啊,忘记了
c
的问题 - 不,它不应该是double
。您不能使用浮点数作为索引。另外,如果 xVal 应该包含整数(以便它们可用于索引),为什么不将vector
声明为vector
int >
而不是vector<双>
?我不知道你的程序中的逻辑是什么,但看起来它(逻辑)应该改进,IMO。The
c
is fine. The problem really is indouble value = ..
, as your compiler says. You have pointers, so you can't access the array's elements like this:It must be
The same for
xyVal
,xxVal
, etc. You need to fix the whole innerfor
loop.But why you allocate the
vector
s like this...? Is there any reason to usenew
? This is so error prone. I'd use justinstead of
And then use
.
instead of->
combined with*
. It so much easier.Ah, forgot about the question for
c
- no, it should not bedouble
. You can't use floating point numbers for indices. Also, ifxVal
is supposed to contain integer numbers (so that they can be used for indices), why don't you just declare thevector
asvector< int >
instead ofvector< double >
? I don't what's the logic in your program, but it looks like it(the logic) should be improved, IMO.