首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Vectors编写算法的问题

使用Vectors编写算法的问题
EN

Stack Overflow用户
提问于 2011-10-20 00:13:08
回答 2查看 188关注 0票数 0
代码语言:javascript
复制
-(void)userShow{
    vector<CGPoint>::iterator it;
    vector<CGPoint>* xp = x.graphPoints;
    vector<CGPoint>* yp = y.graphPoints;
    xVal = new vector<double>();
    yVal = new vector<double>();
    xyVal = new vector<double>();
    xxVal = new vector<double>();
    value = new vector<double>();
    c = 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);
    }

    for (int i = 0; i < xVal->size(); i++){
        c = xVal[i];
        while (xyVal[c] < xxVal[i];){
            c++;
            if ((c-1)<=xxVal[i]<=c){
                double value = xp[c-1] + (xp[c] - yp[c-1])*(xxVal[i] - xyVal[c-1])/(xyVal[c] - xyVal[c-1]);
            }
            yVal->push_back(value);
        }
    }

   UserGraph->removeAllData();
   UserGraph->addDataSet(xVal, yVal, [UIColor redColor], 0, false, true, 2);
   UserGraph->updateAll();
}

上面是我想要发生的事情的伪代码。我仍然在理解向量上有问题。正如您可以在上面看到的yVal = "...“vector<CGPoint>vector<double>的二进制表达式有一个问题。

这个算法应该做的是在两个图x(t)y(t)上画一条线,然后获取x(t)y坐标并将其转换为新的向量。在第二个while中,它使用x(t)x坐标与y(t) x坐标进行比较来获取y坐标。当D12的x和D13的x和D14不匹配时,它需要执行D15=算法。

有人能帮我把伪代码变成工作代码吗?干杯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-20 00:30:57

在你的代码中有一些谜团,但是像这样的东西希望能让你开始。我删除了指向vector的指针,并在一行中添加了一些注释来解释我的更改。

代码语言:javascript
复制
void userShow() {

    // I assume that x.graphPoints is just some `std::vector<CGPoint>` and you just want to use it locally

    // if x.graphPoints returns an "std::vector<CGPoint> *" (pointer-to-vector), 
    // you should probably modify the class/struct/whatever to just use the vector,
    // not a pointer-to-vector
    vector<CGPoint>& xp = x.graphPoints;

    // ditto for y.graphPoints
    vector<CGPoint>& yp = y.graphPoints;

    // You almost never use pointers to containers, nor allocate them with new - 
    // it's an atypical practice in C++
    /* 
    xVal = new vector<double>();
    yVal = new vector<double>();
    xyVal = new vector<double>();
    */

    // instead just create the vectors on the stack
    std::vector<double> xVal, yVal, xyVal;

    std::vector<CGPoint>::iterator it;

    // These have been changed to not use -> member notation, since we're not
    // using pointers anymore
    for(it = xp.begin(); it != xp.end(); ++it){
        xVal.push_back(it->y);
        xxVal.push_back(it->x); // I have no idea what xxVal is? I think it's xyVal?
        // xyVal.push_back(it->x); // like this?
    }

    // you can iterate through a vector with this type of loop, or
    // use an iterator as above
    for (int i = 0; i < xp.size(); ++i){
        int c = 1;
        while (xyVal[c] < xxVal[i]) {
            ++c;

            // (c-1)<=xxVal[i]<=c; // ??? 

            // I think the previous line means...c gets the value of xyVal[i], and
            // xxVal gets c-1? You'll have to explain this, otherwise it it just a
            // free-standing conditional comparison

            // EDIT: I think I understand what you mean
            // this was a conditional check to do the yVal stuff
            /**
            if ( (c-1) <= xxVal[i] && xxVal[i] <= c) {
                // yVal = xp[c-1] + (xp[c] - yp[c-1])*(xxVal[i]-xyVal[c-1])/(xyVal[c] - xyVal[c-1]);
            } */

            // as mentioned, yVal is a vector, so what do you want?
            yVal = xp[c-1] + (xp[c] - yp[c-1])*(xxVal[i]-xyVal[c-1])/(xyVal[c] - xyVal[c-1]);

        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2011-10-20 00:23:48

yVal = xp[c-1] + ....

yVal是指向双精度向量的点(您几乎可以肯定不会使用btw),而不是一个值

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7824476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档