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

  • SEARCH
  • Home
  • 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 6546373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:41:58+00:00 2026-05-25T11:41:58+00:00

//Prints out a given array template <typename T> void print(T t) { for(int i

  • 0
//Prints out a given array 
template <typename T>
void print(T t)
{
    for(int i = 0; i < t.size(); i++)
    {
        cout << t[i] << " ";
    }
    cout << endl;
}

I have an idea but it includes passing the size of the array. Is it possible to avoid this?

*Update
Thanks for all of the answers/ideas but this problem is getting way deeper than my snorkeler can handle. I wanted to rewrite my C++ code in C because it was horribly written and slow. I see now that I have an opportunity to make it even worse in C. I’ll rewrite it from the ground up in Python(performance be damned). Thanks again

  • 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-25T11:41:59+00:00Added an answer on May 25, 2026 at 11:41 am

    If you don’t have ELEMENTS, it’s

    #define ELEMENTS(a) (sizeof(a)/sizeof(*a))
    

    Then,

    #define print_array(a, specifier) print_array_impl(a, specifier, ELEMENTS(a), sizeof(*a))
    
    void print_array_impl(void* a, char* specifier, size_t asize, size_t elsize)
    {
        for(int i = 0; i < asize; i++)
        {
            // corrected based on comment -- unfortunately, not as general
            if (strcmp(specifier, "%d") == 0)
               printf(specifier, ((int*)a)[i]); 
            // else if ... // check other specifiers
            printf(" ");
        }
        printf("\n");
    }
    

    Use like this

    print_array(a, "%d") // if a is a int[]
    

    and, a needs to be an array name, not a pointer (or else ELEMENTS won’t work)

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

Sidebar

Related Questions

i have an array $array that prints out something like: Array ( [Terry] =>
Given an integer array of size N by the user. Print all the possible
The code below prints out all comments for a given submissionid in chronological order.
Given the following code (that doesn't work): while True: # Snip: print out current
The following code prints out 10 . How can I make it print out
I have some code that prints out databse values into a repeater control on
I'm wondering why this code section prints out the following: print request.user.has_perm('bug_tracking.is_developer'): + str(request.user.has_perm('bug_tracking.is_developer'))
Given array a= [1,4,5,9,2].I need to find/print combinations of two values where sum =
so currently I have a PHP function that prints out the applications configuration: function
Here's what I am trying to do: Given a list of names, print out

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.