简体   繁体   中英

my c++ code does not let me input all input values, and i don't know why

using namespace std;

int main() {
    int t;
    cin >> t;
    for (int j = 0; j < t; j++) {
        int n;
        cin >> n;
        vector<string> cities;

        for (int i = 0; i < n; i++) {
            cin >> cities[i];
        }
        int size = cities.size();
        for (int i = 0; i < n; i++) {
            for (int k = 1; k < n; k++) {
                if (cities[i] == cities[k]) size--;
            }
        } 
        cout << size << '\n';
    }
    
}

So, this task is from kattis website(link: https://open.kattis.com/problems/everywhere ) and the goal is to get the number of the cities someone has visited, the problem is that some of these cities were visited twice or even more, so the only thing we need is to get the number of cities.

That's how input looks like:

7
saskatoon
toronto
winnipeg
toronto
vancouver
saskatoon
toronto
3
edmonton
edmonton
edmonton

And my code doesn't let me input the whole input, I mean, I input 3 values and then program stops working. Can you help me?

You forgot to initialize your vector with a size. You can do it by passing n to the constructor:

vector<string> cities(n);

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