Comparing values of two maps.

Trying to compare the prices of the bids in the two maps.Sellers map and the buyers map. Conditions for comparison.

1) One bid is A and the other is B

2) Price of B i greater than price of A.

I then copy the matched bids to a container called matched.I am not sure how to do the match of maps

though I tried with this wchich definitely doesnt work.
[code]

void compareBidList(bidtable one, bidtable two) {
for(iter=bidtable.begin(); iter != bidtable.end(); iter++
{
if (bidtable[]->bidType == 'A' && bidtable[]->bidType == 'B')
if (bidtable[]->price < bidtable[]->price)

//I want to copy the bids that matched the condition to a container matched

}
[code]





can you fix your post and make it easier to read for people who are trying to help you!
The code is a sbelow:

1
2
3
4
5
6
7
8
9
compareBidList(bidtable one, bidtable two) {  
                for(iter=bidtable.begin(); iter != bidtable.end(); iter++ 
                {  
                      if (bidtable[]->bidType == 'A' && bidtable[]->bidType  == 'B')
                       if (bidtable[]->price < bidtable[]->price) 
                    
                    //I want to copy the bids that matched the condition to a container matched
                      Sorted.insert(int, Bid*>::value_type(5, 7));
               }
Here is one way you might compare all your bids:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void compareBidList(const bidtable& one, const bidtable& two)
{
	bidtable::iterator iterOne;

	for(iterOne = one.begin(); iterOne != one.end(); ++iterOne) 
	{
		if(iterOne->bidType == 'A') // select all type A from one
		{
			bidtable::iterator iterTwo;
			for(iterTwo = two.begin(); iterTwo != two.end(); ++iterTwo) 
			{
				if(iterTwo->bidType == 'B') // select all type B from two
				{
					if(iterOne->price < iterTwo->price) // select on price between type A and type B
					{
						// insert *iterOne and *iterTwo into your container
					}
				}
			}
		}
	}
}
Last edited on
Thanks, I had tried it this way after encoutering a number of errors, but still not working fine!

These are the members I have for this class
1
2
3
4
5
6
7
Auctioneer(){};
            ~Auctioneer(){};
            void printTable(map<int, Bid*>);
            map<int, Bid*> bidtable;
            typedef Bid* iter;
            typedef pair<int, Bid*> Pair;
            Bid compareBidList(map<int, Bid*> one, map<int, Bid*> two);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Bid Auctioneer::compareBidList(map<int, Bid*> one, map<int, Bid*> two)
{
	map<int, Bid*> Sorted;
    map<int, Bid*>::iterator iterOne;

	for(iterOne = one.begin(); iterOne != one.end(); ++iterOne) 
	{
		if(bidtable[0]->bidType == 'A') // select all type A from one
		{
			map<int, Bid*>::iterator iterTwo;
			for(iterTwo = two.begin(); iterTwo != two.end(); ++iterTwo) 
			{
				if(bidtable[0]->bidType == 'B') // select all type B from two
				{
					if(bidtable[0]->price < bidtable[0]->price) // select on price between type A and type B
					{
					Sorted[iterOne->bidtable[0]->bidId] = iterOne;
			        Sorted[iterTwo->bidtable[0]->bidId] = iterTwo;

					}
				}
			}
		}
	}
}
for(iter=Sorted.begin(); iter != Sorted.end(); iter++){
cout << iter->second->toString() << endl<<"\n";}
Last edited on
Here is the code with a few errors corrected.

To be honest you need to read up more on using map and iterators.

It is possible that you have bitten off a little more than you can chew with this program... ;o)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Bid Auctioneer::compareBidList(map<int, Bid*> one, map<int, Bid*> two) why return a Bid?
map<int, Bid*> Auctioneer::compareBidList(map<int, Bid*>& one, map<int, Bid*>& two) // pass references &
{
	map<int, Bid*> Sorted;
    map<int, Bid*>::iterator iterOne;
	for(iterOne = one.begin(); iterOne != one.end(); ++iterOne)
	{
		if(iterOne->second->bidType == 'A') // select all type A from one
		{
			map<int, Bid*>::iterator iterTwo;
			for(iterTwo = two.begin(); iterTwo != two.end(); ++iterTwo)
			{
				if(iterTwo->second->bidType == 'B') // select all type B from two
				{
					if(iterOne->second->price < iterTwo->second->price) // select on price between type A and type B
					{
						Sorted.insert(*iterOne);
						Sorted.insert(*iterTwo);
					}
				}
			}
		}
	}
	return Sorted;
}
You are very right Galik (181) , am still having trouble with the maps.But doing all I can to understand them.

Thanks for the assistance!
The trick with a map is that it is a container of pairs.

So when you iterate through a map, each element is a pair.

A pair has two member variable, one for each side of the mapping.

So if I have a map if int to Bid* like you have in std::map<int, Bid*> then when I iterate through it with my iterator iterOne then it wall always point to a pair:

And the pair object holds both the int and the Bid*. If I want the first part of the mapping (the int) then I must say iterOne->first which gives me the int and if I want the second part of the mapping I must use iterOne->second which gives me the Bid*.
I m trying to output the sorted bidlist.

These are the bids for the sellers
1
2
3
4
5
6
7
8
cout<<"These are the Sellers bids\n\n";
                 map<int, Bid*> sellers(bidtable);
                 sellers.erase(10);sellers.erase(11);sellers.erase(12);sellers.erase(13);sellers.erase(14);
                 sellers.erase(15);sellers.erase(16); sellers.erase(17);sellers.erase(18);sellers.erase(19);

                 for(iter=sellers.begin(); iter != sellers.end(); iter++)
                 cout << iter->second->toString() << endl<<"\n";
 


These are the bids for the buyers:

1
2
3
4
5
6
7
cout<<"These are the Buyers bids\n\n";
                 map<int, Bid*> buyers(bidtable);         buyers.erase(0);buyers.erase(1);buyers.erase(2);buyers.erase(3);buyers.erase(4);buyers.erase(5);
                 buyers.erase(6);buyers.erase(7); buyers.erase(8);buyers.erase(9);
                 for(iter=buyers.begin(); iter != buyers.end(); iter++){ 
                 cout << iter->second->toString() << endl<<"\n";
                 //cout << buyers.size() << endl<<"\n";
                 }



Tried doing the sort in the main as below:

auctioneer.compareBidList(sellers,buyers);
I am trying to use sho to show the output of another function.The first function was used to do sorting, and returned a List.

now I want to make a function that uses show() to display the output.This is how I had tried it only to get an error.

Its to diplay the results of two the two sorted lists which used this function. map Auctioneer::compareBidList(map& one, map& two) and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 //**return Sorted.** 
 
 
 void show(const char *msg, map<int, Bid*>& Sorted) {   
       cout << msg << endl;  
       show(Sorted); 
        }   
void compare(map<int, Bid*>& sellers, map<int, Bid*>& buyers) {  
    compare(sellers.begin(), sellers.end(), 
    buyers.begin(),buyers.end(),compareBidList); }  
 
 //my call in the main after declaration was as follows 
  map<int, Bid*> buyers, sellers; 
  Auctioneer auctioneer; 
  auctioneer.compare(sellers,buyers); 
  show(("Bids after sorting:", sellers,buyers);) 

Last edited on
I honestly think you need to start with something much simpler than this.

I don't know why you are erasing all the elements before you try to display them.

Really you need to learn the very basics before you try to tackle something as complex as this.
the honest fact is that this is and assignment due today!I done the all thing in vectors , only to find that ,I wasnt moving good.Switched to maps..........just cant walk off now.Aleast after today, I will get to the basics again.
Well the trouble is calling the function to display in the main.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 //Function that does the compare! 
 
    map<int, Bid*> Auctioneer::compareBidList(map<int, Bid*>& one, map<int, Bid*>&  
                   two) // pass references & 
     { 
  map<int, Bid*> Sorted; 
       map<int, Bid*>::iterator iterOne; 
  for(iterOne = one.begin(); iterOne != one.end(); ++iterOne) 
 { 
    if(iterOne->second->bidType == 'A') // select all type A from one 
    { 
        map<int, Bid*>::iterator iterTwo; 
        for(iterTwo = two.begin(); iterTwo != two.end(); ++iterTwo) 
        { 
            if(iterTwo->second->bidType == 'B') // select all 
         type B from two 
            { 
                if(iterOne->second->price < iterTwo->second- 
                          >price) // select on price between type A and type B 
                { 
                    Sorted.insert(*iterOne); 
                    Sorted.insert(*iterTwo); 
                } 
            } 
        } 
    } 
   }                 
   return Sorted; 
   } 
 
map<int, Bid*> Auctioneer::show(map<int, Bid*>& one, map<int, Bid*>& two) { 
     
     map<int, Bid*> Sorted;
     map<int, Bid*>::iterator iterOne;
     map<int, Bid*>::iterator iterTwo;
     
cout << "-----------------The sorted List-------------------------";     
    for(iterOne=Sorted.begin(); iterOne!= Sorted.end(); iterOne++){
    cout << iterOne->second->toString() << endl<<"\n";}
    for(iterTwo=Sorted.begin(); iterTwo!= Sorted.end(); iterTwo){
    cout << iterTwo->second->toString() << endl<<"\n";}  
}  
 
//my call in the main after declaration was as follows 
   map<int, Bid*> buyers, sellers; 
   auctioneer.compare(sellers,buyers); 
   show("Bids after sorting:", sellers);  
   show(buyers);  
Last edited on
Topic archived. No new replies allowed.