Vector and Iterator
3 posters
Unix C++ :: UNIX CPP :: CPP :: Example Programs
Page 1 of 1
Vector and Iterator
- Code:
#include<iostream.h>
#include<vector.h>
#include<iomanip.h>
using namespace std;
struct stud
{
string name,sex;
int age;
};
int main()
{
vector<stud> details;
int count=1;
stud s;
cout<<"Enter Details of Student "<<count<<endl;
cout<<"Name : ";
while(cin>>s.name)
{
cout<<"Age : ";
cin>>s.age;
cout<<"Sex : ";
cin>>s.sex;
details.push_back(s);
++count;
cout<<"Enter Details of Student "<<count<<endl;
cout<<"Name : ";
}
for(vector<stud>::iterator i=details.begin();i!=details.end();i++)
{
cout<<setw(10)<<i->name<<" "<<setw(10)<<i->age<<" "<<setw(10)<<i->sex<<endl;
}
}
Last edited by Christopher on Tue Mar 02, 2010 6:09 pm; edited 1 time in total
hii
Can you explain what is a vector... is it having an array concept or something different...
Vineet_More- Posts : 27
Points : 31
Join date : 2010-02-26
Re: Vector and Iterator
Vector is also known as dynamic array. Difference between array and vector is storage is handled automatically in vectors but not in arrays. Vectors are most efficient if u want to add and delete elements from one end.
To know more about vectors, read this link.
http://www.cplusplus.com/reference/stl/vector/
To know more about vectors, read this link.
http://www.cplusplus.com/reference/stl/vector/
VECTOR
Can you just Explain what the following line is doing in the program:
vector<stud>::iterator i=details.begin();
vector<stud>::iterator i=details.begin();
Vineet_More- Posts : 27
Points : 31
Join date : 2010-02-26
Re: Vector and Iterator
initializing the value of the iterator which is of the type vector<stud>......it initializes with the starting value in the vector
crack2drop- Posts : 4
Points : 4
Join date : 2010-03-03
Age : 36
Location : Chennai
Re: Vector and Iterator
then for intialising the value instead of writing
vector<stud>::iterator i=details.begin();
we could have written
iterator i=details.begin(); as details has been declared as a vector.
what is the need of the scope resolution operator(:?
Can anyone explain please?
vector<stud>::iterator i=details.begin();
we could have written
iterator i=details.begin(); as details has been declared as a vector.
what is the need of the scope resolution operator(:?
Can anyone explain please?
Vineet_More- Posts : 27
Points : 31
Join date : 2010-02-26
Re: Vector and Iterator
for example, only integer pointer can hold the address of an integer variable. Same here, iterator of type vector<stud> only can hold the address of vector<stud> variable.
Unix C++ :: UNIX CPP :: CPP :: Example Programs
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum