473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

returning array to function

cdg
Could anyone correct any mistakes in this example program. I am just
trying to return an array back to "main" to be printed out. And I am not
sure how a "pointer to an array" is returned to the function call.

#include <iostream>
using namespace std;

int* GetArray(); //function prototype

void main()
{
int* pArray[100];
int n(0);

pArray[n] = GetArray(); //need to return here****

for(n=0; n<100; n++) //then print here****
cout<<pArray[n]<<" ";
cout<<"\n\n";
}

int* GetArray()
{
int* pArray;
int n(0);

pArray = new int[100];

for(n=0; n<100; n++) //this just fills the array in order
pArray[n] = n;

return pArray;
}
Feb 28 '06 #1
26 2750
void main()
{
int* pArray[100];
int *pArray ;
int n(0);
pArray[n] = GetArray(); //need to return here****


pArray = GetArray();
Rest of the code is perfect..........

Feb 28 '06 #2
cdg wrote:
#include <iostream>
using namespace std;

int* GetArray(); //function prototype
To be explicit : GetArray is a function which takes nothing and returns
a pointer to an integer.
(Probably that is not what you want. )

Note that you cannot pass or return arrays to/from functions by value.
(Or, in other words, passing and returning arrays is same as passing
and returning the address of the first element)

void main()
int main()
{
int* pArray[100]; pArray is an array of 100 integer pointers.
int n(0);

pArray[n] = GetArray(); //need to return here****
You want to return an array from GetArray() - This is same as returning
a pointer to first element. The correct code would be :

// changing the type of pArray
int* pArray = GetArray();
for(n=0; n<100; n++) //then print here****
cout<<pArray[n]<<" ";
cout<<"\n\n";
}

int* GetArray()
{ [ code snipped ] }


Aside: Use std::vector<> instead of arrays. Lots of benefits.
http://www.parashift.com/c++-faq-lit....html#faq-34.1

Feb 28 '06 #3
cdg wrote:
Could anyone correct any mistakes in this example program. I am just
trying to return an array back to "main" to be printed out. And I am not
sure how a "pointer to an array" is returned to the function call.

#include <iostream>
using namespace std;

int* GetArray(); //function prototype

void main() `main' returns int {
int* pArray[100];
This declares an array of 100 pointers to int. I suspect that's not what
you want (but see below)...
int n(0);

pArray[n] = GetArray(); //need to return here****
All right, perhaps it *was* what you wanted (though I still have my
doubts).
for(n=0; n<100; n++) //then print here****
cout<<pArray[n]<<" "; This loop will print out the values of the pointers in the array
`pArray', of which pArray[0] is the value obtained by the call to `new'
below -- and the rest are garbage. cout<<"\n\n";
}

int* GetArray()
{
int* pArray;
int n(0);

pArray = new int[100];

for(n=0; n<100; n++) //this just fills the array in order
pArray[n] = n;

return pArray;
}

Now rewrite it -- but first look at the sig. Either you're trying too
hard, or you got compile errors you didn't understand and started making
changes you didn't understand either...

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Feb 28 '06 #4
Neelesh Bodas wrote:
cdg wrote:
#include <iostream>
using namespace std;

int* GetArray(); //function prototype

To be explicit : GetArray is a function which takes nothing and returns
a pointer to an integer.
(Probably that is not what you want. )

Note that you cannot pass or return arrays to/from functions by value.
(Or, in other words, passing and returning arrays is same as passing
and returning the address of the first element)

void main()

int main()

{
int* pArray[100];


pArray is an array of 100 integer pointers.

int n(0);

pArray[n] = GetArray(); //need to return here****

You want to return an array from GetArray() - This is same as returning
a pointer to first element. The correct code would be :

// changing the type of pArray
int* pArray = GetArray();

for(n=0; n<100; n++) //then print here****
cout<<pArray[n]<<" ";
cout<<"\n\n";
}

int* GetArray()
{


[ code snipped ]
}

Aside: Use std::vector<> instead of arrays. Lots of benefits.
http://www.parashift.com/c++-faq-lit....html#faq-34.1


The main benefit of using STL library vector is memory management.
Your code didn't free up the array with a delete which is not
good coding. That's the advantage of using the STL library, most
of the memory management is taken care of for you. But don't be
discouraged, if you learn from your mistakes, (which I'm sure you
will), may you enjoy C++ programming.

JB
Feb 28 '06 #5
cdg
Thanks for your post. It was exactly what I needed to know.

"santosh" <sa***********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
void main()
{
int* pArray[100];


int *pArray ;
int n(0);
pArray[n] = GetArray(); //need to return here****


pArray = GetArray();
Rest of the code is perfect..........

Feb 28 '06 #6
In article <11*********************@i39g2000cwa.googlegroups. com>,
"santosh" <sa***********@gmail.com> wrote:
void main()
{
int* pArray[100];


int *pArray ;
int n(0);
pArray[n] = GetArray(); //need to return here****


pArray = GetArray();
Rest of the code is perfect..........


That's just plain mean.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 28 '06 #7
cdg
>int* pArray[100];
pArray is an array of 100 integer pointers.
I didn't realize this would be an array of 100 integer pointers. I thought
it would just be a declaration for the array itself.
// changing the type of pArray
int* pArray = GetArray();
And I understand now that all that is needed is the address of the first
element.
To be explicit : GetArray is a function which takes >nothing and returns
a pointer to an integer.
(Probably that is not what you want. )


I think the only way to return the array pointer, is if it was passed to
the function or is dynamically allocated in the function so that it could be
returned from there. And there was no need to create it before this
function.

And I plan on using vectors eventually. But I thought I needed to
understand how to move arrays (pointers to arrays) around in a program
first.



Feb 28 '06 #8
In article <dZQMf.8585$Tf3.6776@dukeread09>,
"cdg" <an****@anywhere.com> wrote:
Could anyone correct any mistakes in this example program. I am just
trying to return an array back to "main" to be printed out. And I am not
sure how a "pointer to an array" is returned to the function call.


Other people have pointed out the mistakes in your example. I'm going to
answer your question directly...

To return an array of values from a function you have two basic choices:

int* get_array1( int size ) {
int* result = new int[size];
// fill result
return result;
}

If you use this, you must remember to delete the array using "delete []"
after you are done with it.

A better option (IMHO) is:

int* get_array2( int* parray, int size ) {
// fill parray
return parray;
}

Note how much this latter version looks like the various str* routines
(like strcpy, strcat, &c.)

As to use:

int main() {
int* array1 = get_array1( 100 );
// use array1 like "array1[3]"
delete [] array1;

int array2[100];
get_array2( array2, 100 );
// use array2 like "array2[3]"
// no need for the delete because nothing was newed.
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 28 '06 #9
Daniel T. wrote:
In article <11*********************@i39g2000cwa.googlegroups. com>,
"santosh" <sa***********@gmail.com> wrote:
void main()
{
int* pArray[100];

int *pArray ;
int n(0);
pArray[n] = GetArray(); //need to return here****

pArray = GetArray();
Rest of the code is perfect..........


That's just plain mean.


LOL

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 28 '06 #10
cdg
The main benefit of using STL library vector is memory management.
Your code didn't free up the array with a delete which is not
good coding. That's the advantage of using the STL library, most
of the memory management is taken care of for you. But don't be
discouraged, if you learn from your mistakes, (which I'm sure you
will), may you enjoy C++ programming.

JB


With just the corrected code (below) I`m not sure it would be necessary to
"delete" the array, or even possible to delete it at this point in the
program.

#include <iostream>
using namespace std;

int* GetArray(); //function prototype

void main()
{
int* pArray;
int n(0);

pArray = GetArray();

for(n=0; n<100; n++)
cout<<pArray[n]<<" ";
cout<<"\n\n";
}

int* GetArray()
{
int* pArray;
int n(0);

pArray = new int[100];

for(n=0; n<100; n++) //this just fills the array in order
pArray[n] = n;

return pArray;
}
Feb 28 '06 #11
cdg wrote:
int* pArray[100];
pArray is an array of 100 integer pointers.


I didn't realize this would be an array of 100 integer pointers. I thought
it would just be a declaration for the array itself.
// changing the type of pArray
int* pArray = GetArray();


And I understand now that all that is needed is the address of the first
element.
To be explicit : GetArray is a function which takes >nothing and returns
a pointer to an integer.
(Probably that is not what you want. )


I think the only way to return the array pointer, is if it was passed to
the function or is dynamically allocated in the function so that it could be
returned from there. And there was no need to create it before this
function.

And I plan on using vectors eventually. But I thought I needed to
understand how to move arrays (pointers to arrays) around in a program
first.


Not really. Arrays are evil.

Learn vectors first, when you get a good grasp of pointers, memory
management and exception safety, then look at arrays.

Personally I would create a vector (of the correct size), then call a
function (which accepts a range - 2 iterators) that populates the
vector, then pass the same vector into another function that also
accepts a range, that prints that range to cout.

#include <vector>
#include <iostream>

int main {
std::vector<int> vec(100);
fillVector(vec.begin(), vec.end());
printVector(vec.begin(), vec.end());
}

I'll leave the details to you.

See what you come up with (hint: std::vector<int>::iterator), and then
I'll show you some cool ways of improving it.

First we'll turn the functions into templates, so that they will work
with any built in type, and then we'll get rid of the second function
altogether, and replace it with built in functionality.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 28 '06 #12
cdg wrote:
The main benefit of using STL library vector is memory management.
Your code didn't free up the array with a delete which is not
good coding. That's the advantage of using the STL library, most
of the memory management is taken care of for you. But don't be
discouraged, if you learn from your mistakes, (which I'm sure you
will), may you enjoy C++ programming.

JB

With just the corrected code (below) I`m not sure it would be necessary to
"delete" the array, or even possible to delete it at this point in the
program.

#include <iostream>
using namespace std;

int* GetArray(); //function prototype

void main()
{
int* pArray;
int n(0);

pArray = GetArray();

for(n=0; n<100; n++)
cout<<pArray[n]<<" ";
cout<<"\n\n";

delete [] pArray; }

int* GetArray()
{
int* pArray;
int n(0);

pArray = new int[100];

for(n=0; n<100; n++) //this just fills the array in order
pArray[n] = n;

return pArray;
}

If the handling had not been done in main() the memory
allocated would have been lost, hence the delete [] pArray
before returning. Using STL or well designed classes to
handle memory management, you can avoid the need for exception
handling to clean up before the last pointer to the allocated
memory goes out of scope! When a well designed class Object goes
out of scope the destructor is called automatically freeing up
allocated memory.

JB
Feb 28 '06 #13
cdg
I haven`t been able to find enough information (quickly) to write most of
this. However, this is what I have written, and I was having a problem
knowing how to fill the vector in "fillVector" with values from 0 to 100.
And I am not sure exactly what parameters are used for functions using
vectors.

#include <iostream>
#include <vector>
using namespace std;

void fillVector(); //prototype

int main()
{
vector<int> v(100);
fillVector(v.begin(), v.end());
}

void fillVector(v.begin(), v.end());
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
v.insert

}

void printVector(v.begin(), v.end());
{

}

----------------------------------------

"Ben Pope" <be***************@gmail.com> wrote in message
news:44**********************@taz.nntpserver.com.. .
cdg wrote:
int* pArray[100];
pArray is an array of 100 integer pointers.


I didn't realize this would be an array of 100 integer pointers. I thought it would just be a declaration for the array itself.
// changing the type of pArray
int* pArray = GetArray();


And I understand now that all that is needed is the address of the first element.
To be explicit : GetArray is a function which takes >nothing and returns a pointer to an integer.
(Probably that is not what you want. )


I think the only way to return the array pointer, is if it was passed to the function or is dynamically allocated in the function so that it could be returned from there. And there was no need to create it before this
function.

And I plan on using vectors eventually. But I thought I needed to
understand how to move arrays (pointers to arrays) around in a program
first.


Not really. Arrays are evil.

Learn vectors first, when you get a good grasp of pointers, memory
management and exception safety, then look at arrays.

Personally I would create a vector (of the correct size), then call a
function (which accepts a range - 2 iterators) that populates the
vector, then pass the same vector into another function that also
accepts a range, that prints that range to cout.

#include <vector>
#include <iostream>

int main {
std::vector<int> vec(100);
fillVector(vec.begin(), vec.end());
printVector(vec.begin(), vec.end());
}

I'll leave the details to you.

See what you come up with (hint: std::vector<int>::iterator), and then
I'll show you some cool ways of improving it.

First we'll turn the functions into templates, so that they will work
with any built in type, and then we'll get rid of the second function
altogether, and replace it with built in functionality.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...

Feb 28 '06 #14
On Tue, 28 Feb 2006 16:56:56 -0600 "cdg" <an****@anywhere.com> waved a
wand and this message magically appeared:
I haven`t been able to find enough information (quickly) to write most of
this. However, this is what I have written, and I was having a problem
knowing how to fill the vector in "fillVector" with values from 0 to 100.
And I am not sure exactly what parameters are used for functions using
vectors.

void fillVector(v.begin(), v.end());
Won't work. Try fillVector(vector<int>& v) and write a loop using
v.size() to fill the array.
void printVector(v.begin(), v.end());


Ditto.

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 1 '06 #15

cdg wrote:
I haven`t been able to find enough information (quickly) to write most of
this. However, this is what I have written, and I was having a problem
knowing how to fill the vector in "fillVector" with values from 0 to 100.
And I am not sure exactly what parameters are used for functions using
vectors.

#include <iostream>
#include <vector>
using namespace std;

void fillVector(); //prototype

Since your usage of fillVector() takes two iterators as arguments (see
below) hence the correct prototype is not what you have written above.

int main()
{
vector<int> v(100);
fillVector(v.begin(), v.end());


Since you want to fill the data in the vector, it is advisible to pass
the vector as a non-const reference to fillVector() instead of using
iterators.
[code chopped off]
So the working code goes something like this

#include <iostream>
#include <vector>

void fillVector(std::vector<int>& v) // pass by reference
{
for(int i=0; i < 100; i++)
v.push_back(i);
}
int main()
{
std::vector<int> v;
fillVector(v);
for(int i = 0; i < 100; i++)
std::cout << v[i] << std::endl;
// you can also use iterators (or still better, std::copy() algorithm)
but this is easy to understand when you are transitioning from arrays
to vectors
}

You can look up various sources to find more about
stl.http://www.sgi.com/tech/stl/ is one such place to start with.

Aside : [Top Posting]
Please do not top-post in this group.

Mar 1 '06 #16
cdg wrote:
"Ben Pope" <be***************@gmail.com> wrote in message
news:44**********************@taz.nntpserver.com.. .

#include <vector>
#include <iostream>

int main {
std::vector<int> vec(100);
fillVector(vec.begin(), vec.end());
printVector(vec.begin(), vec.end());
}

I'll leave the details to you.

See what you come up with (hint: std::vector<int>::iterator), and then
I'll show you some cool ways of improving it.

First we'll turn the functions into templates, so that they will work
with any built in type, and then we'll get rid of the second function
altogether, and replace it with built in functionality.
I haven`t been able to find enough information (quickly) to write most of
this. However, this is what I have written, and I was having a problem
knowing how to fill the vector in "fillVector" with values from 0 to 100.
And I am not sure exactly what parameters are used for functions using
vectors.

#include <iostream>
#include <vector>
using namespace std;

void fillVector(); //prototype


//check my hint!
void fillVector(vector<int>::iterator first,
vector<int>::iterator last);
int main()
{
vector<int> v(100);
fillVector(v.begin(), v.end());
}
void fillVector(v.begin(), v.end());
//drop the semicolon!
void fillVector(vector<int>::iterator first,
vector<int>::iterator last)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
v.insert


// OK, you can replace the for loop with a while loop now.
// you won't need v.insert

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 1 '06 #17
cdg
Is there any reason you are not passing the function by reference, and I
am not sure how to write the while loop
for filling the values to vector.
Also, is it necessary to rewrite the function headers with
"vector<int>::iterator". Could you just separate first and last with a
comma - (vector<int>::iterator first, int last).
The vector name was changed to vctr.

#include <iostream>
#include <vector>
using namespace std;

void fillVector(vector<int>::iterator first,
vector<int>::iterator last);
int main()
{
vector<int> vctr(100);
void fillVector(vector<int>::iterator first,
vector<int>::iterator last);
void printVector(vector<int>::iterator first,
vector<int>::iterator last);
}

void fillVector(vector<int>::iterator first,
vector<int>::iterator last);
{
for (i=vctr.begin(); i != vctr.end; i++)
vctr(*i) = i;
}

void printVector(vector<int>::iterator first,
vector<int>::iterator last);
{
for (i=vctr.begin(); i != vctr.end; i++)
cout<<vctr(*i)<<endl;
}
Mar 1 '06 #18
cdg wrote:
Is there any reason you are not passing the function by reference, and I
am not sure how to write the while loop
for filling the values to vector.
Also, is it necessary to rewrite the function headers with
"vector<int>::iterator". Could you just separate first and last with a
comma - (vector<int>::iterator first, int last).
The vector name was changed to vctr.

#include <iostream>
#include <vector>
using namespace std;

void fillVector(vector<int>::iterator first,
vector<int>::iterator last);
int main()
{
vector<int> vctr(100);
void fillVector(vector<int>::iterator first,
vector<int>::iterator last);
void printVector(vector<int>::iterator first,
vector<int>::iterator last);
}

void fillVector(vector<int>::iterator first,
vector<int>::iterator last);
{
for (i=vctr.begin(); i != vctr.end; i++)
vctr(*i) = i;
}

void printVector(vector<int>::iterator first,
vector<int>::iterator last);
{
for (i=vctr.begin(); i != vctr.end; i++)
cout<<vctr(*i)<<endl;
}


I've already given you code that you have now changed. You also need to
read up on how to use std::vector<> and iterators:

#include <vector>
#include <iostream>

// fillVector takes a range
void fillVector(vector<int>::iterator first,
vector<int>::iterator last);

// printVector takes a range
void printVector(vector<int>::iterator first,
vector<int>::iterator last);
int main {
// create a vector of 100 ints
std::vector<int> vec(100);

// fill the entire vector (from beginn to end)
fillVector(vec.begin(), vec.end());
// print the entire vector (from beginn to end)
printVector(vec.begin(), vec.end());
}

// Don't edit anything above this line

void fillVector(vector<int>::iterator first,
vector<int>::iterator last) {
int i = 0;
while(first != last) {
// something
++first;
++i;
}
}

void fillVector(vector<int>::iterator first,
vector<int>::iterator last) {
// stuff
}
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 1 '06 #19
cdg
I am just copying some of what I am finding on the internet, and book or
two. So, I am not completely understanding how vectors work exactly. I know
they are pointers in a sense, and I am just recently getting to understand
pointers. But I thought if you could finish the program in the way you had
explained I thought it would be a good thing to learn from.
And I don't understand the iterators (parameters) you are using, or how
they are being passed. And I am not sure how vectors are passed to
functions. So, I have not figured out a to print the vector to see if it is
being filled.
Here is the program as you have written it so far:

#include <iostream>
#include <vector>
using namespace std;

// fillVector takes a range
void fillVector(vector<int>::iterator first,
vector<int>::iterator last);

// printVector takes a range
//void printVector(vector<int>::iterator first, //off temp
// vector<int>::iterator last); //off temp

void main()
{
// create a vector of 100 ints
vector<int> vec(100);

// fill the entire vector (from begining to end)
fillVector(vec.begin(), vec.end());

// print the entire vector (from begining to end)
// printVector(vec.begin(), vec.end()); //off temp
}

void fillVector(vector<int>::iterator first,
vector<int>::iterator last)
{
int i = 0;
while(first != last)
{
++first;
++i;
}
cout<<vec(5)<<"\n\n"; //***not sure how to test print this***
}

//void printVector(vector<int>::iterator first, //off temp
// vector<int>::iterator last) //off temp
//{

//}

Mar 2 '06 #20
hde
No need to use a pointer for this program at all, you can just use a
reference. This way all the nasty parts of pointers is done for you,
or you can do what everone seems to like a vector :). Long live the
STL!

Mar 2 '06 #21
hde

cdg wrote:
I am just copying some of what I am finding on the internet, and book or
two. So, I am not completely understanding how vectors work exactly. I know
they are pointers in a sense, and I am just recently getting to understand
pointers. But I thought if you could finish the program in the way you had
explained I thought it would be a good thing to learn from.
And I don't understand the iterators (parameters) you are using, or how
they are being passed. And I am not sure how vectors are passed to
functions. So, I have not figured out a to print the vector to see if it is
being filled.
Here is the program as you have written it so far:

#include <iostream>
#include <vector>
using namespace std;

// fillVector takes a range
void fillVector(vector<int>::iterator first,
vector<int>::iterator last);

// printVector takes a range
//void printVector(vector<int>::iterator first, //off temp
// vector<int>::iterator last); //off temp

void main()
{
// create a vector of 100 ints
vector<int> vec(100);

// fill the entire vector (from begining to end)
fillVector(vec.begin(), vec.end());

// print the entire vector (from begining to end)
// printVector(vec.begin(), vec.end()); //off temp
}

void fillVector(vector<int>::iterator first,
vector<int>::iterator last)
{
int i = 0;
while(first != last)
{
++first;
++i;
}
cout<<vec(5)<<"\n\n"; //***not sure how to test print this***
}

Why are you wanting to print something here? Well you are not going to
be able to access vec from this function you are in a different
namespace. :) You might be wanting cout << *first << "\n\n"; ?

Mar 2 '06 #22
hde

hde wrote:
cdg wrote:
I am just copying some of what I am finding on the internet, and book or
two. So, I am not completely understanding how vectors work exactly. I know
they are pointers in a sense, and I am just recently getting to understand
pointers. But I thought if you could finish the program in the way you had
explained I thought it would be a good thing to learn from.
And I don't understand the iterators (parameters) you are using, or how
they are being passed. And I am not sure how vectors are passed to
functions. So, I have not figured out a to print the vector to see if it is
being filled.
Here is the program as you have written it so far:

#include <iostream>
#include <vector>
using namespace std;

// fillVector takes a range
void fillVector(vector<int>::iterator first,
vector<int>::iterator last);

// printVector takes a range
//void printVector(vector<int>::iterator first, //off temp
// vector<int>::iterator last); //off temp

void main()
{
// create a vector of 100 ints
vector<int> vec(100);

// fill the entire vector (from begining to end)
fillVector(vec.begin(), vec.end());

// print the entire vector (from begining to end)
// printVector(vec.begin(), vec.end()); //off temp
}

void fillVector(vector<int>::iterator first,
vector<int>::iterator last)
{
int i = 0;
while(first != last)
{
++first;
++i;
}
cout<<vec(5)<<"\n\n"; //***not sure how to test print this***
}

Why are you wanting to print something here? Well you are not going to
be able to access vec from this function you are in a different
namespace. :) You might be wanting cout << *first << "\n\n"; ?

Sorry, I forgot to mention you might want to move the cout call into
the loop, before the ++first. Sorry about that.

Mar 2 '06 #23
cdg
Thanks for your post and it did help me to see how I could use vectors
instead of arrays. However, for now I would have to use them in a similar
way to arrays. But I was interested in just how iterators would be written.
If you would consider posting it. And is that with both functions.

#include <iostream>
#include <vector>
using namespace std;

void fillVector(vector<int>& v);

void main()
{
vector<int> v;
fillVector(v);
for(int i = 0; i < 52; i++)
cout << v[i]<<" ";
cout<<"\n\n";
}

void fillVector(vector<int>& v)
{
for(int i=0; i < 52; i++)
v.push_back(i);
}
Mar 2 '06 #24
On Wed, 1 Mar 2006 18:59:02 -0600 "cdg" <an****@anywhere.com> waved a
wand and this message magically appeared:

void fillVector(vector<int>::iterator first, vector<int>::iterator last)
{
int count = 0;

while (first != last)
*first++ = count++;
}

Easy peasy.

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 2 '06 #25
dd
You may use vector and auto_ptr (no need to care about memory and
optimize memory usage):

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

auto_ptr<vector<int> > GetArray()
{
auto_ptr<vector<int> > p(new vector<int>);
p->resize(100);
for (int i=0; i<100; i++)
{
(*p)[i] = i;
}
return p;
}

int main(int argc, char* argv[])
{
auto_ptr<vector<int> > p(GetArray());
for (
vector<int>::const_iterator it=p->begin();
it != p->end();
it++)
{
cout << (*it) << "\n";
}
return 0;
}

Mar 2 '06 #26
In article <BauNf.9783$Tf3.745@dukeread09>, "cdg" <an****@anywhere.com>
wrote:
Thanks for your post and it did help me to see how I could use vectors
instead of arrays. However, for now I would have to use them in a similar
way to arrays. But I was interested in just how iterators would be written.
If you would consider posting it. And is that with both functions.

#include <iostream>
#include <vector>
using namespace std;

void fillVector(vector<int>& v);

void main()
{
vector<int> v;
fillVector(v);
for(int i = 0; i < 52; i++)
cout << v[i]<<" ";
cout<<"\n\n";
}

void fillVector(vector<int>& v)
{
for(int i=0; i < 52; i++)
v.push_back(i);
}


#include <algorithm> // for generate_n and copy
#include <iostream> // for cout
#include <iterator> // for ostream_iterator, and back_inserter
#include <vector> // for vector

using namespace std;

// an object of type 'next_number' is a functor that returns 0 the first
// time its called, then 1, 2, 3 &c.
struct next_number {
int n;
next_number(): n( 0 ) { }
int operator()() {
return n++;
}
};

// create an integer vector, fill it with the numbers 0..51 and print
// it out
int main() {
vector<int> vec;
generate_n( back_inserter( vec ), 52, next_number() );
copy( vec.begin(), vec.end(), ostream_iterator<int>( cout, ", " ) );
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 2 '06 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.