204基于顺序存储结构的图书信息表的创建和输出

该博客讨论了如何基于顺序存储结构创建图书信息表,提到了在C++中使用string类、memcpy函数和sizeof操作符。作者指出,虽然当前实现能够正确运行,但在特定情况下(如动态内存分配)可能存在问题,并强调了new和delete在数组操作中的正确用法,即使用delete []来释放new []分配的空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

解答:

#include<iostream>
using namespace std;
#include<string>//string类头文件
#include<string.h>//memcpy头文件
long long  MAXSIZE = 1000;

struct BOOK {
    string ISBN;
    string name;
    float price;
};

int main() {

    BOOK* book = new BOOK[MAXSIZE];
    int currentPos = 0;

    string input_ISBN;
    string input_name;
    float input_price;

    while (true) {
        cin >> input_ISBN >> input_name >> input_price;
        if ("0" == input_ISBN) {
            break;
        }
        else {

            if (currentPos == MAXSIZE) {//原数组空间不够,则将空间扩大至二倍
                BOOK* temp = new BOOK[MAXSIZE];
                memcpy(temp, book, sizeof(book));
                delete []book;
                MAXSIZE *= 2;
                book = new BOOK[MAXSIZE];
                memcpy(book,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值