c++ - 如何将<map>存储在<vector>中?

标签 c++ list templates dictionary stl

需要你的帮助。 假设我有 2 个类:class Categoryclass Product

这里是它的实现

    class Category
    {
        private:
           string _category Name;
           vector<string> _categoryVector;
        public:
           void Add()
           {
            cout << "\n=== ADD <CATEGORY> ===" << endl;
            cout << "\nEnter <Category> Name: ";
            cin >> _categoryName;
            _categoryVector.push_back(_categoryName);
           };
    }

class Product
{
   private:
      string _productName;
      double _productPrice;
      map<string, double> _productMap;
   public:
      void Add()
      {
         cout << "\n=== ADD <PRODUCT> ===" << endl;

         cout <<"\nEnter <Product> Name: ";
         cin >> _productName;
         cout << "\nEnter <Product> Price: ";
         cin >> _productPrice;
         _productMap.insert(pair<string, double>(_productName, _productPrice));         
       };
}

void main()
{
   Category c;
   c.Add();
   c.Add();

   Product p;
   p.Add();
   p.Add();

   system("pause");
}

我想将Product 存储在Category 中。在这个程序中做一些逻辑。

这有可能吗?

谢谢。

最佳答案

我不明白你想在你的程序中做什么,但是 如果您只想将 map 存储在 vector 中,请使用以下方法:

std::vector<std::map<std::string, double> >

关于c++ - 如何将<map>存储在<vector>中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305527/

相关文章:

python - 根据另外两个字典替换一个字典值列表

c++ - 我不应该在线程过程中使用 _endthreadex() 来展开堆栈吗?

c++ - 在 std::ostrstream::str 之后使用 std::ostrstream::freeze 以防止内存泄漏

c# - List<string> 在带有 MVC 的 HttpPost 上作为 System.Collections.Generic.List 返回

c# - 系统不支持异常: 'Collection is read-only.' thrown when removing object from iList

visual-studio-2010 - 函数声明中的 static_assert

c++ - C++ 模板只是 "grammar safe"并且首先不是类型安全的?

c++ - Const 限定符和前向引用

c++ - C++ 中顶点/边的 VTK 自定义 ID

c++ - 是否有像包含目录别名这样的概念?