0% found this document useful (0 votes)
4 views

Customclass Vector Exercse Year2

Uploaded by

slamyume1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Customclass Vector Exercse Year2

Uploaded by

slamyume1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

example: using a custom class with vector ...

class person
{
int age;
string name ;
public:
person(const int age, const string name);
int getage() const;
string getname() const ;
//....
};
it is possible to create a menu option

1. add some person information/data to vector of person


2. then print data of person storing in vector
3. sorting age of person by ascending and descending order
4. sorting name of person by ascending and descending order
(note : using sort algorithm for vector )
5. finding a person by name in vector..
6. delete/remove a person from vector by name ..
7. insert a person to list by any pos..
//....

//vector : copy
// : sort ; ascending, descending ...
// : find
Here is an example using a custom class B in vector..
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class B {
int x;
public:
B(const int xx=0):x(xx){ }
void setx(const int xx ){
x=xx;
}
int getx() const { return x;}
friend int compareascending(const B &a, const B &b);
friend int comparedescending(const B &a, const B &b);
// this operator is using to find custom object
friend bool operator==(const B &a, const B &b){
cout <<"calling ==" <<endl;
return a.x==b.x;
}
};
int compareascending(const B &a, const B &b){
return a.getx() < b.getx();
}
int comparedescending(const B &a, const B &b){
return a.getx() > b.getx();
}

int main()
{
int array[]={20,4,3,5,8,9};
int n=sizeof(array)/sizeof(array[0]);
vector<B> myb;
vector<B*> myc; // object as pointer
// adding element to normal object
for(int i=0;i<n;++i)
myb.push_back(B(array[i]));
// adding elements to pointer object
// so constructor need using new operator is new B(?)
for(int i=0;i<n;++i)
myc.push_back(new B(array[i]));

cout <<"elements myb\n" ;


for(auto obj:myb)
cout <<obj.getx() <<endl;

cout <<"elements myc\n" ;


for(auto obj:myc)
cout <<obj->getx() <<endl;
vector<B *> ::iterator i=myc.begin();
//myc.insert(myc.begin(),cc.getx());
int a=100;
// insert new element to the beginning
myc.insert(myc.begin(), new B(a));
cout <<"elements myc\n" ;
for(auto obj:myc)
cout <<obj->getx() <<endl;
// remove or delete second element to the end
myc.erase(myc.begin()+1,myc.end());
cout <<"elements myc\n" ;
for(auto obj:myc)
cout <<obj->getx() <<endl;

// sorting myb vector

sort(myb.begin(),myb.end(),compareascending);
cout <<"elements myb\n" ;
for(auto obj:myb)
cout <<obj.getx() <<endl;
reverse(myb.begin(),myb.end());
cout <<"reverse elements myb\n" ;
for(auto obj:myb)
cout <<obj.getx() <<endl;
// finding an element of vector
int value;
cout <<"enter a find element ?";
cin>> value;
vector<int> l{1,2,4,5,100};
auto item=find(l.begin(),l.end(),100);
if(item!=l.end())
cout <<"found " <<endl;
else cout <<"not found " <<endl;
cout <<"enter v ?";
cin>>value;
auto it=find(myb.begin(),myb.end(),B(value));
if(it!=myb.end())
cout <<"The element "<< value << " found at pos :"
<<it-myb.begin() <<endl;
else
cout <<"The element " << value <<"not found " <<endl;

You might also like