cipisi 2023-01-08 14:51 采纳率: 87.5%
浏览 20
已结题

用vector存储信息,编译时异常

用vector存储信息,编译时异常

#include<iostream>
#include<vector>
using namespace std;
 
class xinxi
{
public:
    void xingxi(const char* l,int a, double b,const char* c)
    {
        vector<const char*>a4;
        a4.push_back(l);
        vector<int>a1;
        a1.push_back(a);
        vector<double>a2;
        a2.push_back(b);
        vector<const char*>a3;
        a3.push_back(c);
        cout << "库存的水果信息如下:" << endl;
        cout << a4.at(1) << "  " << endl;
        cout << a1.at(1) <<"kg"<< "  " << endl;
        cout << a2.at(1) << "元"<<"  " << endl;
        cout << a3.at(1) << "  " << endl;
 
        cout << a4.at(2) << "  " << endl;
        cout << a1.at(2) <<"kg"<<  "  " << endl;
        cout << a2.at(2) << "元"<< "  " << endl;
        cout << a3.at(2) << "  " << endl;
 
        cout << a4.at(3) << "  " << endl;
        cout << a1.at(3) <<"kg"<<  "  " << endl;
        cout << a2.at(3) << "元"<< "  " << endl;
        cout << a3.at(3) << "  " << endl;
    }
};
int main()
{
    xinxi q;
    q.xingxi("红美人", 100, 15.3, "象山");
    q.xingxi("车厘子", 78, 48.8, "智利");
    q.xingxi("香梨", 132, 7.8, "新疆");
    return 0;
}

错误信息:错误信息:0x00007FFE5A5ACD29 处(位于 储存库存信息.exe 中)有未经处理的异常: Microsoft C++ 异常: std::out_of_range,位于内存位置 0x00000010D9DBF410 处。

  • 写回答

1条回答 默认 最新

  • m0_54204465 2023-01-08 15:27
    关注

    帮你改了代码:

    #include<iostream>
    #include<vector>
    using namespace std;
    
    class xinxi
    {
    public:
        vector<const char*> name;
        vector<int> weight;
        vector<double> price;
        vector<const char*> origin;
    
        void xingxi(const char* l,int a, double b,const char* c)
        {
            name.push_back(l);
            weight.push_back(a);
            price.push_back(b);
            origin.push_back(c);
        }
    
        void print_info()
        {
            cout << "库存的水果信息如下:" << endl;
            for (int i = 0; i < name.size(); i++)
            {
                cout << name[i] << "  " << endl;
                cout << weight[i] <<"kg"<< "  " << endl;
                cout << price[i] << "元"<<"  " << endl;
                cout << origin[i] << "  " << endl;
            }
        }
    };
    int main()
    {
    xinxi q;
    q.xingxi("红美人", 100, 15.3, "象山");
    q.xingxi("车厘子", 78, 48.8, "智利");
    q.xingxi("香梨", 132, 7.8, "新疆");
    q.print_info();
    return 0;
    }
    

    在这个修改后的版本中,每次调用函数xinxi时都会将水果信息存储在类的成员变量中。然后,使用函数print_info输出所有水果的信息。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月16日
  • 已采纳回答 1月8日
  • 创建了问题 1月8日

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持