【问题标题】:C++ : How to return the value the iterator of a set is pointing to?C++:如何返回集合的迭代器指向的值?
【发布时间】:2016-04-30 19:52:43
【问题描述】:

我在一个集合上的迭代器返回值给出了内存位置而不是值。如何访问迭代器指向的元素?

我使用了一个类似的迭代器循环,之前它工作得很好,如返回迭代器指向的值(对于双端队列和向量模板类)。

这个问题是我今天早些时候遇到困难的一个脚本的后续问题 (C++ : adding an object to a set)。

脚本现在如下所示:

头文件

#ifndef EMPLOYEE_HH
#define EMPLOYEE_HH

#include <set>
#include <string>
#include <iostream>

using namespace std ;

class Employee {

public:
  // Constructor
  Employee(const char* name, double salary) :
    _name(name),
    _salary(salary) {
  }

  // Accessors
  const char* name() const { 
    return _name.c_str() ;
  }

  double salary() const {
    return _salary ;
  }

  // Print functions
  void businessCard(ostream& os = cout) const {
    os << "   +--------------------+  " << endl
       << "   |  ACME Corporation  |  " << endl 
       << "   +--------------------+  " << endl
       << "   Name: " << name() << endl
       << "   Salary: " << salary() << endl ;
  }

private:
  string _name ;
  double _salary ;

} ;

class Manager : public Employee {

public:
  //Constructor
  Manager(const char* _name, double _salary):
    Employee(_name, _salary),
    _subordinates() {
  }

  // Accessors & modifiers
  void addSubordinate(Employee& empl) {
    _subordinates.insert(&empl);
  }

  const set<Employee*>& listOfSubordinates() const {
    return _subordinates;
  }

  void businessCard(ostream& os = cout) const {
    Employee::businessCard() ;
    os << "   Function: Manager" << endl ;

    set<Employee*>::iterator iter ;
    iter = _subordinates.begin() ;

    os << "   Subordinates:" << endl ;
    if(_subordinates.empty()==true) {
      os << "      Nobody" << endl;
    }
    while(iter!=_subordinates.end()) {
      os << "      " << *iter << endl ;  // <-- returns the memory location 
      ++iter ;
    }

  }

private:
  set<Employee*> _subordinates ;
} ;

#endif

主脚本

#include <string>
#include <iostream>
#include "Employee.hh"

using namespace std ;

int main() {

   Employee emp1("David", 10000) ;
   Employee emp2("Ivo", 9000) ;
   Manager mgr1("Oscar", 18000) ;   // Manager of Ivo and David
   Manager mgr2("Jo", 14000) ;
   Manager mgr3("Frank", 22000) ;  // Manager of Jo and Oscar (and Ivo and David)

   mgr1.addSubordinate(emp1) ;
   mgr1.addSubordinate(emp2) ;
   mgr3.addSubordinate(mgr1) ;
   mgr3.addSubordinate(mgr2) ;

   cout << '\n' ;
   emp1.businessCard() ;

   cout << '\n' ;
   emp2.businessCard() ;

   cout << '\n' ;
   mgr1.businessCard() ;

   cout << '\n' ;
   mgr2.businessCard() ;

   cout << '\n' ;
   mgr3.businessCard() ;

   cout << '\n' ;       
   return 0;
}

非常感谢任何帮助。

【问题讨论】:

  • 不确定我是否正确理解了这个问题。你想取消引用迭代器*iter?
  • 假设你有一个为Employee定义的输出操作符写os &lt;&lt; " " &lt;&lt; *(*iter) &lt;&lt; endl ;
  • 嗯,好的,我找到了迭代器,*iter 是迭代器引用的对象,因为这是一个指针,你需要 *(*iter) 来获取对象

标签: c++ pointers memory iterator


【解决方案1】:

如果这个:*iter 是一个地址,那么这个:*(*iter)*iter 指向的值。

这应该适用于您的情况:

while(iter != _subordinates.end()) 
{
   os << "      " << **iter << endl ;  // <-- returns the value which is the set
   ++iter;
}

编辑:这解决了问题:(*iter)-&gt;name()

【讨论】:

  • 括号是多余的。
  • @alan 没关系。但我会编辑它只是为了让你的一天更美好
  • 老实说,我在发布问题之前已经尝试过了。因为这是最合乎逻辑的事情。但是,如果我使用 **iter,编译器会给我错误 cannot bind 'std::basic_ostream&lt;char&gt;' lvalue to 'std::basic_ostream&lt;char&gt;&amp;&amp;' for line os &lt;&lt; " " &lt;&lt; **iter &lt;&lt; endl;
  • @user 你试过这个(*iter)-&gt;Whatever吗?
  • @FirstStep 使用 (*iter)-&gt;Whatever 会导致编译器说 Employee 类没有名为“Whatever”的成员。我对使用它并不完全熟悉。我想我应该在 Employee 类中创建一个成员函数,在使用 this 时返回一个对象?
【解决方案2】:

那就是**iter;一个为迭代器解引用,一个为迭代器引用的指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多