A
axcytz
Hi all,
If I have a 1D array and a function that returns this array, how do i do it?
Thanks!
If I have a 1D array and a function that returns this array, how do i do it?
Thanks!
If I have a 1D array and a function that returns this array, how do i do it?
For a general fixed size array, use std::array.
For a general variable length array, use std::vector.
For an array of numbers to be processed in the same way, use std::valarray.
For a string, use std::string or std::wstring, depending on the
character type.
You don't ask about 2D arrays, but the answer there is, for the general
case, to roll your own (e.g. based on std::vector) or use a third party
one such as the Boost matrix class. For the case of image, however, use
the Boost image class. Disclaimer: I haven't used that image class.
Cheers & hth.,
- Alf
Thanks for your help firstly. What I tried is:
Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}
swap(UpdateArray[0], UpdateArray[1]);
return UpdateArray;
}
Something like this. Is the syntax correct? Can I just say return UpdateArray?
For a general fixed size array, use std::array.
For a general variable length array, use std::vector.
For an array of numbers to be processed in the same way, use std::valarray.
For a string, use std::string or std::wstring, depending on the
character type.
You don't ask about 2D arrays, but the answer there is, for the general
case, to roll your own (e.g. based on std::vector) or use a third party
one such as the Boost matrix class. For the case of image, however, use
the Boost image class. Disclaimer: I haven't used that image class.
Cheers & hth.,
- Alf
Thanks for your help firstly. What I tried is:
Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}
swap(UpdateArray[0], UpdateArray[1]);
return UpdateArray;
}
Something like this. Is the syntax correct? Can I just say return UpdateArray?
Whenever possible, try to use std::vector, std::array, and other
standard container classes that automatically check array bounds.
(If your instructor told you to use raw arrays for this assignment, please
tell him/her that comp.lang.c++ disapproves of your curriculum![]()
On 23.10.2013 03:22, (e-mail address removed) wrote:
If I have a 1D array and a function that returns this array, how do i do it?
For a general fixed size array, use std::array.
For a general variable length array, use std::vector.
For an array of numbers to be processed in the same way, use std::valarray.
For a string, use std::string or std::wstring, depending on the
character type.
You don't ask about 2D arrays, but the answer there is, for the general
case, to roll your own (e.g. based on std::vector) or use a third party
one such as the Boost matrix class. For the case of image, however, use
the Boost image class. Disclaimer: I haven't used that image class.
Cheers & hth.,
- Alf
Thanks for your help firstly. What I tried is:
Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}
swap(UpdateArray[0], UpdateArray[1]);
return UpdateArray;
}
Something like this. Is the syntax correct? Can I just say return UpdateArray?
First of all, it's *very* dangerous to get in the habbit of assuming
things about the lengths of raw C++ arrays. C++ does *not* check bounds
on raw arrays. If one (or both) of the two arrays passed into that
function happened to be smaller than NumElement, then you're looking at
undefined behaviour. Typically, you'll read (or write) past the end of
the array and get data corruption, crashes and/or points where a clever
hacker could inject arbitrary binary code into your program. So if you
must deal with raw arrays, ALWAYS pass the sizes around with them and
verify that every read or write is in bounds.
Whenever possible, try to use std::vector, std::array, and other
standard container classes that automatically check array bounds. (If
your instructor told you to use raw arrays for this assignment, please
tell him/her that comp.lang.c++ disapproves of your curriculum
Second of all, no, that syntax isn't correct. What error messages does
your compiler give you? They should point you in the right direction.
As for returning the array, look up "pass by value" and "pass by
reference".
Rennie
On 23.10.2013 03:22, (e-mail address removed) wrote:
For a general fixed size array, use std::array.
For a general variable length array, use std::vector.
For an array of numbers to be processed in the same way, use std::valarray.
For a string, use std::string or std::wstring, depending on the
character type.
You don't ask about 2D arrays, but the answer there is, for the general
case, to roll your own (e.g. based on std::vector) or use a third party
one such as the Boost matrix class. For the case of image, however, use
the Boost image class. Disclaimer: I haven't used that image class.
Cheers & hth.,
- Alf
Thanks for your help firstly. What I tried is:
Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}
swap(UpdateArray[0], UpdateArray[1]);
return UpdateArray;
}
Something like this. Is the syntax correct? Can I just say return UpdateArr
Thanks for your help firstly. What I tried is:
Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}
swap(UpdateArray[0], UpdateArray[1]);
return UpdateArray;
}
Something like this. Is the syntax correct? Can I just say
return UpdateArray?
No, you can't return an array.
Thanks for your help firstly. What I tried is:
Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}
swap(UpdateArray[0], UpdateArray[1]);
return UpdateArray;
}
Something like this. Is the syntax correct?
What you're doing here would, in C++, be done using iterators:
template<class It>
void Function(It src, size_t n, It dest)
{
std::copy(src, src+n, dest);
assert(n>1);
std::swap(*dest, *dest+1);
}
James said:Thanks for your help firstly. What I tried is:
Function(int myArray[], int UpdateArray[])
{
for(int t=0; t < NumElement; t++)
{
UpdateArray[t] = myArray[t];
}
swap(UpdateArray[0], UpdateArray[1]);
return UpdateArray;
}
Something like this. Is the syntax correct? Can I just say
return UpdateArray?No, you can't return an array.
But UpdateArray isn't an array.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.