FriedRike
FriedRike

Reputation: 145

clear and resize vector - c++

I'm trying to make a vector, then enter a while cycle, use that vector, then, resize the vector at the end of each cycle. So I decided to make my_vector.clear() at the end of the cycle and a my_vector.resize(newSize) at the beginning of it. Here's a sample code:

typedef struct my_type{
    int numNextDom;
    bool knockedDown;
} my_type;

int main (int argc, char * const argv[]) {
    int i, testCase, totalDom, numLines, num_dom=0, num_next_dom=0, aux_map_int = 0, linesScanned = 0;
    bool firstTime=true;

    std::cin>>testCase;
    std::vector<my_type> my_vector;

    while(aux_map_int < testCase){

        std::cin>>totalSize>>numLines;

        my_vector.resize(totalSize);

        while (linesScanned < numLines) {
            std::cin>>num_dom>>num_next_dom;

            if(firstTime){
                my_vector[0].numNextDom = totalSize;
                my_vector[0].knockedDown = true;
                firstTime=false;
            }
            my_vector[num_dom].numNextDom = num_next_dom;
            my_vector[num_dom].knockedDown = false;

            linesScanned++;
        }

        std::cout << giveResult(my_vector) << std::endl;
        aux_map_int++;
        my_vector.clear();
    }

    return 0;
}

So you see, after I make my_vector.clean() the vector becomes os size 1, but then, when the loop is re-done, my_vector stays of size 1 and doesn't do the my_vector.resize(totalSize). Am I missing anything?

Upvotes: 1

Views: 4663

Answers (1)

stanm
stanm

Reputation: 3321

I ran your code and the vector is correctly resized.

I think your problem is that you are not resetting linesScanned after the first test, so if totalSize is smaller on the second iteration of the loop than it was on the first one, you will not enter the body of the second loop at all. Even if it is bigger the second time, you will not get the behaviour you expect.

You can fix this issue by adding linesScanned = 0; as the first statement of the outer loop, i.e. just after while(aux_map_int < testCase){

Also, I suggest you use for loops, as they are cleaner and would help you avoid such mistakes in the future.

Here is the program that I tested:

#include <iostream>
#include <vector>

int totalSize = 0;

typedef struct my_type{
    int numNextDom;
    bool knockedDown;
} my_type;

int giveResult(std::vector<my_type>);

int main (int argc, char * const argv[]) {
    int i, testCase, totalDom, numLines, num_dom=0, num_next_dom=0, aux_map_int = 0, linesScanned = 0;
    bool firstTime=true;

    std::cin>>testCase;
    std::vector<my_type> my_vector;

    while(aux_map_int < testCase){

        std::cin>>totalSize>>numLines;

        my_vector.resize(totalSize);
        std::cout << "my_vector.size(): " << my_vector.size() << std::endl;
        std::cout << "linesScanned: " << linesScanned << std::endl;
        std::cout << "numLines: " << numLines << std::endl;

        while (linesScanned < numLines) {
            std::cin>>num_dom>>num_next_dom;

            if(firstTime){
                my_vector[0].numNextDom = totalSize;
                my_vector[0].knockedDown = true;
                firstTime=false;
            }
            my_vector[num_dom].numNextDom = num_next_dom;
            my_vector[num_dom].knockedDown = false;

            linesScanned++;
        }

        std::cout << giveResult(my_vector) << std::endl;
        aux_map_int++;
        my_vector.clear();
    }

    return 0;
}

int giveResult(std::vector<my_type>) {
    return 0;
}

And this is the interaction session from my console:

2
2 2
my_vector.size(): 2
linesScanned: 0
numLines: 2
5 5
6 6
0
3 2
my_vector.size(): 3
linesScanned: 2
numLines: 2

Here you can see that linesScanned is not reset and is going to cause a problem.

Here is correct (with respect to this problem) version of your main function:

int main (int argc, char * const argv[]) {
    int i, testCase, totalDom, numLines, num_dom=0, num_next_dom=0, aux_map_int = 0, linesScanned = 0;
    bool firstTime=true;

    std::cin>>testCase;
    std::vector<my_type> my_vector;

    while(aux_map_int < testCase){
        linesScanned = 0;
        std::cin>>totalSize>>numLines;

        my_vector.resize(totalSize);

        while (linesScanned < numLines) {
            std::cin>>num_dom>>num_next_dom;

            if(firstTime){
                my_vector[0].numNextDom = totalSize;
                my_vector[0].knockedDown = true;
                firstTime=false;
            }
            my_vector[num_dom].numNextDom = num_next_dom;
            my_vector[num_dom].knockedDown = false;

            linesScanned++;
        }

        std::cout << giveResult(my_vector) << std::endl;
        aux_map_int++;
        my_vector.clear();
    }

    return 0;
}

I hope that clears it up.

-Stan

Upvotes: 3

Related Questions