将字符串数组传递给函数而不使用vector

C++: Passing string arrays to function WITHOUT USING VECTORS

本文关键字:vector 函数 字符串 数组      更新时间:2023-10-16

对于我的课堂作业,我必须做这个分数书程序。我正在努力弄清楚如何将字符串数组从一个函数传递到另一个函数,以便后一个函数可以对存储在字符串数组中的数据执行计算。那么,进一步缩小到更大的画面,字符串数组(用于学生姓名)与双数组(用于分数)并行,接收数组的函数必须找到最高分和最低分,计算平均值,并将输出输出到屏幕和文件。我得到了所有的最后一点,但我不能找出正确的语法引用数组到一个函数没有使用向量!

重要:如果你错过了,我们不允许在这个赋值中使用向量。

所以大致的轮廓是:

//blahblahblah, #includes and other starting things
int myFunc(//prototype-what the heck goes here?)    
int main()
{
    //arrays declared
    string names[MAX_NUM];
    double scores[MAX_NUM];
    //...other stuff main does, including calling myFunc...
}
int myFunc( //header-what the heck goes here?)
{
    //Code here to find highest, lowest, and mean scores from data in scores[]
}

很明显,每个指示"这里到底是什么?"的位置将与另一个位置的内容相关。但我不知道怎么做我能找到的答案都是用向量。我们还没有讲到,因此不能用。帮助,好吗?

template<std::size_t size>
int myFunc(std::string (&names)[size]);
int myFunc(std::string *names, std::size_t numberOfNames);
int myFunc(std::string *names); //implicitly assume names points to MAX_NUM strings
//blahblahblah, #includes and other starting things
int myFunc(string names[], double scores[], int elementCount);
int main()
{
   //arrays declared
   string names[MAX_NUM];
   double scores[MAX_NUM];
   //...other stuff main does, including calling myFunc...
   myFunc(names, scores, elementCount);
}
int myFunc(string names[], double scores[], int elementCount)
{
   //Code here to find highest, lowest, and mean scores from data in scores[]
}

我可能会使用

myFunc(std::string* names, double* scores, std::size_t n_students) { /*...*/ }

或者,如果过于精确地使用无向量策略,您可以使用std::list<std::pair<std::string, double>>或类似的东西…