我有一组数字{1,2,3,4,5}或一组字符或其他什么.我想写一个模板方法来打印出完整的数组.它有效,只有一些问题.也许我先发布代码:
template <typename A>
void printArray(A start) {
int i = 0;
while (start[i] != 0) {
std::cout << start[i] << std::endl;
i++;
}
}
int main(int argc, char **argv) {
using namespace std;
int xs[] = {1,2,3,4,5,6,7}; //works
//int xs[] = {1,0,3,6,7}; of course its not working (because of the 0)
int *start = xs;
printArray(start);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你能看到问题吗?while(start[i] != 0)不是读取数组结束的最佳方式;)我有什么其他选项?
谢谢!