[英]Why is my code not allowing me to input values into a string variable with other user inputs for other variables? 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';
}
}
因此,此任務來自 kattis 網站(鏈接: https://open.kattis.com/problems/everywhere ),目標是獲取某人訪問過的城市數量,問題是訪問過其中一些城市兩倍甚至更多,所以我們唯一需要的是獲得城市的數量。
這就是輸入的樣子:
7
saskatoon
toronto
winnipeg
toronto
vancouver
saskatoon
toronto
3
edmonton
edmonton
edmonton
而且我的代碼不允許我輸入整個輸入,我的意思是,我輸入了 3 個值,然后程序停止工作。 你能幫助我嗎?
您忘記使用大小初始化向量。 您可以通過將n
傳遞給構造函數來做到這一點:
vector<string> cities(n);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.