简体   繁体   中英

How to get input separated by space and store it in a vector in c++

I have a class having three instances, one integer - noOfCouples and two vectors - womenHotness and menHotness I have to first take the input noOfCouples and then according to my first input i have to take input separated by spaces and create the two vectors

So far i have done this

#include <iostream>
#include <vector>

class hotness {

    private:
        int noOfCouples;
        std::vector<int> womenHotness;
        std::vector<int> menHotness;

    public:
        void setNoOfCouples(int num);
        void setWomenHotness(std::vector<int>& hotness);
        void setMenHotness(std::vector<int>& hotness);
        int getNoOfCouples();
        std::vector<int> getWomenHotness();
        std::vector<int> getMenHotness();

};

void hotness::setNoOfCouples(int num) {
    noOfCouples = num;
}

void hotness::setMenHotness(std::vector<int> &hotness) {
    menHotness.swap(hotness);
}

void hotness::setWomenHotness(std::vector<int> &hotness) {
    womenHotness.swap(hotness);
}

int hotness::getNoOfCouples() {
    return noOfCouples;
}

std::vector<int> hotness::getMenHotness() {
    return menHotness;
}

std::vector<int> hotness::getWomenHotness() {
    return womenHotness;
}

int main() {
    int t, i = 0, noc, k = 0, output;
    std::vector<int> women(1000);
    std::vector<int> men(1000);
    std::cin >> t;
    hotness input[1000];
    while(i < t) { // this loop is just for test cases
        std::cin >> noc;
        input[i].setNoOfCouples(noc);
        k = 0;
        std::cout << "i = " << i << " k = " << k << "\n";
        while(k<noc) {
            std::cin >> men[k];
            std::cin >> women[k];
            k++;
        }
        input[i].setMenHotness(men);
        input[i].setWomenHotness(women);
        i++;
    }
}

but while debugging i am getting EXC_BAD_ACCESS ie i guess my code is trying to access unassigned address to my vector

Is it the right way to assign take the input and assign into a new vector, or is there any mistake in my code

Please suggest most efficient way

Thanks in advance

You get the error because setMenHotness() and setWomenHotness() use the vector::swap() function.
This will swap the contents of the men vector with the menHotness vector.
So at the first iteration of the while(i < t) loop, after the call to the swap function, the menHotness vector will contain 1000 ints and the men vector will contain none. Then, at the second iteration, you will call std::cin >> men[k]; .This will try to store an integer in men[0] but men[0] does not exist anymore and so you get the error.

I would change the setMenHotness and setWomenHotness functions to use the = operator, ie:

void hotness::setMenHotness(std::vector<int> &hotness) {
    menHotness = hotness;
}
void hotness::setWomenHotness(std::vector<int> &hotness) {
    womenHotness = hotness;
}

and then change the main function like this:

int main() {
    int t, i = 0, noc, k = 0, output;
    int user_input;
    std::vector<int> women;
    std::vector<int> men;
    std::cin >> t;
    hotness input[1000];
    while(i < t) { // this loop is just for test cases
        std::cin >> noc;
        input[i].setNoOfCouples(noc);
        k = 0;
        std::cout << "i = " << i << " k = " << k << "\n";
        while (k < noc) {
            std::cin >> user_input;
            men.push_back(user_input);
            std::cin >> user_input;
            women.push_back(user_input);
            k++;
        }
        input[i].setMenHotness(men);
        input[i].setWomenHotness(women);
        i++;
    }
}

I got the reason for EXC_BAD_ACCESS, it was because of the temporary vector men and women was not getting new instance as it was initiated before the loop for input

Update in the main

int main() {
    int t, i = 0, noc, k = 0, output;
    std::cin >> t;
    hotness input[1000];
    while(i < t) {
        std::cin >> noc;
        std::vector<int> women(1000);
        std::vector<int> men(1000);
        input[i].setNoOfCouples(noc);
        k = 0;
        while(k<noc) {
              std::cin >> men[k];
              k++;
        }
        k = 0;
        while(k<noc) {
            std::cin >> women[k];
            k++;
        }
        input[i].setMenHotness(men);
        input[i].setWomenHotness(women);
        i++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM