我从破解编码面试书中得到了这个问题。我能够用python和java编写这个方法。但是当我试图用C++编写它时,编译器开始对我大喊大叫。我认为问题在于,在主函数中,我有一个由模板实例化的数组,但函数采用的是原始数组。我应该如何实例化一个基本数组?原始数组与C++中的数组模板
// Given a sorted array of positive integers with an empty spot (zero) at the
// end, insert an element in sorted order.
bool sortSortedArray(size_t arrInt[], size_t x)
{
size_t indexArr{0};
size_t insertNum{x};
while (x != 0) {
if (x < arrInt[indexArr]) {
size_t swapVal = arrInt[indexArr];
arrInt[indexArr];
insertNum = swapVal;
++indexArr;
}
}
return true;
}
// Test the sortSortedArray function.
int main()
{
array<size_t, 5> testArr{1, 4, 5, 8, 0};
if (sortSortedArray(testArr, 3)) {
return 0;
}
}
'的std :: array'和内置阵列是完全独立的类型。 – chris 2014-12-03 01:30:33
一般而言,它要求的问题是用一种类型的参数定义一个函数,并用完全不同类型的参数来调用它。 – 2014-12-03 01:33:29