c++ - 从类 C++ 返回映射

标签 c++ class dictionary

我写了一个读入 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/

相关文章:

c++ - 合并集合中满足谓词的元素

c++ - 在 Eigen c++ 中,如何将 NxM 矩阵的每一行乘以 Nx1 标量的 vector ?

dictionary - react : how to get item width from map function

c++ - 重载在参数中获取指针的运算符

c++ - 如果我删除 C++ 中的指针,将会删除什么

c++ - `class HelloWorld : public Gtk::Window` 是什么意思?

python - 如何使用 pygame 创建多个横向移动的 Sprite 对象(如霸王龙游戏)

swift - 使用内联初始化器时 swift class init 崩溃

ios - 如何在 TableView 中显示全局字典项

python - 将具有相同哈希值的两个键放入字典中