changing structural data to a vector

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1187 Views - Last Post: 03 May 2010 - 10:11 AM Rate Topic: -----

#1 Guest_willison*


Reputation:

changing structural data to a vector

Posted 01 May 2010 - 09:14 PM

How can I convert my program to use a vector container so that I can manipulate the data in it.Aparently is just a structure. I still expect the same output but data grouped in a vector container giving me access to each element.

#include <iostream> 
#include <vector> 
#include <string> 
#include <algorithm> 
#include <cstdlib> 
#include <iomanip> 
 
using namespace std; 
 
const int NUMSELLER = 1; 
const int NUMBUYER = 1; 
const int NUMBIDS = 10; 
const int MINQUANTITY = 1; 
const int MAXQUANTITY = 30; 
const int MINPRICE =50; 
const int MAXPRICE = 100; 
 
class Bid { 
    int bidId; 
    int TrdId; 
    char Type; 
    int qty; 
    int price; 
public: 
    void generateBids(); 
    void printVector(const vector<int>& v);
}; 

//---------------------------------------------------------------------
void Bid :: generateBids() {
     
    srand (time(NULL) );  
    struct Bid bid1[21]; 
    int i ; 
    cout << "Generating bids and displaying them\n"; 
 
    for (i = 0; i < 20; i++) { 
        char buyer; 
        char a; 
        int x; 
        //====================Bid ID ====================== 
        x = (rand() % 9) + 1; 
        //x= reinterpret_cast<uint32_t>(&bid1);
        bid1[i].bidId = x; 
        //cout<<"\nThe bid ID is :  "<<i<<": "; 
        //=================TraderID========================== 
        //cin>>bid1[i].TrdId; 
        bid1[i].TrdId = (rand() % 9) + 1; 
        //=================Trader Type========================== 
        //cout<<"Dou you intend to Buy or Ask a bid?(B/A)"<<i<<": "; 
        a = (rand() % 2); 
 
        if (a == 0) { 
            a = 'B'; 
        } else a = 'A'; 
 
        bid1[i].Type = a; 
        //====================Quantity========================== 
        //cout << endl; 
        bid1[i].qty = (rand() % 30) + 1; 
        //cout << endl; 
        //======================price================================== 
        //Auto generate price. 
        //cout<<"The price of the Bid is :$ "<<i<<": "; 
        //cout << endl; 
        bid1[i].price = (rand() % 50) + 50; 
    } 
 
    cout << "Bid data successfully generated!\n\n"; 
    cout << "\n--------------Displaying your bid data---------------\n"; 
    cout << endl; 
    cout << endl; 
    // displaying the stored data 
    cout << "\t\tBidID | TradID | Type  | Qty  |  Price  \n\n"; 
 
    //cout<<"----------------------------------------------------------\n"; 
    for (i = 0; i < 20; i++) { 
        cout << "\tBid\t " <<  setw(3) << bid1[i].bidId << "\t " << setw(3) << bid1[i].TrdId << "\t " 
             << setw(3) << bid1[i].Type <<"\t " << setw(3) << bid1[i].qty <<"\t "  << setw(3) << bid1[i].price <<"\t\n "  ; 
    } 
 
    cout << endl; 
    cout << endl; 
    //cout<<"----------------------------------------------------------\n"; 
} 
// void printVector(const vector<int>& v){
//    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));}
//}
//------------------Test Program------------------------------------------------------------ 
int main() { 
    //srand ( time(NULL) ); 
    Bid bid1; 
    bid1.generateBids(); 
     
system ("pause"); 
    return 0; 
}



Is This A Good Question/Topic? 0

Replies To: changing structural data to a vector

#2 Guest_willson*


Reputation:

Re: changing structural data to a vector

Posted 01 May 2010 - 10:05 PM

Have tried to adjust my code using the vector container but things are not sticking up.I need some assistance in how to elements that have different types into a vector container and start manipulating them with alogorithm.
I expect the program to make a list of 10 buyers and 10 sellers like below: (most values are random)


1 10 20 30 A 15
2 10 20 30 A 15
3 10 20 30 A 15
4 10 20 30 A 15
5 10 20 30 A 15
6 10 20 30 B 15
7 10 20 30 B 15
8 10 20 30 B 15
9 10 20 30 B 15
10 10 20 30 B 15

#include <iostream>
#include <vector>
using namespace std;

const int NUMSELLER = 1;  
const int NUMBUYER = 1;  
const int NUMBIDS = 10;  
const int MINQUANTITY = 1;  
const int MAXQUANTITY = 30;  
const int MINPRICE =50;  
const int MAXPRICE = 100; 

class Bid {
  int oper;
  int bidId; 
  int TrdId; 
  char Type; 
  int qty; 
  int price;
public:
  Bid() {0,0,0,'',0;}
  //Bid(int x,int y,int z,char m,int n) { d = x; }
  Bid &operator =(int x)
  {
      oper = x; return *this;
  }
  int buyerBids(int,int,int,char,int );
  int sellerBids(int,int,int,char,int ); 
};
int Bid::buyerBids(int a, int b,int c, char d, int e)
{
       bidId = a; 
       TrdId = b; 
       Type = c; 
       qty = d; 
       price = e;
       
     a=(rand() % 9) + 1; 
     b=(rand() % 9) + 1;
     c= (rand() % 30) + 1; ; 
     d='B';
     e=(rand() % 50) + 50;

     return (a,b,c,d,e);
}
int Bid::sellerBids(int a, int b,int c, char d, int e)
{
       bidId = a; 
       TrdId = b; 
       Type = c; 
       qty = d; 
       price = e;
       
     a=(rand() % 9) + 1; 
     b=(rand() % 9) + 1;
     c= (rand() % 30) + 1; ; 
     d='A';
     e=(rand() % 50) + 50;

     return (a,b,c,d,e);
}
//-----------------------------------------------------------------

int main()
{
  vector<Bid> v;

  int i;
  char A,B;

  cout << endl;
  cout << endl;
  cout << "Bid data successfully generated!\n\n"; 
  cout << "\n--------------Displaying your bid data---------------\n"; 
  cout << endl; 
 
 // displaying the stored data 
 cout << "\tBidID | TradID | Type  | Qty  |  Price  \n\n"; 
  for (i = 0; i <v.size(); i++)
   {
     v.push_back(v.[i].buyerBids(1,2,B,3));
     v.push_back(v[ i ].sellerBids(0,0,0,A,0));
   }
   cout << endl;
   system("pause");
   return 0;
}

Was This Post Helpful? 0

#3 Guest_willison*


Reputation:

Re: changing structural data to a vector

Posted 02 May 2010 - 02:04 AM

This is where I am now, I have tried to make something very small. Ignoring the auto-generation of numbers, though I still need it.

How do I automatically generate the values and assign them in the set them, tried it but it didnt work for me



//created by William

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

// This class stores information on parts.
class Bid {
    int bidId; 
    int trdId; 
    int type; 
    int qty; 
    int price; 
    
public:

    Bid() { bidId = 0; trdId = 0; type = 0; qty = 0; price = 0; }// Default constructor.
    Bid(int n, int m, int o, int p ,int q) {  // Construct a complete Bid object.
        bidId = n; 
        trdId =m; 
        type =o; 
        qty =p; 
        price =q;
    }
    ~Bid(){}

int set_bidId(int a) { a = (rand() % 9) + 1;}
int set_trdId(int B)/> { b = (rand() % 9) + 1;}
int set_type(int c)  { c = (rand() % 9) + 1;}
int set_qty(int d)   { d = (rand() % 9) + 1;}
int set_price(int e) { e = (rand() % 50) + 50;}

// Accessor functions for Bid data.
int get_bidId() { return bidId; }
int get_trdId() { return trdId; }
int get_type()  { return type; }
int get_qty()   { return qty; }
int get_price() { return price; }
};

//---------------------Show bid details ----------------------------------------
void show(const char *msg, vector<Bid> vect);

bool operator<(Bid a, Bid B)/>
    {
    return a.get_bidId() < b.get_bidId();
    }

bool operator==(Bid a, Bid B)/>
    {
    return a.get_bidId() == b.get_bidId();
    }

int main()
{
vector<Bid> Bidlist;
// Initialize the Bids list.
    Bidlist.push_back(Bid(1,2,3,12,60));
    Bidlist.push_back(Bid(2,7,4,20,89));
    Bidlist.push_back(Bid(3,2,8,30,90));
    Bidlist.push_back(Bid(4,2,9,40,40));

// Display contents of the vector.
show("Bids that have been sorted:\n", Bidlist); // Use the sort() algorithm to sort the Bids list.
cout << endl;                                    // This requires that operator<() be defined for Bid.

sort(Bidlist.begin(), Bidlist.end());

show("Bids that have been matched:\n", Bidlist); // Use the find() algorithm to find a Bid given its number.
                                                  // This requires that operator==() be defined for Bid.

cout<< endl;
cout << "Searching for a particular Bid\n";
vector<Bid>::iterator itr;
itr = find(Bidlist.begin(), Bidlist.end(), Bid(4,2,9,40,40));
cout << "Bid has been found. Its : " << itr->get_bidId() << ".\n";

system("pause");
return 0;
}


// Display the contents of a vector<Bid>.
void show(const char *msg, vector<Bid> vect) {
vector<Bid>::iterator itr;
    cout << msg;
    cout <<"\t\tBidID | TradID | Type  | Qty  |  Price  \n\n"; 
    for(itr=vect.begin(); itr != vect.end(); ++itr)
    {
        cout <<"\t\t " << itr->get_bidId()<< "\t "<< itr->get_trdId() << "\t " << itr->get_type()<< "\t " 
        << itr->get_qty()<< "\t " << itr->get_price()<< "\n";
        cout << "\n";
    }
}



Was This Post Helpful? 0

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: changing structural data to a vector

Posted 02 May 2010 - 04:33 AM

Could you expand on what this
"automatically generate the values and assign them in the set them"
is intended to mean?

It isn't at all clear (at least to me) what you are asking for help with.

As well as that please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: changing structural data to a vector

Posted 02 May 2010 - 07:10 AM

Setters are setters, getters are getters... the random crap in the setter makes no sense. Even having them is at all isn't required for what you're doing.

For random, something like this should work:
Bid getRandomBid() {
	int bidId = (rand() % 9) + 1;
	int trdId = (rand() % 9) + 1;
	int type = (rand() % 9) + 1;
	int qty = (rand() % 9) + 1;
	int price = (rand() % 50) + 50;
	return Bid(bidId, trdId, type, qty, price);
}



However, don't you want bidId to be unique? Perhaps incremented for each instance? Here's how I'd do it:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Bid {
private:
	static int nextBidId;
	int bidId, trdId, type, qty, price; 

public:
	Bid() : bidId(0), trdId(0), type(0), qty(0), price(0) { }
	Bid(int n, int o, int p ,int q) : trdId(n), type(o), qty(p), price(q) {
		bidId = Bid::nextBidId++;
	}

	int get_bidId() const { return bidId; }
	int get_trdId() const { return trdId; }
	int get_type()  const { return type; }
	int get_qty()   const { return qty; }
	int get_price() const { return price; }
	
	// for sort on price.  It's already going to be in bidId order.
	bool operator<(const Bid &other) const { return price < other.price; }
	bool operator==(const Bid &other) const { return bidId == other.bidId; }
	bool operator==(int bidId) const { return this->bidId == bidId; }
	void show(ostream &out) const {
		out  << bidId << '\t' << trdId << '\t' << type << '\t' << qty << '\t' << price;
	}
};

int Bid::nextBidId = 1;

Bid getRandomBid() {
	int trdId = (rand() % 9) + 1;
	int type = (rand() % 9) + 1;
	int qty = (rand() % 9) + 1;
	int price = (rand() % 50) + 50;
	return Bid(trdId, type, qty, price);
}

void initList(vector<Bid> &list) {
	list.push_back(Bid(2,3,12,60));
	list.push_back(Bid(7,4,20,89));
	list.push_back(getRandomBid());
	list.push_back(getRandomBid());
	list.push_back(getRandomBid());
	list.push_back(getRandomBid());
}


void show(const char *msg, const vector<Bid> &vect) {
	cout << msg << endl;
	cout <<"\t\tBidID | TradID | Type  | Qty  |  Price  \n\n"; 
	for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) {
		cout <<"\t\t ";
		itr->show(cout);
		cout << endl;
	}
	cout << endl;
}

void searchTest(vector<Bid> &list, int bidId) {
	cout << "Searching for a particular Bid\n";
	vector<Bid>::iterator itr = find(list.begin(), list.end(), bidId);
	cout << "Bid has been found. Its : ";
	itr->show(cout);
	cout << endl;
}


int main() {
	vector<Bid> bidlist;
	initList(bidlist);

	show("Bids before sort:", bidlist);
	
	sort(bidlist.begin(), bidlist.end());
	show("Bids after sort:", bidlist);
	searchTest(bidlist, 3);

	return 0;
}




Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#6 wmugume2000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 130
  • Joined: 18-March 10

Re: changing structural data to a vector

Posted 02 May 2010 - 11:07 AM

It did really help, thanks so much!
Was This Post Helpful? 0
  • +
  • -

#7 Guest_willson*


Reputation:

Re: changing structural data to a vector

Posted 02 May 2010 - 11:26 AM

How can include a character within the bid? I want each bid to show wether its a buyer, or seller in the out put.


char type = B;

am afraid this will bring up only buyers.
I want to have the fist 10 bids for buyers and the next next 10 for sellers labbled A.

eg.
1 10 20 30 A 7
2 15 11 12 A 84
3 10 20 16 A 14
4 54 38 30 A 41
5 10 22 30 A 25
6 10 15 30 B 4
7 14 11 30 B 15
8 11 47 30 B 46
9 77 20 30 B 48
10 44 20 30 B 44

Am giving it a try too!
Was This Post Helpful? 0

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: changing structural data to a vector

Posted 02 May 2010 - 11:50 AM

Just add another field. If you only have buyers and sellers, then I'd use a bool.

e.g.

class Bid {
private:
	static int nextBidId;
	int bidId, trdId, type, qty, price; 
	bool isBuyer;

public:
	Bid() : bidId(0), trdId(0), type(0), qty(0), price(0), isBuyer(false) { }
	Bid(int n, int o, int p ,int q, bool B)/> : trdId(n), type(o), qty(p), price(q), isBuyer(B)/> {



The nature of the data structure is up to you. If having an auto counter is too restrictive, do something different. If buyers and sellers have slightly differing data, make two classes and have them share a base class. Or keep two vectors.

You need to have a solid idea of what you're trying to do. There are innumerable solutions for any given problem; go with what you feel makes the most sense.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_willlison*


Reputation:

Re: changing structural data to a vector

Posted 02 May 2010 - 02:18 PM

Is it possible for me to have a character withing the data.so that I dont auto-generate for the type.But still leave the bool(isBuyer).

Aparently I have a class Trader, seller and buyer inherit from it.
But the bids are to be generated by the Similator class and I pass them to the auctioneer.
Well here am consetrating on the generation of the bids.If I could generate every bid showing type as the appropriate trader.

eg - setting isButer to true in the buyer class which inherits from Trader
Bid() : bidId(0), trdId(0), type('B'), qty(0), price(0), isBuyer(true) { } 


and
Bid() : bidId(0), trdId(0), type('A'), qty(0), price(0), isSeller(true) { } 


my worry is that! wont I need to introduce another iterator to handle that character?
Was This Post Helpful? 0

#10 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: changing structural data to a vector

Posted 02 May 2010 - 04:38 PM

View Postwilllison, on 02 May 2010 - 03:18 PM, said:

my worry is that! wont I need to introduce another iterator to handle that character?



Maybe. I mean, it's not a huge deal:
void showForType(const vector<Bid> &vect, char type) {
	for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) {
		if (itr->getType()==type) {
			// do something
		}
	}
}



Don't put too much stress on Bid. It's just a collection of information. It could just be a bare struct if you wanted.

It needn't even have a constructor if your Simulator class is in charge of spitting them out.
e.g.
struct Bid {
	int bidId, trdId, qty, price; 
	char type;
};

class Simulator {
private:
	int currentBid;
public:
	Similator();
	Bid getNextBid();
};


Was This Post Helpful? 0
  • +
  • -

#11 Guest_willson*


Reputation:

Re: changing structural data to a vector

Posted 02 May 2010 - 05:19 PM

I am still stuck bringing to life.Am wondering how ( int Similator::nextBidId = 1;), doesnt seem to handle my type.

ERROR: sample1.cpp:25 candidates are: Similator::Similator(const Similator&)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib> 

using namespace std;


const int NUMSELLER = 1;
const int NUMBUYER = 1;
const int NUMBIDS = 10;
const int MINQUANTITY = 1;
const int MAXQUANTITY = 30;
const int MINPRICE =50;
const int MAXPRICE = 100;

struct Bid { 
        int bidId, trdId, qty, price;  
        char type; 
}; 

class Similator { 

private:
        static int nextBidId;
        int bidId, trdId,qty, price; 
        char type;
        int currentBid; 
public: 
        Similator(); 
        Bid getNextBid(); 

        int get_bidId() const { return bidId; } 
        int get_trdId() const { return trdId; } 
        int get_type()  const { return type; } 
        int get_qty()   const { return qty; } 
        int get_price() const { return price; } 
         
        // for sort on price.  It's already going to be in bidId order. 
        bool operator<(const Bid &other) const { return price < other.price; } 
        bool operator==(const Bid &other) const { return bidId == other.bidId; } 
        bool operator==(int bidId) const { return this->bidId == bidId; } 
        void show(ostream &out) const { 
                out  << bidId << '\t' << trdId << '\t' << type << '\t' << qty << '\t' << price; 
        } 
}; 
 
int Similator::nextBidId = 1; 
 
Similator getRandomBid() { 
        int trdId = (rand() % 9) + 1; 
        int type = (rand() % 2); 
            if (type == 0) { 
                type = 'B'; 
            } else type = 'A'; 
        int qty = (rand() % 9) + 1; 
        int price = (rand() % 50) + 50; 
        return Similator(trdId, 'type', qty, price); 
} 

void showForType(const vector<Bid> &vect, char type) { 
        for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
                //if (itr->getType()==type) { 
                cout <<"\t\t ( "; 
                itr->show(cout); 
                cout <<" )"; 
                cout << endl; 
                
        } 
} 

void initList(vector<Bid> &list) { 
 //10 sellers on top then 10 buyers next
 for (int i=0;i<20; i++)
     {
            list.push_back(getRandomBid()); 
     }
} 
 
void show(const char *msg, const vector<Bid> &vect) { 
        cout << msg << endl; 
        cout <<"\t\tBidID | TradID | Type  | Qty  |  Price  \n\n";  
        for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
                cout <<"\t\t ( "; 
                itr->show(cout); 
                cout <<" )"; 
                cout << endl; 
        } 
        cout << endl; 
} 
 
void searchTest(vector<Bid> &list, int bidId) { 
        cout << "Searching for a particular Bid\n"; 
        vector<Bid>::iterator itr = find(list.begin(), list.end(), bidId); 
        cout << "Bid has been found. Its : "; 
        itr->show(cout); 
        cout << endl; 
} 
int main() { 
        vector<Bid> bidlist; 
        initList(bidlist); 
 
        show("Bids before sort:", bidlist); 
         
        sort(bidlist.begin(), bidlist.end()); 
        show("Bids after sort:", bidlist); 
        searchTest(bidlist, 3); 
 
 system("pause");
        return 0; 
}




Was This Post Helpful? 0

#12 Guest_willson*


Reputation:

Re: changing structural data to a vector

Posted 02 May 2010 - 05:22 PM

sorry i meant this function: error reported is about a definition of (int,int,int,int)

but I have (int,char,int,int)

[code]
Similator getRandomBid() {
int trdId = (rand() % 9) + 1;
int type = (rand() % 2);
if (type == 0) {
type = 'B';
} else type = 'A';
int qty = (rand() % 9) + 1;
int price = (rand() % 50) + 50;
return Similator(trdId, 'type', qty, price);
}
[/]
Was This Post Helpful? 0

#13 Guest_willson*


Reputation:

Re: changing structural data to a vector

Posted 02 May 2010 - 05:58 PM

Tried fixing the function call issue, another problem is about the show.how do I make the strut be in it!

Error: 68 'const struct Bid' has no member named 'show'

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib> 

using namespace std;


const int NUMSELLER = 1;
const int NUMBUYER = 1;
const int NUMBIDS = 10;
const int MINQUANTITY = 1;
const int MAXQUANTITY = 30;
const int MINPRICE =50;
const int MAXPRICE = 100;

struct Bid { 
        int bidId, trdId, qty, price;  
        char type; 
}; 

class Similator { 

private:
        static int nextBidId;
        int bidId, trdId,qty, price; 
        char type;
        int currentBid; 
public: 
        Similator();
        Similator(int n, char o, int p ,int q) : trdId(n), type(o), qty(p), price(q){bidId = Similator::nextBidId++;}
        Similator( getNextBid()); 

        int get_bidId() const { return bidId; } 
        int get_trdId() const { return trdId; } 
        int get_type()  const { return type; } 
        int get_qty()   const { return qty; } 
        int get_price() const { return price; } 
         
        // for sort on price.  It's already going to be in bidId order. 
        bool operator<(const Bid &other) const { return price < other.price; } 
        bool operator==(const Bid &other) const { return bidId == other.bidId; } 
        bool operator==(int bidId) const { return this->bidId == bidId; } 
        void show(ostream &out) const { 
                out  << bidId << '\t' << trdId << '\t' << type << '\t' << qty << '\t' << price; 
        } 
}; 
 
int Similator::nextBidId = 1; 
 
Similator getRandomBid() { 
        int trdId = (rand() % 9) + 1; 
        int type = (rand() % 2); 
            if (type == 0) { 
                type = 'B'; } else type = 'A'; 
        int qty = (rand() % 9) + 1; 
        int price = (rand() % 50) + 50; 
        return Similator(trdId, type, qty, price); 
} 

void showForType(const vector<Bid> &vect, char type) { 
        for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
                //if (itr->getType()==type) { 
                cout <<"\t\t ( "; 
              [b]  itr->show(cout); [/b]                cout <<" )"; 
                cout << endl; 
                
        } 
} 

void initList(vector<Bid> &list) { 
 //10 sellers on top then 10 buyers next
 for (int i=0;i<20; i++)
     {
            list.push_back(getRandomBid()); 
     }
} 
 
void show(const char *msg, const vector<Bid> &vect) { 
        cout << msg << endl; 
        cout <<"\t\tBidID | TradID | Type  | Qty  |  Price  \n\n";  
        for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
                cout <<"\t\t ( "; 
                itr->show(cout); 
                cout <<" )"; 
                cout << endl; 
        } 
        cout << endl; 
} 
 
void searchTest(vector<Bid> &list, int bidId) { 
        cout << "Searching for a particular Bid\n"; 
        vector<Bid>::iterator itr = find(list.begin(), list.end(), bidId); 
        cout << "Bid has been found. Its : "; 
        itr->show(cout); 
        cout << endl; 
} 
int main() { 
        vector<Bid> bidlist; 
        initList(bidlist); 
 
        show("Bids before sort:", bidlist); 
         
        sort(bidlist.begin(), bidlist.end()); 
        show("Bids after sort:", bidlist); 
        searchTest(bidlist, 3); 
 
 system("pause");
        return 0; 
}





Was This Post Helpful? 0

#14 Guest_willson*


Reputation:

Re: changing structural data to a vector

Posted 02 May 2010 - 06:16 PM

Somthing am probably messing up.Compiler loaded a new file pointing out this error

ERROR: 2472 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h instantiated from `void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Similator*, std::vector<Similator, std::allocator<Similator> > >, _Size = int]'

But I have no idea about it- only to guess that my iterator is doing a good job, how do I fix it.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib> 

using namespace std;


const int NUMSELLER = 1;
const int NUMBUYER = 1;
const int NUMBIDS = 10;
const int MINQUANTITY = 1;
const int MAXQUANTITY = 30;
const int MINPRICE =50;
const int MAXPRICE = 100;

struct Bid { 
        int bidId, trdId, qty, price;  
        char type; 
}; 

class Similator { 

private:
        static int nextBidId;
        int bidId, trdId,qty, price; 
        char type;
        int currentBid; 
public: 
        Similator();
        Similator(int n, char o, int p ,int q) : trdId(n), type(o), qty(p), price(q){bidId = Similator::nextBidId++;}
        Similator( getNextBid()); 

        int get_bidId() const { return bidId; } 
        int get_trdId() const { return trdId; } 
        int get_type()  const { return type; } 
        int get_qty()   const { return qty; } 
        int get_price() const { return price; } 
         
        // for sort on price.  It's already going to be in bidId order. 
        bool operator<(const Bid &other) const { return price < other.price; } 
        bool operator==(const Bid &other) const { return bidId == other.bidId; } 
        bool operator==(int bidId) const { return this->bidId == bidId; } 
        void show(ostream &out) const { 
                out  << bidId << '\t' << trdId << '\t' << type << '\t' << qty << '\t' << price; 
        } 
}; 
 
int Similator::nextBidId = 1; 
 
Similator getRandomBid() { 
        int trdId = (rand() % 9) + 1; 
        int type = (rand() % 2); 
            if (type == 0) { 
                type = 'B'; } else type = 'A'; 
        int qty = (rand() % 9) + 1; 
        int price = (rand() % 50) + 50; 
        return Similator(trdId, type, qty, price); 
} 

void showForType(const vector<Similator> &vect, char type) { 
        for(vector<Similator>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
                //if (itr->getType()==type) { 
                cout <<"\t\t ( "; 
                itr->show(cout); 
                cout <<" )"; 
                cout << endl; 
                
        } 
} 

void initList(vector<Similator> &list) { 
 //10 sellers on top then 10 buyers next
 for (int i=0;i<20; i++)
     {
            list.push_back(getRandomBid()); 
     }
} 
 
void show(const char *msg, const vector<Similator> &vect) { 
        cout << msg << endl; 
        cout <<"\t\tBidID | TradID | Type  | Qty  |  Price  \n\n";  
        for(vector<Similator>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
                cout <<"\t\t ( "; 
                itr->show(cout); 
                cout <<" )"; 
                cout << endl; 
        } 
        cout << endl; 
} 
 
void searchTest(vector<Similator> &list, int bidId) { 
        cout << "Searching for a particular Bid\n"; 
        vector<Similator>::iterator itr = find(list.begin(), list.end(), bidId); 
        cout << "Bid has been found. Its : "; 
        itr->show(cout); 
        cout << endl; 
} 
int main() { 
        vector<Similator> bidlist; 
        initList(bidlist); 
 
        show("Bids before sort:", bidlist); 
         
        sort(bidlist.begin(), bidlist.end()); 
        show("Bids after sort:", bidlist); 
        searchTest(bidlist, 3); 
 
 system("pause");
        return 0; 
}




Was This Post Helpful? 0

#15 Guest_willson*


Reputation:

Re: changing structural data to a vector

Posted 03 May 2010 - 01:08 AM

How can display a character within the elements of the container?So that I can have something like this:
The code below works fine but listing only integers.

12B45
56B78
45B87
12A68
12A58
78A36


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib> 

using namespace std;


const int NUMSELLER = 1;
const int NUMBUYER = 1;
const int NUMBIDS = 10;
const int MINQUANTITY = 1;
const int MAXQUANTITY = 30;
const int MINPRICE =50;
const int MAXPRICE = 100;

struct Bid { 
        int bidId, trdId, qty, price;  
        int type; 
}; 

class Simulator { 

private: 
        static int nextBidId;
        int bidId, trdId,qty, price; 
        int type;
        int currentBid; 
public: 
       Simulator(){};
       Simulator(int n, char o, int p ,int q) : trdId(n), type(o), qty(p), price(q){bidId =Simulator::nextBidId++;}
       Simulator( getNextBid()); 

        int get_bidId() const { return bidId; } 
        int get_trdId() const { return trdId; } 
        int get_type()  const { return type; } 
        int get_qty()   const { return qty; } 
        int get_price() const { return price;} 
         
        // for sort on price.  It's already going to be in bidId order. 
        bool operator<(const Bid &other) const { return price < other.price; } 
        bool operator==(const Bid &other) const { return bidId == other.bidId; } 
        bool operator==(int bidId) const { return this->bidId == bidId; } 
        void show(ostream &out) const { 
                out  << bidId << '\t' << trdId << '\t' << type << '\t' << qty << '\t' << price; 
        } 
}; 
 
int Simulator::nextBidId = 1; 
 
Simulator getRandomBid() { 
        int trdId = (rand() % 9) + 1; 
        int type = (rand() % 9) + 1; 
        int qty = (rand() % 9) + 1; 
        int price = (rand() % 50) + 50; 
        return Simulator(trdId, type, qty, price); 
} 

//void showForType(const vector<Bid> &vect, char type) { 
//        for(vector<Bid>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
//                if (itr->getType()== type) { 
//                        // do something 
//                } 
//        } 
//} 

void initList(vector<Simulator> &list) { 
 for (int i=0;i<20; i++)
     {
            list.push_back(getRandomBid()); 
     }
} 
 
void show(const char *msg, const vector<Simulator> &vect) { 
        cout << msg << endl; 
        cout <<"\t\tBidID | TradID | Type  | Qty  |  Price  \n\n";  
        for(vector<Simulator>::const_iterator itr=vect.begin(); itr != vect.end(); ++itr) { 
                cout <<"\t\t ( "; 
                itr->show(cout); 
                cout <<" )"; 
                cout << endl; 
        } 
        cout << endl; 
} 
 
void searchTest(vector<Simulator> &list, int bidId) { 
        cout << "Searching for a particular Bid\n"; 
        vector<Simulator>::iterator itr = find(list.begin(), list.end(), bidId); 
        cout << "Bid has been found. Its : "; 
        itr->show(cout); 
        cout << endl; 
} 
int main() { 
        vector<Similator> bidlist; 
        initList(bidlist); 
 
        show("Bids before sort:", bidlist); 
         
        sort(bidlist.begin(), bidlist.end()); 
        show("Bids after sort:", bidlist); 
        searchTest(bidlist, 3); 
 
 system("pause");
        return 0; 
}


Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2