将字符串数组传递给函数而不使用vector
C++: Passing string arrays to function WITHOUT USING VECTORS
对于我的课堂作业,我必须做这个分数书程序。我正在努力弄清楚如何将字符串数组从一个函数传递到另一个函数,以便后一个函数可以对存储在字符串数组中的数据执行计算。那么,进一步缩小到更大的画面,字符串数组(用于学生姓名)与双数组(用于分数)并行,接收数组的函数必须找到最高分和最低分,计算平均值,并将输出输出到屏幕和文件。我得到了所有的最后一点,但我不能找出正确的语法引用数组到一个函数没有使用向量!
重要:如果你错过了,我们不允许在这个赋值中使用向量。
所以大致的轮廓是:
//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>>
或类似的东西…
相关文章:
- 编译器如何区分std::vector的构造函数
- 为什么std::vector和std::valarray初始化构造函数不同
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- std::vector 没有重载函数的实例与参数列表匹配
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 创建一个没有复制构造函数的类的 std::vector 的 std::vector
- 为 std::vector 编写自定义插入函数
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 将 vector<vector<int>> 传递到函数中会产生内存错误
- 是否可以将具有不同签名的 lambda 存储在 std::vector 中并在函数中执行它们(使用各自的参数)?
- 将 cv::Mat 转换为 std::vector 的通用函数
- 从 std::vector 迭代器中执行函数指针
- C++ std::vector 中的虚拟析构函数继承
- 错误:调用"es_queue::set_rpc_vector(std::vector >&, std::__cxx11::string)"没有匹配函数
- 调用2类继承的vector函数
- 在c++中,vector函数push_back会增加空数组的大小吗?
- 如何通过List
从Java到c++的std::vector函数参数使用JNA - 修改std::vector函数(继承?)
- 不能重载现有的std::vector函数