Use transform to fill a map from a vector of user-defined object : transform « Map Multimap « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Map Multimap » transformScreenshots 
Use transform to fill a map from a vector of user-defined object
    
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class PC{
   public:
   enum PC_type Dell, HP, IBM, Compaq };

   PCPC_type appliance = Dell, int model = 220,int serial = );

   bool operator<const PC& rhs const;

   PC_type appliance() const;
   int model() const;
   string name() const;

   void print() const;

   int serial() const;

   private:
   PC_type appliance_;
   int model_;
   int serial_;

};

inline
PC::PCPC::PC_type appliance, int model,int serial )
   : appliance_appliance ), model_model ), serial_serial )
{} 

inline
bool PC::operator<const PC& rhs const
return appliance() < rhs.appliance() ||
   appliance() == rhs.appliance() && model() < rhs.model() );
}

inline
PC::PC_type PC::appliance() const
return appliance_; }

inline
int PC::model() const
return model_; }

string PC::name() const
{
   string what;
   switchappliance() )
   {
      case Dell:         what = "Dell";              break;
      case HP:           what = "HP";                break;
      case IBM:          what = "IBM";               break;
      case Compaq:       what = "Compaq";            break;
      default:           what = "Unknown appliance"break;
   }
   return what;
}

inline
void PC::print() const
{
   char oldfill = cout.fill();
   cout << name() << " - Model "
        << model() << ", Serial number " 
        << serial() << endl;
}

inline
int PC::serial() const
return serial_; }

bool greater_modelconst pair<PC::PC_type,PC> p,int min_model );

int main( )
{
   const PC::PC_type kind[] PC::IBM,PC::Dell, PC::HP};
   int num_appliances = 3, serial=0;
   vector<PC> v;
   forint i = 0; i < 3; ++i )
      v.push_backPCkind[i], i, i ) );

   map<int,PC> sold;
    
   transformkind, kind+num_appliances, v.begin(),insertersold, sold.end() ),make_pair<int,PC> );
   map<int,PC>::const_iterator sold_end = sold.end();

   map<int,PC>::const_iterator site;
   forsite = sold.begin(); site != sold_end; ++site )
      site->second.print();

}
  
    
    
    
  
Related examples in the same category
1.Transform vector of user-defined objects to a multi-map
2.use transform() to create map from two arrays
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.