Smart Accessor Function for Multiple Data Containers?

This is a discussion on Smart Accessor Function for Multiple Data Containers? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I知 Pete, moderately-experienced C++ user who is always looking for better ways to do things. Let me ask ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Smart Accessor Function for Multiple Data Containers?

    Hi everyone,

    I知 Pete, moderately-experienced C++ user who is always looking for better ways to do things.

    Let me ask you guys about a problem I致e scratching my head over lately. Suppose I知 writing a program designed to simulate a large company. I知 interested in tracking each company employee by the location where they work. This company has perhaps a thousand different locations:

    Code:
    class Employee {
      public:
        AccessorFunction1();   // does something
        AccessorFunction2();   // does something different
        AccessorFunction3();   // does something completely different
      protected:
        // Some data
    };
    
    
    class Company {
      public:
        void OrganizeLocation(int a);
    
      protected:
        vector<Employee*> LocationA;
        vector<Employee*> LocationB;
        vector<Employee*> LocationC;
        ...etc...
        vector<Employee*> LocationZZZ;
      };
    Once employees are created and pointers to them are saved in the proper Location vector, I write an accessor function, OrganizeLocation(), designed to do a number of operations on a given vector. The problem is, I have maybe a thousand vectors. How do I call this function and specify which vector I want?

    Currently, I知 using this clunky solution:

    Code:
    void Company::OrganizeLocation(int a){
      switch(a) {
        case 1: {
            for(unsigned int i=0; i<LocationA.size(); i++) {
              LocationA[i]->AccessorFunction1();
              LocationA[i]->AccessorFunction2();
              LocationA[i]->AccessorFunction3();
            }
            break;
          }
        case 2: {
            for(unsigned int i=0; i<LocationB.size(); i++) {
              LocationB[i]->AccessorFunction1();
              LocationB[i]->AccessorFunction2();
              LocationB[i]->AccessorFunction3();
            }
            break;
          }
        case 3: {
            // etc...
          }
        case 4: {
            // etc...
          }
    
        ...etc...
    
        case 1000: {
            // etc...
          }
      }
    }
    The key point here is that whichever vector I choose to operate upon, I値l do the exact same procedure every time. I hate this solution because it results in very long and repetitive code not to mention its very error-prone when you re-editing all those 鏑ocationA敗 into 鏑ocationB/C/D/etc.

    What would be an ideal solution would be if I could do some kind of string substitution into the vector name like (I think) you can do in PERL scripts:

    Code:
    void Company::OrganizeLocation( string $WhichOne$ ){
      for(unsigned int i=0; i<LocationA.size(); i++) {
        Location$WhichOne$[i]->AccessorFunction1();
        Location$WhichOne$[i]->AccessorFunction2();
        Location$WhichOne$[i]->AccessorFunction3();
    }
    Does anyone know of a way to do this? Or, if it can稚 be done in C++, is there a better design approach? Ultimately I need the Company object to hold multiple vectors but use one compact accessor function to perform operations on just one of them.

    Many thanks!
    -Pete

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Just put the vector<employee*> `s into a vector(thus, making a vector<vector<employee*>> as the only data member), and you can get the exact vector by the index from the argument passed in OrganizeLocation.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14
    Well done, all! Good advice...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Map with multiple data types?
    By LuckyPierre in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2009, 08:28 PM
  2. Add multiple data in a std::set using +=operator
    By IndioDoido in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2007, 11:19 AM
  3. Accessor Function
    By Suchy in forum C++ Programming
    Replies: 5
    Last Post: 10-11-2006, 11:44 PM
  4. File Input and mutator/accessor function help
    By Troll in forum C++ Programming
    Replies: 6
    Last Post: 10-31-2005, 01:18 PM
  5. accessor function return type ptr to map
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2003, 02:50 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21