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:S
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:S)

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.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please use code tags. Wrap all your code inside the code tags, like ..

[code]

your code here

[/code]

You still have ~15 minutes to edit your post.

Edited 6 Years Ago by mitrmkar: n/a

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