简体   繁体   English

字符串数组/字符数组

[英]String Arrays/Char Arrays

This is what I have to do: 这是我要做的:

A teacher has asked all her students to line up single file according to their first name. 一位老师要求她的所有学生根据他们的名字排列单个文件。 For example, in one class Amy will be at the front of the line and Yolanda will be at the end. 例如,在一个班级中,艾米将排在队伍的最前面,而尤兰达将在队伍的尽头。 Write a program that prompts the user to enter the number of students in the class, then loops to read in that many names. 编写一个程序,提示用户输入班级的学生人数,然后循环阅读其中的多个名称。 Once all the names have been read in it reports which student wourld be at the front of the line and which one would be at the end of the line. 阅读完所有名称后,它会报告哪个学生名列在行的开头,哪个学生将在行的末尾。 You may assume that no two students have the same name. 您可以假设没有两个学生具有相同的名字。 Input Validation: Do not accept a number less than 1 or greater than 25 for the number of students. 输入验证:接受的学生人数不得小于1或大于25。

This is what I have so far: 这是我到目前为止的内容:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    int StudentNum;

    cout << "How many student are in the class?\n";
    cin >> StudentNum;

    char sname[StudentNum + 1][25];
    if (StudentNum < 1 || StudentNum > 25)
    {
    cout << "Please enter a number between 1-25 and try again\n"; 
    return 0;
    }

    for (int i = 1; i <= StudentNum; i++); 
    {
        cout << "Please enter the name of student #" << i << endl;
        cin >> sname[i];        
    }   
    for (int output = 0; output <=StudentNum; output++);
    {
    cout << endl << sname[output] << endl;
    } 
    system ("pause");
    return 0;
}

Am I missing something about arrays?? 我是否缺少有关数组的信息?

You cannot create such an array because its length has to be known at compile time (ie, it cannot be the result of an expression such as StudentNum + 1 ). 您不能创建这样的数组,因为必须在编译时知道其长度(即,它不能是诸如StudentNum + 1之类的表达式的结果)。

You can solve this issue because by the problem definition you know an upper bound for the array size, so you can use that as a compile time constant. 您可以解决此问题,因为根据问题定义,您知道数组大小的上限,因此可以将其用作编译时间常数。

However, this problem can be solved without using an array at all. 但是,完全使用数组就可以解决该问题。 Read the wording carefully. 仔细阅读措辞。

Hint for the solution without arrays : Think of the array as a single piece of paper (variable) with all the names written one after another. 没有数组的解决方案的提示 :将数组想像成一张纸(变量),所有名称都一个接一个地写。 Not using an array then means that you have to be able to solve the problem without looking at all the names at once . 如果不使用数组,则意味着您必须能够解决问题,而不必一次查看所有名称 How would you come to the answer if I only allowed you to see the names one by one ? 会怎样来回答,如果我只允许你看到的名字一个接一个

Another hint: The problem is still solvable if there were several trillion students in the class (with unique names no less), ie more than could possibly fit in the computer's memory at any one time. 另一个提示:如果班上有几万亿名学生(唯一的名字不少于其他),也就是这个问题仍然可以解决,即在任何时候都超过了计算机的内存。

C++ array dimensions must be known at compile time (ie not dependent on user-entered variables at run-time). 必须在编译时知道C ++数组的维数(即在运行时不依赖于用户输入的变量)。 Use strings instead: 使用字符串代替:

string sname[25];

If you were using something besides char arrays, you could also use a vector . 如果您使用的不是char数组,则还可以使用vector

Think about what the problem statement is actually asking for. 考虑问题陈述的实际要求。 Your program only needs to output the first and last names alphabetically. 您的程序只需要按字母顺序输出名字和姓氏。 Do you actually need to store all the names to do that? 您实际上需要存储所有名称来做到这一点吗?

Just for fun, here's how I would do it. 只是为了好玩,这就是我的做法。 Don't turn this in unless are ready to explain to your teacher how it works. 除非准备好向您的老师解释它是如何工作的,否则不要上交它。

struct MinMax {
    std::string min;
    std::string max;
    MinMax& operator+(const std::string& kid) {
        if( min.empty() || kid < min) min = kid;
        if( max.empty() || kid > max) max = kid;
        return *this;
    }
};

int main() {
    int nKids;
    std::cout << "How many students? " << std::flush;
    std::cin >> nKids;

    std::cout << "Enter students' names, followed by EOF\n";
    MinMax mm(std::accumulate(
        std::istream_iterator<std::string>(std::cin),
        std::istream_iterator<std::string>(),
        MinMax()));
    std::cout << mm.min << ", " << mm.max << "\n";
}

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

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