[英]string/integer vector - cout
我試圖制作一個程序,要求用戶輸入10個人吃過的煎餅,然后列出。 人們食用的名字和薄煎餅存儲在不同的向量中。 從向量打印值時出現錯誤。
#include <iostream>
#include <bits/stl_vector.h>
#include <bits/stl_bvector.h>
using namespace std;
int main() {
vector<int> pancakes;
vector<string> name;
int temp_num;
for (int x = 0; x < 10; x++) {
cout << "Enter pancakes eaten by person " << x+1 << endl;
cin >> temp_num;
pancakes.push_back(temp_num);
name.push_back("Person " + x);
}
for (int x = 0; x < 10; x++){
cout << name[x] << " ate " << pancakes[x] << " candies." << endl;
}
return 0;
}
我收到的錯誤是“下標的值不是數組”。
您不能添加std::string
和int
,因此這是不允許的
name.push_back("Person " + x);
但是,您可以使用std::to_string
然后進行連接。
name.push_back("Person " + std::to_string(x));
另外我不確定為什么有<bits>
includes,應該只包含
#include <iostream>
#include <string>
#include <vector>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.