C++用户定义的向量

dan*_*lex 0 c++ stdvector

如何在允许用户输入定义向量名称的同时在c ++中声明向量?好的,在审核了您的回复之后,这里有更详细的信息; 以下是来自VS08 C++控制台应用程序的错误消息 -

Error 2 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::istream' to 'char *' e:\c++\project 5\project 5\project 5.cpp 58 project 5
Run Code Online (Sandbox Code Playgroud)

这是代码:

void addRecord()
{
     vector<char>recordName;
     vector<inventory>recordNameUser;
     cout << "Name the record you want to store it as\n";
     cin.get(cin, recordName);
     cout << "Enter the item description\n";
     cin.get(cin, recordNameUser.itemDescription);
     cout << "Enter the quanity on hand\n";
     cin >> recordNameUser.quanityOnHand;
     cout << "Enter the wholesale cost\n";
     cin >> recordNameUser.wholesaleCost;
     cout << "Enter the retail cost\n";
     cin >> recordNameUser.retailCost;
     cout << "Enter the date of the inventory (mm/dd/yyyy)\n";
     cin >> recordNameUser.inventoryDate;
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*hen 5

你真的想做什么?

通常,用户不关心变量名称.您可以做的是使用不同的用户定义键存储不同的向量:

map<string, vector<int> > user_vectors;
while (true) {
  string key = GetNameFromUserInput();
  int value = GetValueFromUserInput();
  user_vectors[key].push_back(value);
}
Run Code Online (Sandbox Code Playgroud)

根据您编辑的问题描述,您根本不需要矢量.

map<string, inventory> inventory_map;
while (!done) {
  string item_name;
  cin >> item_name;
  inventory item;
  cin >> item.itemDescription;
  cin >> item.quantityOnHand;
  ...;
  inventory_map[item_name] = item;
}
for (map<string, inventory>::const_iterator it = inventory_map.begin();
     it != inventory_map.end(); ++it) {
   cout << "Inventory contains: " << it->first
        << " described as: " << it->second.description;
}
Run Code Online (Sandbox Code Playgroud)