Monday 30 March 2009

Example to show the difference between struct and class in C++

This is a very simple example showing the difference between struct and class in C++. In reality there is hardly any difference between struct and class in C++. They are exactly the same except that the default is public in struct and private in class. You can do everything you can do in a class in a struct but its not a good practice. The reason being struct's are inherited from C programming and as the name suggests it was meant to represent a structure. You can also read the C++ FAQ that explains it better.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This very simple example demonstrates the basic difference
//between struct and class in C++

#include<iostream>

using namespace
std;

class
A {
int
a; //default in class is private

public
:
int
b;
void
initVariables(int c1, int c2, int c3)
{

a=c1, b=c2, c=c3;
}

void
printVariables()
{

cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl;
}


private
:
int
c;
};


struct
B {
int
a; //default in struct is public

public
:
int
b;
void
initVariables(int s1, int s2, int s3)
{

a=s1, b=s2, c=s3;
}

void
printVariables()
{

cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl;
}


private
:
int
c;
};


int
main()
{

A var1;
var1.initVariables(10, 11, 12);
//var1.a = 15; //Cant be changed as its private
var1.b = 16;
//var1.c = 17; //Cant be changed as its private
cout<<"Printing the variables from the class"<<endl;
var1.printVariables();

B var2;
var2.initVariables(20, 21, 22);
var2.a = 25; //possible
var2.b = 26;
//var2.c = 27; //Cant be changed as its private
cout<<"\n\nPrinting the variables from the struct"<<endl;
var2.printVariables();
cout<<"\n\n";

return
0;
}



The output is as follows:

Friday 27 March 2009

Example of Virtual Functions

The following is a simple example of Virtual functions. There is a Shape class which is the base class and three other classes are derived from the Shape class. The Circumference method in the base class is 'pure virtual' so it has to be defined in the derived classes else the compiler will complain about the class being abstract. Note that even if a base class has a single pure virtual function, it would become as an Abstract Base class.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This shows an example of C++ virtual function/methods
//This example also covers a pure virtual method
//This also shows an example of Inheritance
#include<iostream>

using namespace
std;

//Base Class
class Shape {
public
:
virtual
void Area(int length, int breadth)
{

cout<<"Shape: Area = "<<length * breadth<<endl;
}

virtual
void Circumference(int length, int breadth) = 0; //pure virtual
};

//Derived class - Inherits Shape as Public
class Rectangle : public Shape {
public
:
void
Circumference(int length, int breadth)
{

cout<<"Rectangle: Circumference = "<<2*(length + breadth)<<endl;
}
};


//Derived class - Inherits Shape as Public
class Square : public Shape {
public
:
//Overloaded Area because for Square length = breadth
void Area(int length)
{

Shape::Area(length, length);
}

//Overloaded Circumference because for Square length = breadth
void Circumference(int length)
{

cout<<"Square: Circumference = "<<(4 * length)<<endl;
}

void
Circumference(int length, int breadth)
{

Circumference(length);
}
};


//Derived class - Inherits Shape as Private as Area and Circumference
//for Square is very different from the Base class
class Circle : private Shape {
public
:
//Overloaded Area
void Area(int radius)
{

cout<<"Circle: Area = "<<(3.14 * radius * radius)<<endl;
}

//Overloaded Circumference
void Circumference(int radius)
{

cout<<"Circle: Circumference = "<<(2 * 3.14 * radius)<<endl;
}

private
:
//Nobody should call these methods
void Area(int length, int breadth) {};
void
Circumference(int length, int breadth) {};
};


int
main()
{

Rectangle r;
cout<<"\n\nRectangle Class - Dimensions 3 x 4"<<endl;
r.Area(3, 4);
r.Circumference(3, 4);

Square s;
cout<<"\n\nSquare Class - Dimensions 3 x 3"<<endl;
s.Area(3);
Shape *s1 = &s; //possible to access derived class through base
s1->Area(3,3);
s.Circumference(3);

Circle c;
cout<<"\n\nCircle Class - Radius is 3"<<endl;
//c.Area(3,3); //- Not Possible as its private in Circle
c.Area(3);
//Shape *c1 = &c; // not possible because conversion from 'Circle *'
// to 'Shape *' exists, but is inaccessible
//c.Circumference(3,3); //- Not Possible as its private in Circle
c.Circumference(3);
return
0;
}




The output of the program is as follows:

Wednesday 25 March 2009

Program to print itself

I came across this interesting program that can print itself. I have modified it from C to C++ and added few comments. Its worth exploring.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Challenge program to self print itself
//Original from http://www.cprogramming.com/challenges/solutions/self_print.html
#include<iostream>
char *program = "#include <iostream>%cchar *program = %c%s%c;%cint main()%c{%c printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c return 0;%c}%c";
int
main()
{

printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);
return
0;
}

//Note the trick
//In ASCII, 10 = LF or new line feed
//In ASCII, 34 = double quotes or "
//%c is printing charachter. 10 for new line and 34 for double quotes
//%s is printing the string




The output is as follows. Note that the aim is for the program to print itself and not the comments. Also the output is wrapped around.

Tuesday 24 March 2009

Simulating FIFO behaviour with priority queues for equal priorities

In priority queue, the C++ specifications does not define the behaviour of the algorithms when equal-priority items are involved. It is assumed that the ordering is not important in case of a priority queue as long as the same priority items are in correct order as compared to other priorities. For a programmer though this can be important as he expects FIFO ordering for same priority items. To overcome the problem I mentioned in earlier program, we can add another item to get correct FIFO ordering for same priority items.

This is just one approach and there may be other approaches. Also notice that I have used a local variable which requires the order to be input. You can also use the static variable approach for this.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Example of slightly advanced priority queue making sure that
//along with priorities, queue functionality of FIFO is maintained
#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace
std;

class
PrioritizedWord
{

private
:
int
m_prio;
string m_word;
int
m_order; //To set FIFO operation

public
:
explicit
PrioritizedWord()
{ }

explicit
PrioritizedWord(const std::string& word, const int priority = 0, const int order = 0):
m_word(word), m_prio(priority), m_order(order)
{ }

//Operator overloading for priority comparison
const bool operator <(const PrioritizedWord& pW) const
{

if
(m_prio == pW.m_prio)
return
(m_order > pW.m_order);
else
return
(m_prio < pW.m_prio);
}

//Operator overloading to print output
friend ostream& operator <<(ostream& out, const PrioritizedWord& pW)
{

out<<pW.m_word;
return
out;
}

int
getPrio(void)
{
return m_prio;}
string getWord(void)
{
return m_word;}
};


int
main()
{

priority_queue<PrioritizedWord> zahidQueue;

//Note the final order will not be the same as the specs do not
//define behaviour in case two numbers with equal priority exist
zahidQueue.push(PrioritizedWord("First", 23, 1));
zahidQueue.push(PrioritizedWord("Second", 23, 2));
zahidQueue.push(PrioritizedWord("Third", 23, 3));
zahidQueue.push(PrioritizedWord("Fourth", 51, 4));
zahidQueue.push(PrioritizedWord("Fifth", -1, 5));

cout<<"\n\nZahid Queue elements :"<<endl;
while
(!zahidQueue.empty())
{

cout<<zahidQueue.top()<<" "<<endl;
zahidQueue.pop();
}


return
0;
}



The output is as follows:

Saturday 21 March 2009

Basic String manipulation example

The following is a basic string manipulation example


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program intends to show basic string operations
#include<iostream>
#include<string>

using namespace
std;

int
main(void)
{

//Lets ask for a name and make a banner to welcome the person
//Adapted from Accelerated C++ by Andrew Koenig and Barbara Moo
string name;
cout << "\nHello, what is your name? ";
cin >> name;
cout << "\n\n\n\n";
string banner = "Welcome " + name; //String addition
int bannerSize = banner.size(); //We use this size for everything
string stars((bannerSize + 4), '*');
string filler(bannerSize, ' ');
cout << stars << endl;
cout << "* " << filler << " *" << endl;
cout << "* " << banner << " *" << endl;
cout << "* " << filler << " *" << endl;
cout << stars << endl;

//Now lets check some basic string pointer operations
cout<<"\n\n\n\n";
string strs;
{

char
*text="Some program";
cout<<"text = "<<text<<endl;

//copying char* to string
strs = text;
//text[0]='A'; //-- Not possible. Run time crash as text is const pointer
cout<<"str locally = "<<strs<<endl;
}

cout<<"str globally = "<<strs<<endl;

return
0;
}



The output is as follows:

Thursday 19 March 2009

Example of friend class and friend function

The following example shows a simple friend class and friend function example:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows a basic example of friend class and function
#include<iostream>

using namespace
std;

class
A {
public
:
int
square(int num)
{
return num*num;}
protected
:
int
reduced_square(int num)
{
return (num-1)*(num-1);}
private
:
int
cube(int num)
{
return num*num*num;}
friend class
B;
};


//Class B friend of class A
class B {
public
:
int
b_square(int num)
{

A a;
return
a.square(num);
}

int
b_reduced_square(int num)
{

A a;
return
a.reduced_square(num);
}

int
b_cube(int num)
{

A a;
return
a.cube(num);
}

private
:
int
b_priv;
int
b_default_cube()
{

return
b_cube(b_priv);
}

friend
int C(int c);
};


//function C friend of Class B
int C(int c)
{

B b;
b.b_priv = c;
//A a;
//return a.cube(b.b_priv); - Not possible as C is not friend of A
return b.b_default_cube();
}



int
main()
{

cout<<"\nExample of Friend Class and Function"<<endl;
A a;
cout<<"\nA: Square of 3 = "<<a.square(3)<<endl;
//The following is not possible
//cout<<"Reduced Square of 3 = "<<a.reduced_square(3)<<endl;
//cout<<"Cube of 3 = "<<a.cube(3)<<endl;

B b;
cout<<"\nB: Square of 4 = "<<b.b_square(4)<<endl;
cout<<"B: Reduced Square of 4 = "<<b.b_reduced_square(4)<<endl;
cout<<"B: Cube of 4 = "<<b.b_cube(4)<<endl;

cout<<"\nC: Cube of 5 = "<<C(5)<<endl;

return
0;
}



The output is as follows:

Tuesday 17 March 2009

An example of bound C++ maps

The following example shows how to define a bound C++ map which can only contain maximum number of specified elements. This is just an approach and there will probably be better and more efficient approaches.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//The example below shows a bound map. In a general C++ map you
//can add as many elements as required. In this case there can
//only be 10 elements in the map. If you try adding more it will
//generate an error
#include<iostream>
#include<map>
#include<string>
#include<assert.h>

using namespace
std;

map<int, string> freq;
int
a[10]={0,0,0,0,0,0,0,0,0,0};

int
store_in_maps(string s)
{

int
i;
for
(i=0; i < 10; i++)
{

if
(a[i]==0)
break
;
}

if
(i==10)
{

cout<<"Sorry no more Ids available in the pool"<<endl;
assert(0);
}


a[i]=1;
freq[i] = s;
return
i;
}


int
remove_from_map(string s)
{

map<int, string>::const_iterator iter;
for
(iter=freq.begin(); iter != freq.end(); ++iter)
{

if
(iter->second == s)
break
;
}

if
(iter==freq.end())
{

cout<<"Cant find the string "<<s<<endl;
return
(-1);
}

a[iter->first]=0;
return
iter->first;
}



int
main()
{

store_in_maps("first");
store_in_maps("second");
store_in_maps("third");
store_in_maps("fourth");
store_in_maps("fifth");
store_in_maps("sixth");
store_in_maps("seventh");
store_in_maps("eighth");
store_in_maps("ninth");
store_in_maps("tenth");
remove_from_map("fifth");
store_in_maps("eleventh");
remove_from_map("zahid");
remove_from_map("fourth");

map<int, string>::const_iterator iter;
for
(iter=freq.begin(); iter != freq.end(); ++iter)
{

cout<<"First: "<<(iter->first)<<" Second: "<<(iter->second)<<"\t\tPool value: "<<a[iter->first]<<endl;
}


return
0;
}



The output of the program is as follows:

Sunday 15 March 2009

An example of advanced erase in C++ maps

The following example shows how to use erase in C++ maps


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows an example of how C++ maps erase work.
#include <iostream>
#include <map>

using namespace
std;

int
main ()
{

map<char,int> zgmap;
map<char,int>::iterator it,it_low,it_up;

zgmap['a']=1;
zgmap['b']=2;
zgmap['c']=3;
zgmap['d']=4;
zgmap['e']=5;
zgmap['g']=6;
zgmap['j']=7;
zgmap['n']=8;

it_low=zgmap.lower_bound ('c'); // it_low points to c
it_up=zgmap.upper_bound ('j'); // it_up points to n and not j

zgmap.erase(it_low,it_up); // erases all elements from c to j

//Print Map contents
cout<<"\n** Printing the zgmap contents **"<<endl;
for
( it=zgmap.begin() ; it != zgmap.end(); it++ )
{

cout << (*it).first << " => " << (*it).second << endl;
}


return
0;
}



The output of the program is as follows:

Thursday 12 March 2009

An example of C++ maps

The following example shows the working of C++ maps. This also has a simple example of union which is sometimes used in C and C++ programs


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows an example of how maps work.
//In maps, the first parameter is key and second value
//The keys in the map are automatically softed from lower to higher
#include<iostream>
#include<map>
#include<string>

using namespace
std;

//defining a union that is used with newMap_
union uu
{

char
c;
int
i;
}
u;

//Lets define two different maps
//The first parameter is key and second value
map<string, int> portMap_;
map<void *, uu> newMap_;

int
main()
{

//first entry in portmap
portMap_["first"] = 1;

//example of using the iterator
map<string, int>::const_iterator it;
string z = "second";
it = portMap_.find(z); //not in the map so wont be found
if(it == portMap_.end())
{

portMap_[z] = 22; //add second element
}

//Add thrid element directly
z = "third";
portMap_[z] = 12345;

//Add 4th element by insert
portMap_.insert(pair<string,int>("fourth", 4444));

//Add 5th element by insert
portMap_.insert(pair<string,int>("fifth", 5555));


cout<<"\n** Printing the portmap_ values **"<<endl;
for
(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;

cout<<"\n** Removing fourth element **"<<endl;
z = "fourth";
it = portMap_.find(z);
portMap_.erase(it);

cout<<"\n** Printing the portmap_ values **"<<endl;
for
(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;

//Playing with New Map
cout<<"\n\nCreating New Map whose key is a void pointer"<<endl;

uu u_val1, u_val2;
void
*val1, *val2;
u_val1.i = 70, val1 = &u_val1;
newMap_[val1]=u_val1;

val2 = val1;
map<void *, uu>::const_iterator it_new;
it_new = newMap_.find(val2);
if
(it_new != newMap_.end())
{

u_val2 = it_new->second;
cout<<"Note that since u_val2 is a union you can print i or c as required"<<endl;
cout<<"val2 = "<<val2<<" value.c = "<<u_val2.c<<endl;
cout<<"val2 = "<<val2<<" value.i = "<<u_val2.i<<endl;
}


return
0;
}



The output is as follows:

Wednesday 11 March 2009

Multi-threading with Thread Synchronisation

Here is an example explaining the concept of multiple threads and their synchronisation:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Example from http://www.adrianxw.dk/SoftwareSite/Threads/Threads2.html
//This shows an example of Multithreading and thread synchronisation
#include <windows.h>
#include <process.h>
#include <iostream>
using namespace std;

void
Func1(void *);
void
Func2(void *);

CRITICAL_SECTION Section; //This will act as Mutex


int
main()
{

HANDLE hThreads[2];
InitializeCriticalSection(&Section);

//Create two threads and start them
hThreads[0] = (HANDLE)_beginthread(Func1, 0, NULL);
hThreads[1] = (HANDLE)_beginthread(Func2, 0, NULL);

//Makes sure that both the threads have finished before going further
WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);

//This is done after all threads have finished processing
DeleteCriticalSection(&Section);

cout << "Main exit" << endl;
return
0;
}


void
Func1(void *P)
{

int
Count;

for
(Count = 1; Count < 11; Count++)
{

EnterCriticalSection(&Section);
cout << "Func1 loop " << Count << endl;
LeaveCriticalSection(&Section);
}

return
;
}


void
Func2(void *P)
{

int
Count;

for
(Count = 10; Count > 0; Count--)
{

EnterCriticalSection(&Section);
cout << "Func2 loop " << Count << endl;
LeaveCriticalSection(&Section);
}

return
;
}





The output is as follows:
Complete explanation of the above program is available here.

Monday 9 March 2009

Simple example of Priority Queue

Here is a simple example of priority queue:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy

//Example showing how simple priority queues work using C++

#include <iostream>

#include <string>

#include <vector>

#include <queue>



using namespace
std;



class
PrioritizedWord

{


private
:

int
m_prio;

string m_word;



public
:

explicit
PrioritizedWord()

{ }


explicit
PrioritizedWord(const std::string& word, const int priority = 0):

m_word(word), m_prio(priority)

{ }


//Operator overloading for priority comparison

const bool operator <(const PrioritizedWord& pW) const

{


return
(m_prio < pW.m_prio);

}


//Operator overloading to print output

friend ostream& operator <<(ostream& out, const PrioritizedWord& pW)

{


out<<pW.m_word;

return
out;

}


int
getPrio(void)

{
return m_prio;}

string getWord(void)

{
return m_word;}

};




int
main()

{


int
somenum = 2;

while
(somenum > 0)

{


priority_queue<PrioritizedWord> wordQueue, zahidQueue, *temp=NULL;

//Word Queue first time, Zahid Queue second time

if(somenum%2==0)

{


temp = &wordQueue;

}


else


{


temp = &zahidQueue;

}




//Note the final order will not be the same as the specs do not

//define behaviour in case two numbers with equal priority exist

temp->push(PrioritizedWord("First", 23));

temp->push(PrioritizedWord("Second", 23));

temp->push(PrioritizedWord("Third", 23));

temp->push(PrioritizedWord("Fourth", 51));

temp->push(PrioritizedWord("Fifth", -1));



cout<<"\n\nWord Queue elements (All elements lost):"<<endl;

while
(!wordQueue.empty())

{


cout<<wordQueue.top()<<" "<<endl;

wordQueue.pop();

}




cout<<"\n\nZahid Queue elements (Elements preserved):"<<endl;

if
(!zahidQueue.empty())

{


priority_queue<PrioritizedWord> tempQueue;

while
(!zahidQueue.empty())

{


PrioritizedWord &temppW = zahidQueue.top();

cout<<temppW<<" "<<endl;

tempQueue.push(PrioritizedWord(temppW.getWord(), temppW.getPrio()));

zahidQueue.pop();

}


while
(!tempQueue.empty())

{


PrioritizedWord &temppW = tempQueue.top();

zahidQueue.push(PrioritizedWord(temppW.getWord(), temppW.getPrio()));

tempQueue.pop();

}

}




cout<<"\n\nZahid Queue - Removing an element and leaving the rest"<<endl;

{


priority_queue<PrioritizedWord> tempQueue;

while
(!zahidQueue.empty())

{


PrioritizedWord &temppW = zahidQueue.top();



if
(temppW.getWord() != "Second")

{


tempQueue.push(PrioritizedWord(temppW.getWord(), temppW.getPrio()));

zahidQueue.pop();

}


else


{


zahidQueue.pop();

break
;

}

}


while
(!tempQueue.empty())

{


PrioritizedWord &temppW = tempQueue.top();

zahidQueue.push(PrioritizedWord(temppW.getWord(), temppW.getPrio()));

tempQueue.pop();

}

}




cout<<"\n\nZahid Queue elements after removal of element"<<endl;

while
(!zahidQueue.empty())

{


cout<<zahidQueue.top()<<" "<<endl;

zahidQueue.pop();

}


somenum--;

}




return
0;

}


The output is as follows:

Sunday 8 March 2009

More on Vector Manipulation

This is slightly advanced vector manipulation program


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows advanced vector manipulation
#include<iostream>
#include<vector>

using namespace
std;

//This method prints the vector
void printer(vector<int> v)
{

unsigned int
i;
cout << "Size = " << v.size() << endl;
cout << "Contents = ";
for
(i = 0; i <v.size(); i++)
cout<<v[i]<<" ";
cout<<endl;
}


//Overloaded method same as above but takes pointers
void printer(vector<int*> v)
{

unsigned int
i;
cout << "\nOverloaded Method " << endl;
cout << "Size = " << v.size() << endl;
cout << "Contents = ";
for
(i = 0; i <v.size(); i++)
cout<<*v[i]<<" ";
cout<<endl;
}


int
main()
{

vector<int> v1(5, 1);
cout<<"** Original **"<<endl;
printer(v1);

unsigned int
i;
//Modifying the list above
for(i = 0; i < v1.size(); i++)
v1[i] = i + 3;
cout<<"\n** Modified **"<<endl;
printer(v1);

vector<int>::iterator it;
//Inserting in the list after the first element '0' and after the 4th elem '9 9 9 9'
it = v1.begin();
it += 1;
v1.insert(it,0);
it = v1.begin(); //Crash if you dont do this
it += 5; //Because we inserted '0', 4th element has become 5th.
v1.insert(it, 4, 9);
cout<<"\n** After Insert **"<<endl;
printer(v1);

//Testing Pop back - removes element from the end
v1.pop_back();
v1.pop_back();
cout<<"\n** After couple of Pop back's **"<<endl;
printer(v1);

//New Vector
cout<<endl<<"\n********** NEW *************"<<endl;
vector<int*> v2;
int
*a=new int(5);
int
*b=new int(6);
int
*c=new int(7);
int
*d=new int(11);
v2.push_back(a);
v2.push_back(b);
v2.push_back(c);
printer(v2);
cout<<"\ntwo pop_back and a push_back"<<endl;
v2.pop_back();
v2.pop_back();
v2.push_back(d);
printer(v2);
cout<<"\nClear Vector two"<<endl;
v2.clear();
printer(v2);

return
0;
}




The output is as follows:

Saturday 7 March 2009

Basic Vector Manipulation

Here is an example of basic vector manipulation




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy

//This program shows how to create vectors, check size and capacity,

//change the value of the elements and finally clear the vector

#include <iostream>

#include <vector>



using namespace
std;



int
main()

{


//Create a vector called vectorOne

vector<int> vectorOne; //Note size is 0

//Resize to 3 with default value 7

vectorOne.resize(3,7);

//Print the output to see the resize

cout<<"Test 1"<<endl;

for
(unsigned i=0;i<vectorOne.size();i++)

{


cout<<"Element: "<<i<<" Value: "<<vectorOne.at(i)<<endl;

}




//Resize further to 7 and new values default to 4

vectorOne.resize(7,4);

cout<<"\nTest 2"<<endl;

for
(unsigned i=0; i<vectorOne.size(); i++)

{


cout<<"Element: "<<i<<" Value: "<<vectorOne.at(i)<<endl;

}




//Check what the size and capacity of the vector is

cout<<endl;

cout<<"Size of vectorOne is: "<<vectorOne.size()<<endl;

cout<<"Capacity of vectorOne is: "<<vectorOne.capacity()<<endl;



//Lets modify the values of these vectors

for(unsigned i=0,j=22;i<vectorOne.size();i++,j+=i)

{


vectorOne.at(i)=j;

}




cout<<"\nTest 3"<<endl;

for
(unsigned i=0; i<vectorOne.size(); i++)

{


cout<<"Element: "<<i<<" Value: "<<vectorOne.at(i)<<endl;

}




//Use reserve to reallocate vectorOne with enough storage for 10 elements

vectorOne.reserve(10);

cout<<"\nSize of vectorOne is: "<<vectorOne.size()<<endl;

cout<<"Capacity of vectorOne is: "<<vectorOne.capacity()<<endl;



//Try resizing to 15 and dont add any default values for the new elements

vectorOne.resize(15);

cout<<"\nSize of vectorOne is: "<<vectorOne.size()<<endl;

cout<<"Capacity of vectorOne is: "<<vectorOne.capacity()<<endl;



//Remove all elements of the vector

vectorOne.clear();

cout<<"\nVector Cleared"<<endl;

cout<<"Size of vectorOne is: "<<vectorOne.size()<<endl;

cout<<"Capacity of vectorOne is: "<<vectorOne.capacity()<<endl;

cout<<endl;



return
0;

}


The output of the program is as follows: