简体   繁体   English

字符串/整数向量-cout

[英]string/integer vector - cout

I tried to make a program that will ask the user to input pancakes eaten by 10 people and then list them. 我试图制作一个程序,要求用户输入10个人吃过的煎饼,然后列出。 People's names and pancakes eaten were stored in different vectors. 人们食用的名字和薄煎饼存储在不同的向量中。 I am getting an error when printing the values from the vectors. 从向量打印值时出现错误。

#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;
}

The error I'm getting is "Subscripted value is not an array.". 我收到的错误是“下标的值不是数组”。

You cannot add a std::string and an int , so this is not allowed 您不能添加std::stringint ,因此这是不允许的

name.push_back("Person " + x);

You can, however, use std::to_string and then concatenate. 但是,您可以使用std::to_string 然后进行连接。

name.push_back("Person " + std::to_string(x));

Also I'm not sure why you have <bits> includes, you should only have 另外我不确定为什么有<bits> includes,应该只包含

#include <iostream>
#include <string>
#include <vector>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM