Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 动态数组打印函数_C++ - Fatal编程技术网

C++ 动态数组打印函数

C++ 动态数组打印函数,c++,C++,在这个岗位上 这是什么意思?抱歉,如果问题不明确,我只需要更新我的print_数组函数。下面是我可怜的人的动态数组的完整代码 有没有人能告诉我,如果你想让你的函数能够打印到ostreams而不是cout,你会这样做 //i added a default argument of cout, so you don't have to specify void print_array(std::ostream &os = cout) { for (int i = 0; i <

在这个岗位上

这是什么意思?抱歉,如果问题不明确,我只需要更新我的print_数组函数。下面是我可怜的人的动态数组的完整代码


有没有人能告诉我,如果你想让你的函数能够打印到
ostream
s而不是
cout
,你会这样做

//i added a default argument of cout, so you don't have to specify
void print_array(std::ostream &os = cout) 
{ 
  for (int i = 0; i < size; i++) 
     os << array[i] << endl; 
} 
//我添加了一个默认参数cout,因此您不必指定
无效打印数组(std::ostream&os=cout)
{ 
对于(int i=0;ios从你的例子来看,每当运算符不清楚你的问题是什么时。你是在问关于
运算符的问题还是我为什么要这样做?有人告诉我在code reveiw上要这样做?为什么你的函数看起来与他们的不同…你能详细说明一下吗?我把这些函数放在哪里?
print_array()
在类声明中的位置很好。重载运算符将在类声明之外声明。修改print_数组以接受std::ostream&stream作为参数并输出到它,而不是std::cout yes。std::cout是特定std::ostream的预定义全局实例,类似地,std::cin也是预定义的ned特定std::istream的全局实例。只需将std::cout传递到打印数组就足以将数据输出到屏幕/控制台,但您也可以使用相同的函数将其输出到文件,如有必要,可通过网络等。任何std::ostream。
std::ostream& operator<<(std::ostream& stream, dynamic_array const& data)
{
    data.print_array(stream); // After you fix print_array
    return stream;
}    
#include "c_arclib.cpp"
template <class T> class dynamic_array
{
private:
    T* array;
    T* scratch;
public:
    int size;

    dynamic_array(int sizein)
    {  
        size=sizein;
        array = new T[size]();
    }

    void print_array()
    {  
        for (int i = 0; i < size; i++) cout << array[i] << endl;
    }

    void merge_recurse(int left, int right)
    {  
        if(right == left + 1)
        {  
            return;
        }
        else
        {  
            int i = 0;
            int length = right - left;
            int midpoint_distance = length/2;
            int l = left, r = left + midpoint_distance;
            merge_recurse(left, left + midpoint_distance);
            merge_recurse(left + midpoint_distance, right);
            for(i = 0; i < length; i++)
            {  
                if((l < (left + midpoint_distance)) && (r == right || array[l] > array[r]))
                {  
                    scratch[i] = array[l];
                    l++;
                }
                else
                {  
                    scratch[i] = array[r];
                    r++;
                }
            }
            for(i = left; i < right; i++)
            {  
                array[i] = scratch[i - left];
            }
        }
    }

    int merge_sort()
    {  
        scratch = new T[size]();
        if(scratch != NULL)
        {  
            merge_recurse(0, size);
            return 1;
        }
        else
        {  
            return 0;
            }
    }

    void quick_recurse(int left, int right)
    {  
        int l = left, r = right, tmp;
        int pivot = array[(left + right) / 2];
        while (l <= r)
        {  
            while (array[l] < pivot)l++;
            while (array[r] > pivot)r--;
            if (l <= r)
            {  
                tmp = array[l];
                array[l] = array[r];
                array[r] = tmp;
                l++;
                r--;
            }
        }
        if (left < r)quick_recurse(left, r);
        if (l < right)quick_recurse(l, right);
    }

    void quick_sort()
    {  
        quick_recurse(0,size);
    }

    void rand_to_array()
    {  
        srand(time(NULL));
        int* k;
        for (k = array; k != array + size; ++k)
        {  
            *k=rand();
        }
    }
};

int main()
{  
    dynamic_array<int> d1(10);
    cout << d1.size;
    d1.print_array();
    d1.rand_to_array();
    d1.print_array();
    d1.merge_sort();
    d1.print_array();
}
//i added a default argument of cout, so you don't have to specify
void print_array(std::ostream &os = cout) 
{ 
  for (int i = 0; i < size; i++) 
     os << array[i] << endl; 
} 
std::ostream& operator<<(std::ostream& stream, dynamic_array const& data) 
{ 
     data.print_array(stream); // After you fix print_array 
     return stream; 
}
dynamic_array<int> d(10);
cout << d;
// above is logically equivalent to:
// operator<<(std::cout, d)
dynamic_array<int> d(10);
cout << "Data:" << d;
// above is logically equivalent to:
// operator<<( operator<<(std::cout, "Data:"), d);
std::ostream& operator<<(std::ostream& stream, int const& i);