Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this Google test assertions

ASSERT_FLOAT_EQ(longerVector[0], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[1], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[2], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[3], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[4], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[5], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[6], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[7], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[8], shorterVector[2]);

as you can see, there are two vectors, shorter with n elements and longer with n*n elements (in this case 3 and 9 respectively).

How could I create two nested for loops, which would make my code shorter and simpler?

I tried for example this but it failed. I understand why but I was not able to come up with anything better.

for(size_t i = 0; i < shorterVector.size(); i++)
{
    for(size_t j = 0; j < shorterVector.size(); j++)
    {
        ASSERT_FLOAT_EQ(longerVector(i*j), shorterVector(j);
    }
}

Thank you very much for any help and I apologize for maybe a dumb question.

question from:https://stackoverflow.com/questions/65952594/iterate-over-vectors-with-n-and-nn-elements

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
217 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use this instead

for(size_t i = 0; i < longerVector.size(); i++)
{
        ASSERT_FLOAT_EQ(longerVector[i], shorterVector[i%3]);  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...