"Alf P. Steinbach" wrote:
* Fil -> Alex Blekhman:
I keep note of the vector
thing for later, when I am more confident with the classes.
On the contrary, unless you really want to struggle you should start using
std::vector immediately, and only use raw arrays for things like initialization
data.
C++0x will to a large extent obviate the need also for that use of raw arrays,
but we're not there yet.
'main' must have result type 'int' (even if MSVC incorrectly accepts 'void').
Also, using 'void' to indicate empty argument list is a C-ism: in C++ it serves
no purpose other than C compatibility.
Here's good C++ 'main':
int main()
{
// Whatever.
}
{
int myArray[7]={6,3,5,7,1,2,4};
int sizeOfArray;
sizeOfArray=sizeof(myArray)/sizeof(int);
int * mySortedArray;
mySortedArray=new int [sizeOfArray];
mySortedArray=sortArray(myArray,sizeOfArray);
The memory allocated for `mySortedArray' is leaked. A value you
return from `sortArray' function overwrites the one stored in
`mySortedArray'. So, the pointer is lost forever and memory is
leaked.
I thought I had to keep some space in the memory to store the result of the
function. Should I just write this:
int * mySortedArray;
mySortedArray=sortArray(myArray,sizeOfArray);
No, you should either sort in place, like
#include <iostream> // std::cout
#include <ostream> // std::endl, operator<<
#include <algorithm> // std::sort
// At newbie level, just regard this functions as magic:
template< typename T, std::size_t N >
std::size_t nElementsOf( T (&)[N] ) { return N; }
// Then:
int main()
{
int a[] = {6,3,5,7,1,2,4};
std::sort( &a[0], &a[nElementsOf(a)] );
for( std::size_t i = 0; i < nElementsOf( a ); ++i )
{
std::cout << a[i] << std::endl;
}
}
or use std::vector to make a copy (std::vector will deal with proper
deallocation, which otherwise is very difficult to get right).
Before showing that, a little change of /notation/.
Note that this version does exactly the same as the above one, just via 2 little
helper functions for the direct expressions in the above code:
#include <iostream> // std::cout
#include <ostream> // std::endl, operator<<
#include <algorithm> // std::sort
// As before...
template< typename T, std::size_t N >
std::size_t nElementsOf( T (&)[N] ) { return N; }
template< typename T, std::size_t N >
T* startOf( T (&a)[N] ) { return &a[0]; }
template< typename T, std::size_t N >
T* endOf( T (&a)[N] ) { return &a[N]; }
int main()
{
int a[] = {6,3,5,7,1,2,4};
std::sort( startOf(a), endOf(a) );
for( std::size_t i = 0; i < nElementsOf( a ); ++i )
{
std::cout << a[i] << std::endl;
}
}
Now, here's how to use std::vector to make a copy of the data, and sort that copy:
#include <iostream> // std::cout
#include <ostream> // std::endl, operator<<
#include <algorithm> // std::sort
#include <vector> // std::vector
// As before...
template< typename T, std::size_t N >
std::size_t nElementsOf( T (&)[N] ) { return N; }
template< typename T, std::size_t N >
T* startOf( T (&a)[N] ) { return &a[0]; }
template< typename T, std::size_t N >
T* endOf( T (&a)[N] ) { return &a[N]; }
int main()
{
int a[] = {6,3,5,7,1,2,4};
std::vector<int> v( startOf(a), endOf(a) );
std::sort( v.begin(), v.end() );
for( std::size_t i = 0; i < v.size(); ++i )
{
std::cout << v[i] << std::endl;
}
}
The 'begin' member function of std::vector corresponds to raw array 'startOf',
yielding something that refers to the first element, and the 'end' member
corresponds to raw array 'endOf', something that refers to a hypothetical
element right after the last actual element, "one past the end".
And since the program now sorts a copy of the data, the initial data can be made
'const' (guarantee of no change) and 'static' (allocated at program start up,
not using stack space):
static int const a[] = {6,3,5,7,1,2,4};
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?