Arrays and Vectors

Arrays
Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.
Internally, an array does not keep any data other than the elements it contains (not even its size, which is a template parameter, fixed on compile time). It is as efficient in terms of storage size as an ordinary array declared with the language's bracket syntax ([]). This class merely adds a layer of member and global functions to it, so that arrays can be used as standard containers.
Unlike the other standard containers, arrays have a fixed size and do not manage the allocation of its elements through an allocator: they are an aggregate type encapsulating a fixed-size array of elements. Therefore, they cannot be expanded or contracted dynamically.
Zero-sized arrays are valid, but they should not be dereferenced (members front, back, and data).
Unlike with the other containers in the Standard Library, swapping two array containers is a linear operation that involves swapping all the elements in the ranges individually, which generally is a considerably less efficient operation. On the other side, this allows the iterators to elements in both containers to keep their original container association.
Another unique feature of array containers is that they can be treated as tuple objects: The header overloads the get function to access the elements of the array as if it was a tuple, as well as specialized tuple_size and tuple_element types.
// To print the Transpose of Matices
#include< iostream.h >
#include< conio.h >
main()
{
int a[3][3],b[3][3],r,c;
cout <<"Enter the Matrix 3X3\n";
for(r=0;r<3;r++)
for(c=0;c<3;c++)
cin >>a[r][c];
clrscr();
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[c][r]=a[r][c];
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
printf("%d",a[r][c]);
printf("\n");
}
cout <<"Transpose of Matrix\n";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
cout< cout<<"\n";
}
getch();
}
Vectors
Vectors are sequence containers representing arrays that can change in size.
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.
Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.
Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).
Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.
Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.
Example: - This expreiment execute in Dev-c++
#include < iostream >
#include < vector >
int main ()
{
std::vector first;
std::vector second;
std::vector third;
first.assign (7,100); // 7 ints with a value of 100
std::vector::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
std::cout << "Size of third: " << int (third.size()) << '\n';
int c;
c=getchar();
putchar(c);
return 0;
}
Output: -
Size of first: 7
Size of second: 5
Size of third: 3
std::array is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.
std::vector is a small class containing pointers into the heap. (So when you allocate a std::vector, it always calls new.) They are slightly slower to access because those pointers have to be chased to get to the arrayed data... But in exchange for that, they can be resized and they only take a trivial amount of stack space no matter how large they are.
Command Line Arguments
Command-line parameters are passed to a program at runt-time by the operating system when the program is requested by another program, such as a command interpreter ("shell") like cmd.exe on Windows or bash on Linux and OS X. The user types a command and the shell calls the operating system to run the program. Exactly how this is done is beyond the scope of this article (on Windows, look up CreateProcess; on UNIX and UNIX-like systems look up fork(3) and exec(3) in the manual).
The uses for command-line parameters are various, but the main two are:
* Modifying program behaviour - command-line parameters can be used to tell a program how you expect it to behave; for example, some programs have a -q (quiet) option to tell them not to output as much text.
*Having a program run without user interaction - this is especially useful for programs that are called from scripts or other programs.
The first argument (argc) is the number of elements in the array, which is the second argument (argv). The second argument is always an array of char*, because the arguments are passed from the command line as character arrays (an array can be passed only as a pointer). Each whitespace-delimited cluster of characters on the command line is turned into a separate array argument. The following program prints out all its command-line arguments by stepping through the array:
Ex : -
#include < iostream.h >
#include < stdio.h >
#include < conio.h >
#include < stdlib.h >
int main(int argc, char* argv[])
{
int c;
for(int i = 1; i < argc; i++)
cout << atoi(argv[i]) << endl;
c=getchar();
putchar(c);
getch();
return 0;
}
In this program, you can put any number of arguments on the command line. You’ll notice that the for loop starts at the value 1 to skip over the program name at argv[0]. Also, if you put a floating-point number containing a decimal point on the command line, atoi( ) takes only the digits up to the decimal point. If you put non-numbers on the command line, these come back from atoi( ) as zero.
inline Functions
The inline specifier tells the compiler to expand a function’s code inline rather than calling the function. The inline specifier is a request, not a command, because several factors may prevent a function’s code from being expanded inline. Some common restrictions include recursive functions, functions that contain loops or switch statements, or functions that contain static data. The inline specifier precedes the rest of a function’s declaration.
The benefits of defining a function for such a small operation include:
* It is easier to read and understand a call to shorterString than it would be to read and interpret an expression that used the equivalent conditional expression in place of the function call.
* If a change needs to be made, it is easier to change the function than to find and change every occurrence of the equivalent expression.
* Using a function ensures uniform behavior. Each test is guaranteed to be implemented in the same manner.
* The function can be reused rather than rewritten for other applications.
#include < iostream.h >
inline float mul(float x, float y)
{
return(x*y);
}
inline double div (double p, double q)
{
return(p/q);
}
int main()
{
float a=12.75;
float b=9.75
cout << mul(a,b) << "\n";
cout << div(a,b) << "\n";
return 0;
}


Free Web Hosting