我写了一个读入 map
的类。但我需要 map 可以在类外进行编辑。所以我的问题是如何返回 map
。
class ReadMap
{
string fileName;
public:
//constructors and destructor
ReadMap(){fileName="blank.txt";}
ReadMap(string name){fileName=name;}
~ReadMap(){}
//Function to print out visible list
void show()
{
LineDatabase Entry;
int LineNumber=100;
string buffer;
ifstream myfile (fileName.c_str() );
while (myfile.good())
{
myfile >> LineNumber >> ws;
getline (myfile, buffer);
Entry.insert(pair<int, string>(LineNumber, buffer));
cout <<buffer << endl;
}
//return Entry;
}
};
最佳答案
让 show() 的调用者传递对要填充的 map 的引用可能会更好,因为返回 map 往往会产生高开销。像这样:
void show(LineDatabase& Entry) {
// do your map readin logic
return;
}
关于c++ - 从类 C++ 返回映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6065591/