【发布时间】:2023-03-09 21:35:01
【问题描述】:
嘿,我的导师(有点)给了我这个任务,我无法解决它。
所以基本上我得到的const vector<string> &data 充满了字符串,我需要检查它们的位置,比如它们在那个向量中的什么位置,所以这里是一个例子:
获取输入:data={"chair","desk","table","chair","chair","desk"}
我的输出应该是:{"chair" ->{0,3,4},"desk"->{1,5} , "table"->{2}}
所以我做的是:
map<string, set<size_t>> index(const vector<string> & data) noexcept {
map<string, set<size_t>> res; // thats gon be my return
if (data.empty()){
return res;
}
for (auto it = data.begin(); it != data.end(); it++) {
if(res.find(*it)==res.end()){
//so basically when we are the first , and nothing is there so
// it should insert for example =chair , and a 0
res.insert(std::make_pair(*it, 0)); // i tried it like that it doesnt work
}else{
// when the string "chair is already in there i want to append to the set the current index where we are and thats what i dont know how
}
}
return res;
}
如何获取当前字符串的索引并将其附加到我的 set
【问题讨论】:
-
你忘了说你的代码出了什么问题。
-
我正在寻求建议,我的代码没有任何问题需要帮助,了解如何在我的代码中实现此任务,如何获取当前字符串的索引并将其附加到我的集合中
-
什么索引?向量中的位置?
-
是的,我就是这个意思