Click here to Skip to main content
15,442,007 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Hi,

I want to display an array without using loop(for, while ...) something like it:

C++
int arr[10];
std::cout<<arr;


But it gives a number.

right now im using the code bellow:

C++
for(int i=0;i<10;i++)
std::cout<<arr[i]


is there any way to do it like the first one?

Thank you,
Posted
Comments
joshrduncan2012 24-May-13 14:21pm    
Why do you want to avoid a loop of some kind?
Sergey Alexandrovich Kryukov 24-May-13 14:52pm     CRLF
In some our inquirers, the desire for abuse my any means is even stronger than the desire to get some results... :-) —SA
joshrduncan2012 24-May-13 14:54pm    
Good point. I'm starting to realize that the longer I'm a member of CP.
JackDingler 24-May-13 15:15pm     CRLF
Somewhere, some code has to do some looping to do what you want. You can move the code around, but it will still be there. Depending on your compiler and environment, you could do a for_each or BOOST_FOR_EACH. This is another way to loop...
m.r.m.40 24-May-13 15:32pm    
thanks for your attention, i'll use a loop.

For example the following code example demonstrates how to print array without loop:
C++
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
  short arr[] = { 10, 20, 30, 40 };

  copy(arr,arr + sizeof(arr) / sizeof(arr[0]),ostream_iterator<short>(cout,"\n"));
}
 
Share this answer
 
v3
Comments
Y.Desros 25-May-13 14:41pm    
Nice Alex!5!
Volynsky Alex 25-May-13 14:46pm    
Thanks Y.Desro
hi dear friend
with below code you can display your string without any loop:

<pre lang="c++">
char a[5]="hello";
printf("%s",a);
 
Share this answer
 
Comments
Richard MacCutchan 25-May-13 4:28am    
Won't work for an integer array.
Manfred Rudolf Bihy 26-May-13 8:10am     CRLF
I wonder how this could even work with an Array of characters. I always thought that strings had to be terminated with a 0, but then I know almost nothing (very very Little) about C++. :) It would surely depend on how the Memory beyond the Array is filled, wouldn't it?
Richard MacCutchan 26-May-13 8:19am    
You are correct of course. I think you know a lot more than you admit. ;)
[no name] 25-May-13 5:40am     CRLF
if each index of array has one digit test it may be it will help you: int a[10];char b[10]; b=a; of course it might copy just first index. else i don't know more than it.excuse me
Richard MacCutchan 26-May-13 8:20am    
That will not work either; you cannot assign a simple array like that in C++.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900