向量/数组到整数

发布于 2024-12-11 09:01:47 字数 1259 浏览 0 评论 0原文

-(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' 和 '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 技术交流群。

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

发布评论

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

评论(1

隔纱相望 2024-12-18 09:01:47

c 没问题。正如编译器所说,问题确实出在 double value = .. 中。你有指针,所以你不能像这样访问数组的元素:

double value = yVal[c-1] + ...

它必须是

double value = (*yVal)[c-1] +

相同的xyValxxVal等。你需要修复整个内部<代码>for循环。


但是为什么你像这样分配向量......?有什么理由使用new吗?这太容易出错了。我会使用 just

vector<double> xVar;

而不是

xVal = new vector<double>();

And then use . 而不是 ->* 结合使用。就容易多了。


啊,忘记了 c 的问题 - 不,它不应该是 double。您不能使用浮点数作为索引。另外,如果 xVal 应该包含整数(以便它们可用于索引),为什么不将 vector 声明为 vectorint > 而不是 vector<双>?我不知道你的程序中的逻辑是什么,但看起来它(逻辑)应该改进,IMO。

The c is fine. The problem really is in double value = .., as your compiler says. You have pointers, so you can't access the array's elements like this:

double value = yVal[c-1] + ...

It must be

double value = (*yVal)[c-1] +

The same for xyVal, xxVal, etc. You need to fix the whole inner for loop.


But why you allocate the vectors like this...? Is there any reason to use new? This is so error prone. I'd use just

vector<double> xVar;

instead of

xVal = new vector<double>();

And then use . instead of -> combined with *. It so much easier.


Ah, forgot about the question for c - no, it should not be double. You can't use floating point numbers for indices. Also, if xVal is supposed to contain integer numbers (so that they can be used for indices), why don't you just declare the vector as vector< int > instead of vector< double >? I don't what's the logic in your program, but it looks like it(the logic) should be improved, IMO.

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