2016-09-04 9 views
0

私は他の関数オブジェクトを格納するベクトル変数を持つ関数クラスを持っています。各関数はネットワーククラスにマップされます。マップ内に格納されたオブジェクトのベクトルをループするにはどうすればよいですか? C++

std::map<std::string, Function> functions; 

場合I出力が機能をマッピングした後に、そのオブジェクトのベクトル変数(next_func)を示す単一のオブジェクト。

cout << network.functions.at("C"); 

出力:私は試してみて、マップされた各機能のためのベクトル変数を表示するマップをループしようとすると、

(FUNCTION: id=C, next_func=[D]) 

はしかし、それは空のベクトル変数を示しています。

for (auto element : network.functions) { 
    cout << element.second << "\n"; 
} 

出力:私は私が行っ必要なもののために反復の適切な形式を使用していない場合は

(FUNCTION: id=C, next_func=[]) 
(FUNCTION: id=D, next_func=[]) 
(FUNCTION: id=E, next_func=[]) 

は、私はそれが私のクラス構造とは何かをだかはわかりませんか。

マップされたオブジェクトをループして、ベクトル変数内のオブジェクトを表示/操作するにはどうすればよいですか?

実施例:

Function.h

#ifndef Function_H 
#define Function_H 

#include <vector> 
#include <iterator> 
#include <iostream> 
#include <string> 

using namespace std; 

class Function { 
public: 
    string name; 
    std::vector<Function*> func_pre; 
    std::vector<Function*> func_next; 
public: 
    Function(); 
    Function(const string& id, int duration); 
    Function(const Function&); 
    ~Function(); 
    Function& operator=(const Function& t); 
    string name_() const; 
    void addNext(Function& s); 
    void addPre(Function& p); 
    std::string toString() const; 
    friend std::ostream& operator<<(std::ostream&, const Function&); 
}; 
#endif 

Function.cpp

#include <sstream> 
#include "Function.h" 

using namespace std; 

Function::Function() { 
    name = ""; 
} 

Function::Function(const string& id, int duration) { 
    this->name = id; 
} 

Function::Function(const Function& t) { 
    this->name = t.name_(); 
} 

Function::~Function() {} 

Function& Function::operator=(const Function& t) { 
    if (this != &t) { 
     name = t.name; 
    } 
    return *this; 
} 

string Function::name_() const { 
    return this->name; 
} 

void Function::addNext(Function& s) { 
    Function* e = &s; 
    func_next.push_back(e); 
} 

void Function::addPre(Function& p) { 
    Function* e = &p; 
    func_pre.push_back(e); 
} 

std::ostream& operator<<(std::ostream& s, const Function& e) { 
    s << e.toString(); 
    return s; 
} 

std::string Function::toString() const { 
    std::string s = "(Function: id=" + name +" ,to="; 
    s = s+"["; 
    for(unsigned int i = 0; i < func_next.size(); i++) 
     s = s + func_next[i]->name_() + " "; 
    s = s+"], from=["; 
    for(unsigned int i = 0; i < func_pre.size(); i++) 
     s = s + func_pre[i]->name_() + " "; 
    s = s + "])"; 
    return s; 
} 

Map.h

#ifndef Map_H 
#define Map_H 

#include <map> 
#include <vector> 
#include <string> 
#include "Function.h" 

class Map{ 

public: 
    std::map<std::string, Function> fucntions; 
public: 
    explicit Map(); 
    Map(const Map& n); 
    ~Map(); 
    Map& operator=(const Map& i); 
    void addFunction(const string id, int x); 
    void addDep(const string& from, const string& to); 
    std::string toString()const; 
}; 
#endif 

Map.cpp

#include "Map.h" 
#include <iterator> 
#include <iostream> 
#include <iterator> 

using namespace std; 

Map::Map() { 
    fucntions = {}; 
} 

Map::Map(const Map& n) { 
    this->fucntions = n.fucntions; 
} 

Map::~Map() { 
} 

Map& Map::operator=(const Map& i) { 
    if (this != &i) { 
     fucntions = i.fucntions; 
    } 
    return *this; 
} 

void Map::addFunction(const string id, int x) { 
    Function t(id, x); 
    fucntions[t.name_()] = t; 
} 

void Map::addDep(const string& from, const string& to) { 
    fucntions.at(from).addNext(fucntions.at(to)); 
    fucntions.at(to).addPre(fucntions.at(from)); 
} 

std::string Map::toString() const { 
    std::string s = "(\n"; 
    std::map<std::string, Function>::const_iterator i = fucntions.begin(); 
    std::map<std::string, Function>::const_iterator end = fucntions.end(); 
    if (i == end) 
     s += "<Empty>"; 
    else 
     do{ 
      s += (*i).second.toString(); 
      if (++i != end) s+= ",\n"; 
     }while (!(i==end)); 
    s +="\n)"; 
    return s; 
} 

main.cppに

#include <iostream> 
#include "Map.h" 
#include "Function.h" 

using namespace std; 

int main(){ 
    Map m; 
    m.addFunction("A", 10); 
    m.addFunction("B", 30); 
    m.addFunction("C", 20); 
    m.addFunction("D", 40); 
    m.addFunction("E", 20); 
    m.addDep("A", "B"); 
    m.addDep("A", "C"); 
    m.addDep("B", "D"); 
    m.addDep("D", "E"); 
    m.addDep("C", "E"); 
    m.addDep("B", "C"); 

    cout << m.fucntions.at("C") << "\n\n"; 

    for (auto& element : m.fucntions) { 
     cout << "Predecessor counts: " << element.first 
       << " : "<< element.second << "\n"; 
    } 
} 
+2

'Function'には作業コピーコンストラクタがありますか? 'for(auto&element:network.functions)'を実行すると何か変更されますか? – aschepler

+0

「機能」とは何ですか?あなたは、[最小、完全で、かつ実証可能な例](http://stackoverflow.com/help/mcve)を作成して私たちを見せてください。 –

+0

@ascheplerはい、それは作業コピーコンストラクタを持ち、 '&'を追加した後に動作します。これはベクターのコピーを返すためですか? – girthquake

答えて

2

このコピーコンストラクタ

Function::Function(const Function& t) { 
    this->name = t.name_(); 
} 

&hellip;ポインタメンバーに不確定な値を残します。

一部のコンパイラとオプションでは、nullポインタ値を取得することがありますが、これは保証されていません。これらのポインタメンバーの値に正式にアクセスすることは未定義の動作です。さらに


、このコピー代入演算子、

Function& Function::operator=(const Function& t) { 
    if (this != &t) { 
     name = t.name; 
    } 
    return *this; 
} 

&hellip;ポインタメンバーを割り当てません。それが技術的にエラーであるかどうかは、ロジックによって決まります。しかし、バグのように見えるのは、完全に割り当てられた値を保持しない割り当てなのでです。


ヒント:これは、余分なデフォルト・初期化を回避するという利点を持っている

Function::Function(Function const& other) 
    : name(other.name) 
    , func_pre(other.func_pre) 
    , func_next(other.func_next) 
{} 

、それ:代わりに割り当てを通じてメンバーを初期化するのは、あなたがこのように、コンストラクタのメンバー初期化子リストを経由してそれを行うことができます割り当て不可能だがコピー可能なメンバーのために働く。

この特定の実装例は、コピーコンストラクタを定義または宣言していない場合にコンパイラが生成するものです。

+0

私のコードを見ていただきありがとうございます。 Functionコピーコンストラクタを 'this-> name = t.name;'に変更しました。しかし、私はどのように私は割り当てられた値を保持する関数の代入演算子を得ることができるか分からない。 – girthquake

2

私が得

for (map<string,Function>::iterator element=m.fucntions.begin();element!=m.fucntions.end();element++) { 
    cout <<"Predecessor counts: " << element->first << " : "<< element->second << "\n"; 
} 

出力は私があなたにエラーを把握することができません、++ 98ループイテレータcを実施しています

Predecessor counts: A : (Function: id=A ,to=[B C ], from=[]) 
Predecessor counts: B : (Function: id=B ,to=[D C ], from=[A ]) 
Predecessor counts: C : (Function: id=C ,to=[E ], from=[A B ]) 
Predecessor counts: D : (Function: id=D ,to=[E ], from=[B ]) 
Predecessor counts: E : (Function: id=E ,to=[], from=[D C ]) 

あるごmain.cppに次のコードを試してみてくださいforループが、私のソリューションはあなたを助けると思う。

関連する問題