简体   繁体   English

我的 c++ 代码不允许我输入所有输入值,我不知道为什么

[英]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.因此,此任务来自 kattis 网站(链接: https://open.kattis.com/problems/everywhere ),目标是获取某人访问过的城市数量,问题是访问过其中一些城市两倍甚至更多,所以我们唯一需要的是获得城市的数量。

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.而且我的代码不允许我输入整个输入,我的意思是,我输入了 3 个值,然后程序停止工作。 Can you help me?你能帮助我吗?

You forgot to initialize your vector with a size.您忘记使用大小初始化向量。 You can do it by passing n to the constructor:您可以通过将n传递给构造函数来做到这一点:

vector<string> cities(n);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的代码不允许我将值输入到字符串变量中,而其他用户输入其他变量? C++ - Why is my code not allowing me to input values into a string variable with other user inputs for other variables? C++ 我的 c++ 代码不允许我输入变量 - My c++ code isn't letting me input a variable C ++ getline函数不允许我输入 - c++ getline function does not let me input c++, &#39;avg = sum/5&#39; 给了我垃圾值,但写 avg = sum/2 给出了工作,我不知道为什么 - c++, 'avg = sum/5' gives me junk values but writing avg = sum/2 gives works and i don't know why 为什么我不能在C ++中输入我的std :: vector - Why I can't input into my std::vector in C++ 我对“团队”codeforces 任务有疑问,不明白为什么我的 c++ 代码有效而无效 - I have a problem with “team” codeforces task, don't understand why my c++ code does work and doesn't 我的 N 皇后问题一直有效,直到 n = 5,我不知道为什么 C++ - My N Queen Problem works up until n = 5 and I don't know why C++ 我的 C++ 应用程序在输入一个值(std::cin >> 值)后关闭,我不知道为什么 - My C++ application closes after entering a value (std::cin >> value) and I don't know why 不知道C++中这段代码是什么意思 - I don't know the meaning of this piece of code in C++ 为什么Visual Studio(2017; 15.7.5)不允许我更改C ++关键字的颜色 - Why Visual Studio (2017; 15.7.5) don't let me change c++ keywords color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM