Hey,
Ok i needed quick help on vectors in C++.I have a vector which consits of a complete bidlist for Traders i.e Askers and Buyers. Now what i want to do is,Get the first ask bid and compare that with the Buy bids,via the price and quantity.

Now what i want to do is in another class called Simulator, i want to be able to get the first asker and compare it with the buyers via their prices and quantity.

How do extract the first element in the vector and compare it?Im new to vectors so im not quite sure on how to do it
This is what ive beena able to do so far:

``````class Bid
{
private:

public:
static int BidID;
{
bidID = Bid::BidID++;
}
//**************************************************

void show(ostream &out) const
{
}

//***************************************
void show2(ostream &out) const
{
}
//********************************************
void show3(ostream &out) const
{
}

//********************************************

bool operator<(const Bid &other) const
{
return price < other.price;
}

};

int Bid:: BidID=0;

//*******************Sets the values for the Bid List*********************************
Bid getBids()
{
int MinPrice=50, MaxPrice=150;
int range1=(MaxPrice-MinPrice)+1;
int MinQuantity=30, MaxQuantity=100;
int range2=(MaxQuantity-MinQuantity)+1;

int quantity = MinQuantity+int(range1*rand()/(RAND_MAX + 1.0));
int price = MinPrice+int(range2*rand()/(RAND_MAX + 1.0));

int NUMBIDS=10;
int bidID = Bid::BidID;
if(bidID<NUMBIDS)
{
}
else
{
}

}

//*****************************************Adds the random values to the Vector*******************
void theBidlist(vector<Bid> &list)
{
int numbids=20;
for (int i=0;i<numbids;i++)
{
list.push_back(getBids());
}
}

//*******************************************Scans through the vector****************************
void show(const vector<Bid> &vect)
{
cout<<"********************************************"<<endl;

for(vector<Bid>::const_iterator sh=vect.begin(); sh != vect.end(); ++sh)
{
sh->show(cout);
}
cout << endl;
}

//****************************************************************************************************

void show2(const vector<Bid> &vect)
{

for(vector<Bid>::const_iterator sh=vect.begin(); sh != vect.end(); ++sh)
{
sh->show2(cout);
}
cout << endl;
}

//**************************************************************************************************

void show3(const vector<Bid> &vect)
{

for(vector<Bid>::const_iterator sh=vect.begin(); sh != vect.end(); ++sh)
{
sh->show3(cout);
}
cout << endl;
}

(Does pretty much nothing for now.I needed it to work for matching.But im not sure on how to do it)

Simulator:

#include<iostream>
using namespace std;
class Simulator
{

vector<Bid> BidList;

public:
void run();

};

void Simulator::run()
{
theBidlist(BidList);
show(BidList);
cout<<"Bid list after sort:"<<endl;
sort(BidList.begin(), BidList.end());
show2(BidList);
show3(BidList);
}``````

I dont need u to give me a whole program i just need help on figuring out on how i can actually get a element in a vector and compare it with the rest.

Although you've posted using code tags please consider proper indentation for future. Otherwise, it's pointless.

``````class Bid
{
private:

public:
static int BidID;
Bid ( int tid, char ttype, int p , int q ) : traderid ( tid ), …``````

## All 2 Replies

Although you've posted using code tags please consider proper indentation for future. Otherwise, it's pointless.

``````class Bid
{
private:

public:
static int BidID;
Bid ( int tid, char ttype, int p , int q ) : traderid ( tid ), tradertype ( ttype ), quantity ( p ), price ( q )
{
bidID = Bid::BidID++;
}
//**************************************************

void show ( ostream &out ) const
{
out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
}

//***************************************
void show2 ( ostream &out ) const
{
if ( tradertype == 'A' )
out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
}
//********************************************
void show3 ( ostream &out ) const
{
if ( tradertype == 'B' )
out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
}

//********************************************

bool operator< ( const Bid &other ) const
{
return price < other.price;
}
};

int Bid:: BidID = 0;

//*******************Sets the values for the Bid List*********************************
Bid getBids()
{
int MinPrice = 50, MaxPrice = 150;
int range1 = ( MaxPrice - MinPrice ) + 1;
int MinQuantity = 30, MaxQuantity = 100;
int range2 = ( MaxQuantity - MinQuantity ) + 1;

int quantity = MinQuantity + int ( range1 * rand() / ( RAND_MAX + 1.0 ) );
int price = MinPrice + int ( range2 * rand() / ( RAND_MAX + 1.0 ) );

int NUMBIDS = 10;
int bidID = Bid::BidID;
if ( bidID < NUMBIDS )
{
}
else
{
}

}

//*****************************************Adds the random values to the Vector*******************
void theBidlist ( vector<Bid> &list )
{
int numbids = 20;
for ( int i = 0;i < numbids;i++ )
{
list.push_back ( getBids() );
}
}

//*******************************************Scans through the vector****************************
void show ( const vector<Bid> &vect )
{
cout << "********************************************" << endl;

for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
{
sh->show ( cout );
}
cout << endl;
}

//****************************************************************************************************

void show2 ( const vector<Bid> &vect )
{

for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
{
sh->show2 ( cout );
}
cout << endl;
}

//**************************************************************************************************

void show3 ( const vector<Bid> &vect )
{

for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
{
sh->show3 ( cout );
}
cout << endl;
}

#include <iostream>

using namespace std;
class Simulator
{

vector<Bid> BidList;
public:
void run();
};

void Simulator::run()
{
theBidlist ( BidList );
show ( BidList );
cout << "Bid list after sort:" << endl;
sort ( BidList.begin(), BidList.end() );
show2 ( BidList );
show3 ( BidList );
}``````

How do extract the first element in the vector and compare it?

Vectors can be accessed like an array...

So you could do:- `vector_array;` Seeing as you have a vector of objects you would need to call the method for that object to do any comparison.

For example:- `vector_array.getNameOrWhatever();`

So say if i needed to get the bids from the bids class into the simulator,how would i declare that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.