Reputation:
In this post
https://codereview.stackexchange.com/questions/5745/dynamic-array-improvements-0
What does this mean? Sorry if the question is vague..I just need to update my print_array function. Full code is below..for my poor man's dynamic array.
Can someone tell me how the overloaed << function works?
// My Current Print Array
void print_array()
{
for (int i = 0; i < size; i++) cout << array[i] << endl;
}
If you are going to write print_array at least write it so that it can use alternative stream (not just std::cout). Then write the output operator.
// SO user advice
std::ostream& operator<<(std::ostream& stream, dynamic_array const& data)
{
data.print_array(stream); // After you fix print_array
return stream;
}
// My Dynamic Array Class
#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();
}
~ ~
Upvotes: 0
Views: 5613
Reputation: 1356
If you want your function to be able to print to ostream
s other than cout
, you would do it like this
//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;
}
The operator<<()
function can be explained as follows: it returns a reference to an ostream
object, which is the class that cout
is a part of. Returning a reference allows for chaining. Now, since operator<<()
is not a member function, the first argument is the left side of the operator, in many cases, it would be cout
. The second argument is the right side of the operator. And I don't think that is valid C++ syntax, it should be const dynamic_array &data
.
Upvotes: 0
Reputation: 4346
From your example, whenever operator << is matched between std::ostream& stream
and dynamic_array const& data
compiler will invoke:
std::ostream& operator<<(std::ostream& stream, dynamic_array const& data)
{
data.print_array(stream); // After you fix print_array
return stream;
}
which behaves like a global operator. In other words, calling:
dynamic_array<int> d(10);
cout << d;
// above is logically equivalent to:
// operator<<(std::cout, d)
Note that the operator<< function returns std::ostream&
. That is because we want to be able to chain operator calls:
dynamic_array<int> d(10);
cout << "Data:" << d;
// above is logically equivalent to:
// operator<<( operator<<(std::cout, "Data:"), d);
Since you are using templates to output your array, the stream you are outputing to must know how to interpret the template type. In the example here we are using integers, and there is a predefined operator for that:
std::ostream& operator<<(std::ostream& stream, int const& i);
The only think left to change is like Joshua suggested to modify your print_array function to use ostream& rather than predefined cout.
Upvotes: 1