如何放置在映射(字符串和向量)?



我不确定是否可以在map容器中有一个向量。

如果是,我可以有向量和向量的映射吗?

输入>
ONE 1 11 111 1111
TWO 22 2 2222
THREE 333 3333 3
map<string, vector<int>> mp;

如何在上述容器中放置输入?

map<vector<int>, vector<int>> mp;

如果这是可能实现的,您将如何在这里放置元素,以及如何访问这些元素?

第一个例子很容易实现,例如:

#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <string>
using namespace std;
void loadFile(const string &fileName, map<string, vector<int>> &mp)
{
ifstream file(fileName.c_str());
string line;
while (getline(file, line))
{
istringstream iss(line);
string key;
if (iss >> key)
{
vector<int> &vec = mp[key];
int value;
while (iss >> value) {
vec.push_back(value);
}
}
}
}
int main()
{
map<string, vector<int>> mp;
loadFile("input.txt", mp);
// use mp as needed...
return 0;
}

最新更新