我正在尝试创建一个C ++ Map,其中包含一个字符串作为键,一个C ++迭代器(一个向量迭代器)作为值。 在附加代码中:我有一个称为 numbers 的向量,该向量保留键的值,并且我正在使用此向量元素的迭代器,将其存储在名为 Directory 的地图中。 该代码应按编写的方式工作,但是当我尝试打印目录键值时,某些值未出现在目录中。我真的需要再看一遍,因为我现在已经花了几个小时,不知道自己在哪里错了。请帮忙!
#include <iostream>
#include <vector>
#include <map>
using namespace std;
vector<string> keys;
map<string,vector<string>::iterator> Directory;
void printStuff()
{
for (auto x : Directory)
{
cout << "Key: " << x.first << "---- Value: " << (*x.second) << endl;
}
}
void add()
{
string name;
string number;
printf("Enter Key : ");
cin >> name;
printf("Enter Value : ");
cin >> number;
keys.push_back(number);
vector<string>::iterator num;
num = keys.end() - 1;
Directory[name] = num;
}
int main()
{
int ch = 10;
while (ch != 0)
{
cout << "1. Add a Contact\n";
cout << "2. Lookup Directory\n";
cin >> ch;
switch (ch)
{
case 1:
add();
break;
case 2:
printStuff();
break;
case 3:
exit(0);
break;
}
}
return 0;
}