简体   繁体   English

使用vector访问类的成员

[英]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? 有没有办法让我使用上面定义的这个向量访问Employee类的成员? I want to construct a map container with level of the employee as the key value. 我想构建一个以雇员级别作为键值的map容器。

如果要从迭代器访问Employee成员,可以使用成员访问运算符->

the_map[it->level()] = blah;
 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";

Unless I'm misinterpreting your question, this is simple enough. 除非我误解你的问题,否则这很简单。

  • it is an iterator over the vector of Employee . itEmployee向量的迭代器。
  • *it is the Employee *itEmployee
  • it-> lets you access members of that Employee it->允许您访问该Employee成员
    • eg: it->name() 例如: it->name()

So, if you want to build a map of employee level to Employee , you can do this: 因此,如果您要为Employee构建员工级别的地图,您可以这样做:

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 现在您拥有了employeeMap,它将员工的Level映射到Employee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM