lundi 4 mai 2015

Going through and inserting data to std vectors

I am strugling with this problem for a longer while now. I never had this kind of problems when using QVector, but unfortunetally I cannot use qt in this one... What I am trying to do is to go through 2 vectors which contains points X and Y as doubles. I am trying to insert some x and y points in between the points that are pre defined. The problem is that after second loop everything goes bad, iterators start to get weird values and the points are inserted in the 0 index all the time... This iterator thing is really confusing, I cant understand why cant i insert to my vectors simply using intiger indexes. I tried the C++11 way as well, but didnt give me anything. The m_x vector represents time in seconds and m_y vector represents temperature. m_x is double, but it can only take integer posive values.

int CProcessData::calculateMidPoints()
{
    if((0 == m_x.size()) || (0 == m_y.size()))
        return 1;

    std::vector<double>::iterator itX = m_x.begin();
    std::vector<double>::iterator itY = m_y.begin();
    double midPointY = 0;

    for (uint32_t i = 0; i < (m_x.size() - 1); i++)
    {
        if (i != m_x[i])
        {
            m_x.insert(itX, (double)i);
            midPointY = (m_y[i - 1] * 1 + m_y[i] * (1 / (m_x[i + 1] - m_x[i]))) /
                        (1 + (1 / (m_x[i + 1] - m_x[i])));
            m_y.insert(itY, midPointY);
        }
        else
        {
            itX++;
            itY++;
        }
    }

    return 0;
}

I would really aprichiate help in here.

Aucun commentaire:

Enregistrer un commentaire