如何获得大小C ++动态数组 [英] How to get size c++ dynamic array

查看:115
本文介绍了如何获得大小C ++动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C ++,我需要建立结构航空并使用它。

I'm studying C++ and I need to create structure Airplane and work with it.

我的结构的 Airplane.h

#include "stdafx.h"
using namespace std;

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

和这是我的code

#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.我不能输入型

I have two questions.
1. I can't input type in

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

和我的功能 FindAirplanesAndPrint 启动,但不为任何类型值,正常工作。如何使我的PROGRAMM获得价值?结果
2.如何在功能动态数组大小?因为它似乎在每一个函数数组 N 的传球大小是错误的方式。

and my function FindAirplanesAndPrint starts to work without any value for type. How to make my programm to get value?
2. How to get size of dynamic array in functions? Because it seems the passing size of array n in every function is the wrong way.

推荐答案

1)Ommiting无关,这基本上是你得到了什么:

1) Ommiting the irrelevant, this is basically what you got:

cin >> n;
getline(cin, type);

运营商的GT;&GT; 叶输入缓冲区的新行字符,这就是第一个字符是函数getline 看到。由于的'\\ n'是默认的行分隔符,你会得到一个空行。要修复它叫 cin.ignore()你打电话之前函数getline 丢弃'\\ N'

operator>> leaves a new-line character in the input buffer and that's the first character that getline sees. Since '\n' is the default line delimiter, you get an empty line. To fix it call cin.ignore() before you call getline to discard the '\n'.

2)如果你想坚持使用裸指针,传递大小作为一个参数是你唯一的选择。切换到的std ::矢量,你会得到你可以随时查询尺寸()方法。

2) If you wish to stick with raw pointers, passing the size as a parameter is your only choice. Switch to std::vector and you get size() method that you can query at any time.

这篇关于如何获得大小C ++动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆