开发者

access a member of class using vector

I have a class defined as

typedef std::string Name;
typedef int Age;
typedef std::string Level;

class Employee
{
public:
    // Employee (arguments);
    //  virtual ~Employee ();
    void name(Name const & name) { name_ = name; }
    Name name() const { return name_; }

    void age(Age const & age) { age_ = age; }
    Age age() const { return age_; }

    void level(Level const & level) { level_ = level; }
    Level level() const { return level_; }

private:
    Name name_;
    Age age_;
    Level level_;
};

std::vector<Employee> read_file(std::string filename);

std::vector<Employee> employees = read_file("data.txt");
std::cout << employees.size() << std:: endl;

for(std::vector<Employee>::iterator it = employees.begin(); it != employees.end(); ++it)
{
    std::cout << *it << std::endl;  
}

Is there a way for me to access the members of the class Employee using this vector defined above? I want to construct a map contain开发者_开发百科er with level of the employee as the key value.


If you want to access members of Employee from the iterator you can use the member access operator ->:

the_map[it->level()] = blah;


Unless I'm misinterpreting your question, this is simple enough.

  • it is an iterator over the vector of Employee.
  • *it is the Employee
  • it-> lets you access members of that Employee
    • e.g.: it->name()

So, if you want to build a map of employee level to Employee, you can do this:

std::map<Level, Employee> employeeMap;
for(std::vector<Employee>::iterator it = employees.begin();
    it != employees.end();
    ++it)
{
  employeeMap[ it->level() ] = *it;
}

Now you have your employeeMap, which maps the employee's Level to the Employee


 const Name name   = it->name();
 const Age age     = it->age();
 const Level level = it->level(); 

or

 const Name name   = employees[i].name();
 const Age age     = employees[i].age();
 const Level level = employees[i].level();

will work fine.

I would, however, strongly recommend you return each of the above items as references as it will generally be a lot faster than making a copy of the object.

ie

class Employee
{
public:
    // Employee (arguments);
    //  virtual ~Employee ();
    void name(Name const & name) { name_ = name; }
    Name& name() { return name_; }
    const Name& name() const { return name_; }

    void age(Age const & age) { age_ = age; }
    Age& age() { return age_; }
    const Age& age() const { return age_; }

    void level(Level const & level) { level_ = level; }
    Level& level() { return level_; }
    const Level& level() const { return level_; }
private:
    Name name_;
    Age age_;
    Level level_;
};  

Then you can access the values by reference as follows:

 const Name& name   = it->name();
 const Age& age     = it->age();
 const Level& level = it->level();

It also means you can change the values like this:

 it->name()  = "Goz";
 it->age()   = 33;
 it->level() = "Programmer";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜