1
#include<iostream> 
using namespace std;
int main() {
  int *p=new int [10];

}

After creating the array, I am confused and want to know if it's possible to do the following:

  1. read an array of integers without builtin arrays such as vector.
  2. print out this array.
  3. Is there any built-in command that we can use in cpp to get the actual size of the array, which means we don't have to use sizeof(*p)/size(p[0]).

And what do you think will be the easiest way of doing this task, on the condition that we still use the pointer of an array.

Thanks for helping me out!

11
  • 2
    int *p=new int; this will get you only one dynamically allocated integer variable, if you want an array you would use int *p=new int[10]; for an array of 10 integers Commented Jul 16, 2013 at 10:10
  • Sorry, typed too fast, didn't notice that. already edited now Commented Jul 16, 2013 at 10:13
  • 2
    You can avoid loops by typing everything 10 times. You cannot get the length of a dynamically allocated array, but you have it already: it is 10. Commented Jul 16, 2013 at 10:15
  • You would have to print the array manually one item by one... accessing every value, like cout << "index 5:" << p[5] << endl; I think that the loop is a way to go. Why do you need to avoid the loop? Commented Jul 16, 2013 at 10:18
  • 2
    You should really use std::vector, it's much more convinient. Then you can use algorithms like std::transform to apply operations on each element. Simple arrays are cheese and dated ;-). I'm not sure if you can use std::transform on arrays, if you have a very good reason to use them, check out the documentation. Commented Jul 16, 2013 at 10:30

4 Answers 4

2

I think you could use std::for_each. Together with c++ lambda functions it is a very comprehensive tool:

for_each(p, p+10, []->(int element){ cout << element; })

sry, I didn't compile the code above, so it might contain errors. But the pattern is clear, I hope.

If your compiler does not support lambdas, you can use functors or function pointers (just google for it). I really like this approach and I don't like loops either.

But, of course as mentioned above, internally the for_each loops.

Sign up to request clarification or add additional context in comments.

Comments

1

This does not create an array, but it gives you a pointer to a single integer.

int *p=new int;

change this for example to:

const unsigned size = 10;
int * p = new int[size]; /*dont forget to call delete[] on p when you are ready.*/

Now p points to 10 ints, you know the size since you've put size to 10.

Sure you can print the values of p without a loop

/*Assuming you initialize the values.*/
cout << p[0];
cout << p[1];
/*etc*/
cout << p[9];

That said I think the easiest way to solve your problem is to use vector<int> ( but this doesn't satisfy you first point ). You'll always get the proper size of a vector with vector<TYPE>::size() and a intance of vector makes it easy to iterate ( which is also looping ) over the items to print them.

2 Comments

That's actually my problem, I couldn't read the input properly. For example,I want to read"1 2 3 4..." until I push enter but neither cin nor cin.getline works, so I really want to know a proper way to input like this.
have you tried typing ctrl+d in the terminal? Actually it will really help if you specify what exactly is your input and how you would like to see your output.
1

You would have to print the array manually one item by one... accessing every value, like:

cout << "index 5:" << p[5] << endl;

I think that the loop is a way to go. I would avoid a loop only if the array had few items, and fixed length like 3 to 5 items or so and desperately needed to optimize the code for speed, but not with console output which will be slow anyway. But the compilers usualy support loop unrolling optimalisations.

@nio, but dude, I think I still have trouble of doing this. for example, is there any way that I can input in this simple way "1 2 3 4" with space and then it reads the array? or I have to do it with enter? (Now, I don't care if we use loop or not any more :P) – Cancan

This code will read 4 items separated by combination of spaces and newlines:

int *p=new int[10];  
int i;

for(i=0;i<4;i++)
{
    cin >> p[i];
}

for(i=0;i<4;i++)
{
    cout  << "index " << i << " :" << p[i] << endl;
}

3 Comments

He asked not to use loops.
there are more comments under his question
@nio,sorry, I wasn't here. I think I expressed what I wanted to say in a wrong way. So, actually, what I want to do is that. 1.dynamically create an array 2. input however many numbers I want(if it doesn't exceed the maximum size given)in the format "1 2 3"with space 3.once I press "enter"(on keyboard :P) it will print what I exactly entered. 4.I should say c rather than c++, but since c++ has new, that's why I say it in cpp. Anyway, what I want to do here is just to try out the feeling of using pointers with array. Hope you can help me out.
0

1. read an array of integers without using loops nor some builtin arrays such as vector.
Recursion. Everything that can be done using loops can be done recursively.

2. print out this array without using loop.
Recursion too.

3. Is there any built-in command that we can use in cpp to get the actual size of the array, which means we don't have to use sizeof(*p)/size(p[0]).

No, when you're using arrays in c++ you need to remember the length of an arrays by yourself. sizeof(*p)/size(p[0]) won't help you too

2 Comments

Don't use recursion in C++. It has only educational value. It is not reliable, especially if you don't know the size of the array beforehand. Recursion can cause stack overflows, since the recursion method arguments are put onto the stack.
@anhoppe, interesting point of view, I like recursive approaches to things like tree traversal and didn't met stack overflow yet. How real is the problem? What size of stack is typical and do it lead to security issues like code injection?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.