首先,我是C++/OOP的新手.
我想在我的课程中包含一个std :: map,并且想知道如何为我的班级用户提供地图typedef和功能.我是否需要像下面的简单示例那样做?(我只展示了一个子集来表明我正在尝试做什么)
对于我已经包含容器的任何类(即使是映射方法的子集),我不得不这样做.它似乎也是一个类维护问题(例如,今天可能不需要一些map方法,但将来需要它)
ps除了回答这个问题之外,对以下示例的任何其他更正/反馈都表示赞赏.
#include <map>
#include <string>
class Inventory {
public:
typedef std::string key_type;
typedef std::string mapped_type;
typedef std::map<key_type, mapped_type>::value_type value_type;
Inventory() { }
Inventory(int lotNum) : lotNum_(lotNum) { }
void insert(const value_type& el) { cars_.insert(el); }
//
// TODO: iterators, erase, etc
//
private:
int lotNum_;
std::map<key_type, mapped_type> cars_;
};
int main() {
Inventory ourCars(1);
ourCars.insert( Inventory::value_type( "BMW","ABC123" ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)