#include<iostream>
#include<string>
using namespace std;
class Prerequisites
{
public:
void orderClasses(string* Input);
};
void Prerequisites::orderClasses(string* Input)
{
// Need to find the length of the array Input
}
int main()
{
Prerequisites A;
string classes[]={"CSE121: CSE110",
"CSE110:",
"MATH122:"
};
A.orderClasses(classes);
}
我需要在方法顺序Classes中找到数组classes[]的长度。 我无法更改方法 orderClasses 的签名!这是一个要求。
最佳答案
您应该将数组中元素的数量传递给 orderClasses()
.由于这不是一种选择,请考虑一些替代方案:
- 将另一个成员函数添加到
Prerequisites
当你调用orderClasses()
时通知它数组的大小. - 为数组中的最后一个字符串使用标记值,这样当您在函数中看到该值时,您就知道已经到达末尾。
- 将数组中的第一个字符串设为包含数组中元素数量的字符串。
这些都不是解决问题的好方法:当然,在仍然使用数组的情况下,最好的选择只是将数组大小传递给函数。在大多数情况下,根本不使用数组而只传递 std::vector<std::string>
会更好。包含字符串。
关于c++ - 如何查找字符串数组中的元素数量(字符串传递给函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3098209/