Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Vector and Iterator

3 posters

Go down

Vector and Iterator Empty Vector and Iterator

Post by Christopher Tue Mar 02, 2010 5:52 pm

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
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Vector and Iterator Empty hii

Post by Vineet_More Tue Mar 02, 2010 5:56 pm

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

Back to top Go down

Vector and Iterator Empty Re: Vector and Iterator

Post by Christopher Tue Mar 02, 2010 6:16 pm

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/
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Vector and Iterator Empty VECTOR

Post by Vineet_More Wed Mar 03, 2010 11:52 am

Can you just Explain what the following line is doing in the program:
vector<stud>::iterator i=details.begin();

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

Back to top Go down

Vector and Iterator Empty Re: Vector and Iterator

Post by crack2drop Wed Mar 03, 2010 6:22 pm

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

Back to top Go down

Vector and Iterator Empty Re: Vector and Iterator

Post by Vineet_More Wed Mar 03, 2010 6:30 pm

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(:Smile?
Can anyone explain please?

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

Back to top Go down

Vector and Iterator Empty Re: Vector and Iterator

Post by Christopher Wed Mar 03, 2010 6:38 pm

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.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Vector and Iterator Empty Re: Vector and Iterator

Post by Vineet_More Wed Mar 03, 2010 6:53 pm

yupp.got it thanks..

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

Back to top Go down

Vector and Iterator Empty Re: Vector and Iterator

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum