2016-04-30 8 views
-1

イテレータからの返り値は、値の代わりにメモリ位置を与えます。イテレータが指している要素にアクセスするにはどうすればよいですか?C++:セットのイテレータが指している値を返すには?

イテレータが指している値を返すときと同じように、それ以前によく似たイテレータループを使用しました(両端キューとベクターテンプレートクラスの両方)。

この質問は、私が本日の今日の難しかったスクリプト(C++ : adding an object to a set)のフォローアップです。次のように

スクリプトは今になります

ヘッダファイルすべてのヘルプははるかに高く評価されて

#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; 
} 

#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 

メインスクリプト。

+0

私は質問を正しく理解しているかわかりません。イテレータ '* iter'を逆参照したいと思いますか? – user463035818

+1

'Employee'の' os << "" << *(* iter)<< endl;の出力演算子が定義されているとします。 –

+1

ああ、イテレータを見つけました、 'iter'はイテレータが参照するオブジェクトですこれはポインタであるため、オブジェクトを取得するには '*(* iter)'が必要です – user463035818

答えて

2

この場合:*iterはアドレスです:*(*iter)がそのオブジェクトです。

これはあなたのケースで動作するはずです:

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

編集:これは、問題を修正しました:(*iter)->name()

+1

括弧は冗長です。 –

+0

@alanそれは本当に問題ではありません。しかし、私はあなたの一日をより良い一日にするためにそれを編集します。 –

+0

正直言って、私はすでに質問を掲示する前にこれを試しました。これが最も論理的なことだったので。しかし、私が** iterを使用すると、コンパイラは 'std :: basic_ostream 'のlvalueを' std :: basic_ostream && ''行 'os <<"に対してエラー " endl; ' –

1

**iterだろう。イテレータが参照する1つの逆参照、イテレータが参照するポインタの1つです。

関連する問題