Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:01:58+00:00 2026-05-26T16:01:58+00:00

In this post https://codereview.stackexchange.com/questions/5745/dynamic-array-improvements-0 What does this mean? Sorry if the question is vague..I

  • 0

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();
}

~
~

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:01:58+00:00Added an answer on May 26, 2026 at 4:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This post started as a question on ServerFault ( https://serverfault.com/questions/131156/user-receiving-partial-downloads ) but I determined
I was reading this thread/post: https://stackoverflow.com/questions/262298/windows-c-ui-technology and am also wondering about a non .NET
Ok, I have an app as described in this post: https://stackoverflow.com/questions/1623105/good-database-structure-for-a-new-web-app I've prepared a
As a follow up to this post: https://stackoverflow.com/questions/521432/best-jquery-rounded-corners-script Assuming jQuery is already being included,
I've read this: https://stackoverflow.com/questions/631850/how-do-you-name-your-many-to-many-relationship-tables But my question is a little different. I'm just wondering
https://stackoverflow.com/questions/5282437/cucumber-pickle-factory-girl-given-a-returning-undefined-step - this post didn't help me a lot. Gemfile: gem 'rails' # 3.0.5
I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I got the problem similar to this post here: https://rails.lighthouseapp.com/projects/8994/tickets/106-authenticity_token-appears-in-urls-after-ajax-get-request routes.rb map.namespace(:admin, :active_scaffold =>
It's not quite clear from this post: https://devblogs.microsoft.com/pfxteam/concurrentdictionarys-support-for-adding-and-updating/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.