如何从函数返回的数组中创建一个新数组

How can I create a new array from the array returned by a function?

本文关键字:数组 一个 新数组 函数 返回 创建      更新时间:2023-10-16

我有一种调整阵列的方法,但是它不起作用,我现在不知道该如何修复它。我如何从shuffleArray返回的数组中创建一个新数组,因为我无法将洗牌数组分配给一个新数组,并且它为元素提供了相同的索引?

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;

}

简短答案:将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 ::数组。第一个声明就足够了。
  • 始终使用new时必须有一个delete。否则,您会创建一个内存泄漏。同样,通常可以使用标准容器来避免这种情况。
  • 使用描述性名称而不是文字。这使您的代码更清晰,可读。
相关文章: