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.

The traders have : traderid,bidid,tradertype(i.e buyer or asker), quantity and price. Ive managed to make a vector showing 10 traders of each.i.e 10 buyers and 10askers, also giving them a random value of quantity and price.

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:

int bidID,quantity,price,traderid;
char tradertype;
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 traderid;
int NUMBIDS=10;
char tradertype;
int bidID = Bid::BidID;
if(bidID<NUMBIDS)
{
traderid=0;
tradertype='A';
}
else
{
traderid=1; 
tradertype='B';
}


return Bid(traderid,tradertype,quantity, price);
}

//*****************************************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) 
{
cout<<"*******************Only Trader Ask*************************"<<endl;

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

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

void show3(const vector<Bid> &vect) 
{
cout<<"*******************Only Trader Buys*************************"<<endl;

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:

   int bidID, quantity, price, traderid;
   char tradertype;
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 traderid;
   int NUMBIDS = 10;
   char tradertype;
   int bidID = Bid::BidID;
   if ( bidID < NUMBIDS )
   {
      traderid = 0;
      tradertype = 'A';
   }
   else
   {
      traderid = 1;
      tradertype = 'B';
   }

   return Bid ( traderid, tradertype, quantity, price );
}

//*****************************************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 )
{
   cout << "*******************Only Trader Ask*************************" << endl;

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

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

void show3 ( const vector<Bid> &vect )
{
   cout << "*******************Only Trader Buys*************************" << endl;

   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[0]; 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[0].getNameOrWhatever();

Edited 6 Years Ago by iamthwee: n/a

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

This article has been dead for over six months. Start a new discussion instead.