如何从函数返回的数组创建新数组?

时间:2016-12-20 17:23:44

标签: c++ arrays function

我有一种方法可以改组数组,但它不能正常工作,我现在也不知道如何修复它。如何在shuffleArray返回的数组中在main中创建一个新数组,因为我不能将混洗数组分配给一个新数组,它给了我相同的元素索引? / p>

using namespace std;

template <class T> T min (T array, T size){
    int min=array[0];
    for(int i=1;i<size;i++){
        if(array[i]<min){min=array[i];}
    }
return min;
}

template <class T> T indexOf (T array[], const int size, T value){

    for(int i=0;i<size;i++){
        if(array[i]==value){return value;}
    }
    return -1;
    }

template <class T> T shuffleArray (T array[], T size){

    T* Array2 = new T[size];

    for(int i=0;i<size;i++){
       Array2[i]=array[i];
    }

    random_shuffle(&Array2[0],&Array2[size]);

    return *Array2;
}

int main(){

    int a[]= {1,2,3,4,5};
    int index = indexOf(a, 5, 3);

    cout << endl << "The index is:" << shuffleArray(a, 5)<<endl;
    cout << endl << "The index is:" << index<<endl;
    return 0;


}

1 个答案:

答案 0 :(得分:0)

简答:将std容器用作std::array,它们具有适当的复制构造函数,可以省去一些麻烦。顺便说一下,std::array作为一个原始数组也不会慢(它基本上是原始数组,但是包含在一个类中并给出了一些很好的成员函数来处理它)。

详细解答:我不完全确定您要打印到std::cout的内容。但最有可能的是,在改组之前和之后它应该是3的位置。然后代码应如下所示(使用std=c++11进行编译以使用constexprauto):

#include <iostream>
#include <algorithm> // std::min, std::find and std::random_shuffle
#include <array> // use std::array instead of raw pointers

// using namespace std; Bad idea, as tadman points out in comment

// use std::min instead of
// template <class T> T min (T array, T size)

// use std::find instead of
// template <class T> T indexOf (T array[], const int size, T value){

// T is not the value_type of the array, but the array iteself. Works also for all other std containers with random access iterators
template<class T> T shuffleArray(T array) // array is copied here
{
    // shuffle
    std::random_shuffle(array.begin(), array.end());

    // return copy of array
    return array;
}

int main()
{
    constexpr int numberToFind = 3; // constexpr is not necessary, but the variable seems not intented to change in this code
    // use standard container instead of raw array
    std::array<int,5> a = {1,2,3,4,5};

    // iterator to numberToFind in a
    auto it = std::find(a.begin(), a.end(), numberToFind); // auto deduces the type, so you do not have to write std::array<int,t>::iterator and by the way the code is more flexible for changes

    // shuffle a and store result in aShuffled
    auto aShuffled = shuffleArray(a);

    // iterator to numberToFind in aShuffled
    auto itShuffled = std::find(aShuffled.begin(), aShuffled.end(), numberToFind);

    // pointer arithmetics give the index
    std::cout << "The index before shuffling is:" << it - a.begin() << std::endl;
    std::cout << "The index after  shuffling is:" << itShuffled - aShuffled.begin() << std::endl;

    return 0;
}

正如一些评论已经告诉你的,未来的一些提示:

  • 使用std容器而不是原始数组的原始指针
  • 使用来自标准库的经过良好测试的算法,而不是自己编写,除非您有非常具体的需求
  • auto在C ++ 11中非常容易处理迭代器类型。顺便说一句,你必须改变几乎没有使用std :: vector而不是std :: array。第一个宣言就足够了。
  • 当您使用new时,必须始终有delete。否则,您会创建内存泄漏。通过使用标准容器,通常可以避免这种情况。
  • 使用描述性名称而不是文字。这使您的代码更清晰,更易读。
相关问题