Programming and Graphics

(Kiana) #1

5.3 Sorting with the STL 137


char oros[l];
char * memad;

for (int i=0;i<l;i++)
{
memad = &onoma[i];
oros[i] = *memad;
cout << oros[i];
}

Running the code produces the output:


arkouditsa

Problems


5.2.1.Describe the action of the following statements:


float a[5];
float * pnt = a;
pnt=3.5;

5.2.2.Explore whether it is possible to convert a character array into a string.


5.3 Sorting with the STL.........................


Consider a vectorvdefined in the main program and passed to a function named
exfnc(v)as a function argument,exfnc(v). We have seen that, in fact, the
main program passes to the function the memory address of the first element of
the vectorv. Accordingly,vinexfnc(v)should be interpreted as a pointer.


C++ includes the standard template library (STL) offering a variety of
utility functions and data structures (see Appendix G). Among them is the
functionsortthat sorts a a subset of a list encapsulated in a vectorv[i],
wherei=0,...,Nāˆ’1. This function receives as input the memory address of
the first element of the subset, and the memory address of the last element of
the subset increased by one unit.


For example, ifvis a vector of floats, the following statements will sort
the whole list:


float * pnt = &v[0];
sort(pnt, pnt+N);
Free download pdf