[英]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.