STL Container Class Introduction
CoderSource.net
STL Container Class Introduction - Article by consulttoday
Level: BeginnerType: Article
Rating: Page: 2 of 3

Date: 12/23/2005 12:00:00 AM

Environment: C++, Unix, Windows

clear() , erase()


As the name suggests clear() method clears the vector elements.

So the size of the vector becomes zero after the clear(). On the other hand erase helps to erase a particular element from the vector or a range of vectors. erase() has 2 overloaded versions for this purposes. One overloaded method takes one iterator the other one takes 2 iterators as starting and finishing deleting locations. Here is a code to illustrate

#include <iostream>
#include <vector>
using namespace std;
void Display(vector<int> m)
{
for(int i=0;i<m.size();i++)
cout<<m.at(i)<<endl;
}
int main()
{
vector<int> m;
for(int i=0;i<5;i++)
m.push_back(i);
cout<<"The full list is "<<endl;
Display(m);
cout<<"After deleting the first element "<<endl;
m.erase(m.begin());//Deleting the first element
Display(m);
cout<<"After deleting the first 2 elements"<<endl;
//Deleting first 2 elements
m.erase(m.begin(),m.begin()+2);
Display(m);
return 0;
}

The output of this code is

The full list is
0
1
2
3
4
After deleting the first element
1
2
3
4
After deleting the first 2 elements
3
4

empty()


This method checks whether the vector is empty or not. This returns a bool value if the vector size is 0, i.e., the vector is empty. Here is the code using empty()

#include <iostream>
#include <vector>

using namespace std;

voidDisplay(vector<int> m)
{
for(int i=0;i<m.size();i++)
cout<<m.at(i)<<endl;
}
int main()
{
vector<int> m;
for(int i=0;i<5;i++)
m.push_back(i);
cout<<"The full list is "<<endl;
if(!m.empty())
Display(m);
else
System.out.println(“The vector is empty”);
return 0;
}

swap()


This method swaps two vectors. Here is the code

#include <iostream>
#include <vector>

using namespace std;
voidDisplay(vector<int> m)
{
for(int i=0;i<m.size();i++)
cout<<m.at(i)<<endl;
}
int main()
{
vector<int> m;
vector<int> n(5,2);
for(int i=0;i<5;i++)
m.push_back(i);
cout<<"m :"<<endl;
Display(m);
cout<<"n :"<<endl;
Display(n);
m.swap(n);
cout<<"m :"<<endl;
Display(m);
cout<<"n :"<<endl;
Display(n);
return 0;
}

Here we have two vectors initially. Vector m takes the values 0 to 4 while the vector n takes the values 2. Here is the output of the code.

m :
0
1
2
3
4
n :
2
2
2
2
2
m :
2
2
2
2
2
n :
0
1
2
3
4

As you can see values of m and n are swapped.

max_size()


vectors can grow and diminish in runtime, but they also have a maximum size. This method returns the maximum size possible for a vector. As you may have already guessed, these values are different from type to type. But this value is same for a particular type. Suppose you want to declare a vector of int and another of int pointers. Both will have the same maximum size.

Here is a code that prints some of the max sizes.

#include <iostream>
#include <vector>

using namespace std;
int main()
{
vector<int> iv;
vector<float> fv;
vector<double> dv;
vector<char> cv;
vector<string> sv;
vector<bool> bv;

cout<<"Integer Vector Max Size :"<<iv.max_size()<<endl;
cout<<"Float Vector Max Size :"<<fv.max_size()<<endl;
cout<<"Double Vector Max Size :"<<dv.max_size()<<endl;
cout<<"Character Vector Max Size :"<<cv.max_size()<<endl;
cout<<"String Vector Max Size :"<<sv.max_size()<<endl;
cout<<"Boolean Vector Max Size :"<<bv.max_size()<<endl;

return 0;
}

Here is the output of the code.

Integer Vector Max Size :1073741823
Float Vector Max Size :1073741823
Double Vector Max Size :536870911
Character Vector Max Size :4294967295
String Vector Max Size :268435455
Boolean Vector Max Size :4294967295

reserve()


This method will reserve the values already there in the vector and then increase the capacity of the vector. Here is the code.

#include <iostream>
#include <vector>

using namespace std;

voidDisplay(vector<int> m)
{
for(int i=0;i<m.capacity();i++)
cout<<m.at(i)<<endl;
}

int main()
{
vector<int> m(4,3);
cout<<m.capacity()<<endl;
m.reserve(10);
cout<<m.capacity()<<endl;
Display(m);
return 0;
}

Here previously the capacity of the vector was 4. Then the vector size is increased but the values previously there in the vector will not be erased.

resize()


resize(), as the name suggests the method resizes the vector . If the argument given is greater than the size, then the capacity will be increased. We can also put a value to fill the remaining space.

#include <iostream>
#include <vector>

using namespace std;

voidDisplay(vector<int> m)
{
for(int i=0;i<m.capacity();i++)
cout<<m.at(i)<<endl;
}

int main()
{
vector<int> m(4,3);
cout<<m.capacity()<<endl;
m.resize(8,3);
cout<<m.capacity()<<endl;
Display(m);
return 0;
}

Here is the output of the program

4
8
3
3
3
3
3
3
3
3

As you can see the capacity of the vector is increased to 8.

rbegin(), rend()


rbegin() returns the iterator to the first element if the vector is reversed. Say there is a vector containing 1,4,5,6 then rbegin () returns an iterator for 6. On the other hand rend() returns the iterator to a location beyond the physical end when the vector is reversed.

In a nutshell, rbegin() and rend() are nothing but the reverse version of begin() and end(). Here is a code to illustrate their usages.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> m;
m.push_back(11);
m.push_back(12);
m.push_back(23);
m.push_back(25);
cout<<*m.rbegin()<<endl;
cout<<*(m.rend()-1)<<endl;
return 0;
}

The output of this code is

25
11

1 2 3

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us