私は他の関数オブジェクトを格納するベクトル変数を持つ関数クラスを持っています。各関数はネットワーククラスにマップされます。マップ内に格納されたオブジェクトのベクトルをループするにはどうすればよいですか? 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";
}
}
'Function'には作業コピーコンストラクタがありますか? 'for(auto&element:network.functions)'を実行すると何か変更されますか? – aschepler
「機能」とは何ですか?あなたは、[最小、完全で、かつ実証可能な例](http://stackoverflow.com/help/mcve)を作成して私たちを見せてください。 –
@ascheplerはい、それは作業コピーコンストラクタを持ち、 '&'を追加した後に動作します。これはベクターのコピーを返すためですか? – girthquake