如何获取C++动态数组的大小

2024-05-27

我正在学习 C++,我需要创建结构Airplane并与之合作。

我的结构飞机.h

#include "stdafx.h"
using namespace std;

struct Airplane {
    string destination;
    int number;
    string type;
};

这是我的代码

#include "stdafx.h"
#include "Airplane.h"

string SetDestination(int n);
string SetType(int n);
void PrintAirplaneList(Airplane * &airplaneList, int n, string title);
void SortByDestination (Airplane *&airplaneList, int n);
void FindAirplanesAndPrint(Airplane *&airplaneList, int n, string type);

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;

srand((unsigned)time(NULL));

int n;
cout << "Input n = ";
cin >> n;

Airplane * airplaneList = new Airplane[n];

for (int i = 0; i < n; ++i)
{
    airplaneList[i].destination = SetDestination(rand()%5);
    airplaneList[i].number = rand()%9001 + 1000;
    airplaneList[i].type = SetType(rand()%3);
}

PrintAirplaneList(airplaneList, n, "List:");
SortByDestination (airplaneList, n);
PrintAirplaneList(airplaneList, n, "Sorted list (by destination):");

string type;
cout << "Input type: ";
getline(cin, type);
FindAirplanesAndPrint(airplaneList, n, type);

delete [] airplaneList;

system("PAUSE");
return 0;
}

string SetDestination (int n)
{
    string destination;
    switch(n){
    case 0: destination = "Tokio"; break;
    case 1: destination = "Amsterdam"; break;
    case 2: destination = "Moscow"; break;
    case 3: destination = "Philadelphia"; break;
    case 4: destination = "San Diego"; break;
    default: destination = "Unknown city"; break;
    }
    return destination;
}

string SetType (int n)
{
    string type;
    switch(n){
    case 0: type = "passenger"; break;
    case 1: type = "cargo"; break;
    case 2: type = "post"; break;
    default: type = "unknown type"; break;
    }
    return type;
}

void PrintAirplaneList(Airplane *&airplaneList, int n, string title)
{
    cout << "\n";
    cout << title << "\n\n";
    for (int i = 0; i < n; ++i)
    {
        cout << "Destination: " << airplaneList[i].destination << "\n";
        cout << "Number: " << airplaneList[i].number << "\n";
        cout << "Type: " << airplaneList[i].type << "\n\n";
    }
}

void SortByDestination (Airplane *&airplaneList, int n)
{
    for (int i = 0; i < n - 1; ++i)
    {
        for (int j = 0; j < n -1; ++j)
        {
            if(airplaneList[j + 1].destination > airplaneList[j].destination) continue;
            Airplane tempAirplane = airplaneList[j];
            airplaneList[j] = airplaneList[j + 1];
            airplaneList[j + 1] = tempAirplane;
        }
    }
}

void FindAirplanesAndPrint(Airplane *&airplaneList, int n, string type) {
    cout << "Type - " << type << "\n";
    int count = 0;
    for (int i = 0; i < n; ++i)
    {
        if (airplaneList[i].type == type)
        {
            cout << "Destination: " << airplaneList[i].destination << "\n";
            cout << "Number: " << airplaneList[i].number << "\n";
            ++count;
        }
    }
    if (count == 0)
    {
        cout << "Not found\n";
    }
}

我有两个问题。
1.我无法输入文字

string type;
cout << "Input type: ";
getline(cin, type);
FindAirplanesAndPrint(airplaneList, n, type);

和我的功能FindAirplanesAndPrint开始工作时没有任何类型值。如何让我的程序获得价值?
2. 如何在函数中获取动态数组的大小?因为看起来数组的传递大小n在每个函数中都是错误的方式。


“如何在函数中获取动态数组的大小?因为它似乎是数组的传递大小n在每个功能中都是错误的方式。”

然而,当您使用动态分配的 C 样式数组时,这是唯一的方法。

如果您想避免显式发送大小,则传递一些包装此原始内存缓冲区的对象并提供其他检索大小的方法。这里最合理的解决方案是使用std::vector<Airplane>.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取C++动态数组的大小 的相关文章

随机推荐

  • 如何使自定义对话框透明?

    我需要使我的自定义对话框透明 我怎样才能做到这一点 您还可以在构造函数中使用以下行 getWindow setBackgroundDrawable new ColorDrawable 0
  • 未使用 form_with 显示时出错

    早上好 我正在按照教程进行操作http edgeguides rubyonrails org getting started html http edgeguides rubyonrails org getting started html
  • 如何缩放 CAShapeLayer

    我很快就成功制作了动画bezier path它包含在一个CAShapeLayer 我唯一的问题是将其实现到其他屏幕尺寸上 有谁知道我如何扩展CAShapeLayer里面有路径吗 即使其变为原始大小的一半 使用这个函数 var shapela
  • 将 1d 数组索引转换为 3d 数组索引?

    我有一个 int 我想将其转换为 3d 数组索引的 3 个 int 这是我正在处理的示例 byte array new byte XSize YSize ZSize int i 0 other code array cur other co
  • 使用 NXP 读卡器库读取 NFC Mifare 卡

    我正在尝试使用 NFC 读卡器库读取 Mifare Ultralight 卡的内容 我对 NFC 完全陌生 正在使用这个 github 存储库 https github com JohnMcLear NXP Raspberry Pi Car
  • 如何使用 React 和 Jest 模拟 onPaste 事件?

    我正在尝试在我的反应项目上使用 JEST 测试来模拟粘贴事件 我有一个外部组件 App 其中包含带有 onPaste 事件的输入字段 我想测试过去的数据并检查输入值 it on past with small code gt Create
  • 重用 R 中内置的模型

    在 R 中构建模型时 如何保存模型规范以便可以在新数据上重用它 假设我根据历史数据建立逻辑回归 但直到下个月才会有新的观察结果 最好的方法是什么 我考虑过的事情 保存模型对象并在新会话中加载 我知道某些模型可以使用 PMML 导出 但还没有
  • Chrome 和 Firefox 不会在 iframe 中显示大多数 PDF 文档

    我运行的是 Windows 7 64 位 我最近更新了操作系统以安装最新更新 从那时起 大多数 PDF 文档已停止在我的程序中呈现 我的程序想要在单独的 iFrame 中显示 PDF 文档 我通过获取 PDF 文档的 URL 并设置来做到这
  • 如何在 AngularJS/Bootstrap3 中禁用基于复选框的内容

    我正在创建一个表单 用户可以在其中配置重复事件 因此有大量控件 顶部是一个用于启用 禁用计划的复选框 如何禁用但不隐藏基于复选框的整个部分 如果选中 用户应该能够对时间表进行修改 如果没有检查 则不允许进行任何更改 我相当确定我可以在每个控
  • 无法解析配置“:app:_debugCompile”的所有依赖项

    我正在尝试在我的 Android 应用程序上使用 Google 地图 我安装了 Google Play 服务并获取了 API 密钥 我不明白为什么我会收到此错误消息 我认为它抱怨以下内容
  • PhpStorm IDE:Ubuntu 18 LTS 中光标卡在文本选择(突出显示)模式

    我正在使用 PhpStorm 来处理 Laravel 项目 但是 鼠标卡在文本突出显示模式下并且右键单击在 IDE 上不起作用 关闭IDE并重新启动并不能解决问题 我试图在网上寻找答案但没有成功 对于那些像我一样苦苦挣扎的人 我有一个解决方
  • 您使用什么来通过其自定义协议来测试(功能/负载/压力)您的网络服务?

    我最近创建了一个回合制游戏服务器 可以接受数十万个并发客户端连接 长话短说 Linux 上的 epoll 通信基于简单 定制 基于线路的协议 该服务器允许客户端连接 寻找游戏比赛中的其他玩家 玩所述游戏 发送动作 聊天消息等 并在游戏结束时
  • Rails 创建 schema_migrations - Mysql2::Error: 指定的键太长

    我正在使用Rails 3 2 6和Mysql 6 0 9 但我在MySQL 5 2 25上有完全相同的错误 当我创建新数据库时 rake db create 然后当我尝试加载架构时 rake schema load 我收到此错误 Mysql
  • 如何在 JavaScript 中检查 IsPostBack? [复制]

    这个问题在这里已经有答案了 我需要运行 JavaScript 函数 onLoad 但仅在页面第一次加载时才执行 即不是回发的结果 基本上 我需要检查 JavaScript 中的 IsPostBack 谢谢 服务器端 写 if IsPostB
  • PouchDB 和 CouchDB 的 Cookie 身份验证并未实际登录用户

    我有一个节点应用程序 它使用 CouchDB 作为数据库 在客户端使用 PouchDB 要登录 将向节点服务器发送请求 节点服务器使用 CouchDB 实例对用户的凭据进行身份验证 然后将 cookie 发送回客户端 问题是 重新启动浏览器
  • Docker Compose - 如何执行多个命令?

    我想做这样的事情 我可以在以下代码中运行多个命令 db image postgres web build command python manage py migrate command python manage py runserver
  • JScript:如何运行外部命令并获取输出?

    我正在使用 cscript exe 运行 JScript 文件 在脚本中我需要调用外部console命令并获取输出 Tried var oShell WScript CreateObject WScript Shell var oExec
  • 如何对 nginx ingress 中的特定 HTTP 方法进行基本身份验证?

    我可以使用基本身份验证创建入口 我遵循 kubernetes ingress nginx 中的模板 apiVersion extensions v1beta1 kind Ingress metadata name ingress with
  • Html 视频播放器播放声音但不播放视频

    我正在制作网页 并尝试插入视频 视频播放器加载正常 您可以按播放 但只播放音频 而不播放视频 在 Chrome 中预览时 在 firefox 中查看时可以正常工作 我的代码如下
  • 如何获取C++动态数组的大小

    我正在学习 C 我需要创建结构Airplane并与之合作 我的结构飞机 h include stdafx h using namespace std struct Airplane string destination int number