我如何循环通过存储在映射中的对象向量?c++
How can I loop through a vector of objects stored within a map? C++
我有一个函数类,其vector变量存储其他函数对象。每个函数都映射到一个网络类中。
std::map<std::string, Function> functions;
当我输出一个对象时,它在映射函数后显示该对象的向量变量(next_func
)。
cout << network.functions.at("C");
输出:(FUNCTION: id=C, next_func=[D])
然而,当我尝试遍历映射以尝试为每个映射函数显示vector变量时,它显示一个空vector变量。
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") << "nn";
for (auto& element : m.fucntions) {
cout << "Predecessor counts: " << element.first
<< " : "<< element.second << "n";
}
}
这个复制构造函数
Function::Function(const Function& t) {
this->name = t.name_();
}
白马王子;使指针成员的值不确定。
对于某些编译器和选项,您可能会获得空指针值,但这不能保证:正式访问这些指针成员的值是未定义的行为。
另外,这个拷贝赋值操作符
Function& Function::operator=(const Function& t) {
if (this != &t) {
name = t.name;
}
return *this;
}
白马王子;不赋值指针成员。这在技术上是否是个错误取决于你的逻辑。但它看起来确实像一个bug,因为它是一个赋值,没有保留完整的赋值。
提示:可以通过构造函数的成员初始化列表,而不是通过赋值来初始化成员,如下所示:
Function::Function( Function const& other )
: name( other.name )
, func_pre( other.func_pre )
, func_next( other.func_next )
{}
这样做的优点是避免了额外的默认初始化,并且它适用于不可赋值但可复制的成员。
如果您不定义或声明复制构造函数,则编译器将为您生成这个特定的实现示例。
在main.cpp
中尝试以下代码for (map<string,Function>::iterator element=m.fucntions.begin();element!=m.fucntions.end();element++) {
cout <<"Predecessor counts: " << element->first << " : "<< element->second << "n";
}
我得到的输出是
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 ])
我已经实现了c++98循环迭代器,我无法在你的for循环中找出错误,但我想我的解决方案可以帮助你。
相关文章:
- 使用std::函数映射对象方法
- 使用"std::unordereded_map"映射到"std::list"对象
- 在c++中访问int到类对象的映射时出错
- 如何创建对象函数指针C++映射?
- 准确了解对象在内存中的映射方式
- 将包含不可复制对象的对插入到映射中
- 用unordered_map映射对象作为键
- 如何通过文件映射对象重新映射共享内存的视图?
- 文件映射对象和文件对象可以互换使用吗
- 如何从 std::映射'm'对象中返回 Iter 以'n'选定对象 (c++)
- 包含抽象基类ptr_map的映射对象
- C++ 映射对象的顺序
- 更新标准::映射对象数据
- C++ 内存泄漏与我的 std::映射 &对象和标准::矢量 &对象
- Windows CreateFileMapping:具有相同支持文件的不同文件映射对象
- 使用C++中的文件映射对象进行读取
- c++构造函数映射对象(已编辑)
- 在映射对象上定义自定义迭代器:神秘的"incomplete type"错误
- 在(c++) cocos2dx中添加映射对象到Vector
- c++代码中共享文件映射对象大小的问题