C++ 将集合附加到地图<;尺寸&u t>;在里面

C++ 将集合附加到地图<;尺寸&u t>;在里面,c++,hashmap,C++,Hashmap,嘿,我的导师(有点)给了我这个任务,我解决它有困难 基本上,我得到的是const vector&data字符串,我需要检查它们的位置,比如它们在向量中的位置,下面是一个例子: 使用以下信息获取输入:data={“椅子”、“桌子”、“桌子”、“椅子”、“椅子”、“桌子”} 我的输出应该是:{“椅子”->{0,3,4},“桌子”->{1,5},“桌子”->{2} 所以我所做的是: map<string, set<size_t>> index(const vector<

嘿,我的导师(有点)给了我这个任务,我解决它有困难

基本上,我得到的是
const vector&data
字符串,我需要检查它们的位置,比如它们在向量中的位置,下面是一个例子:

使用以下信息获取输入:
data={“椅子”、“桌子”、“桌子”、“椅子”、“椅子”、“桌子”}

我的输出应该是:
{“椅子”->{0,3,4},“桌子”->{1,5},“桌子”->{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;
}
映射索引(常量向量和数据)无例外{
map res;//这将是我的回报
if(data.empty()){
返回res;
}
for(auto it=data.begin();it!=data.end();it++){
如果(res.find(*it)=res.end()){
//所以基本上当我们是第一个,没有什么是这样的
//它应该插入例如=椅子和0
res.insert(std::make_pair(*it,0));//我试过,好像它不起作用一样
}否则{
//当字符串“chair”已经在那里时,我想将当前索引添加到集合中,这就是我不知道如何做的
}
}
返回res;
}

如何获取当前字符串的索引并将其附加到我的集合中,以便按上述方式工作?

通过使用基于索引的循环,您可以非常轻松地实现这一点:

map<string, set<size_t>> index(const vector<string> & data) 
{    
    map<string, set<size_t>> res; 

    for (std::size_t i = 0u; i < data.size(); ++i)
        res[ data[i] ].insert(i);
      //    ^string^          ^index

    return res;
}
地图索引(常量向量和数据)
{    
地图资源;
对于(std::size_t i=0u;i
#包括
#包括
#包括
#包括
#包括
标准::映射索引(常数标准::向量和数据)无例外
{
地图资源;
对于(size_t idx=0u;idxstd::难道你忘了说你的代码出了什么问题吗?我在征求意见,我的代码没有什么问题。有关如何在我的代码中实现这项任务,如何获取当前字符串的索引并将其附加到我的集合中,什么索引?向量中的位置?是的,这就是我的意思谢谢你。
decltype(data.size())
?为什么会这样?@MICHAEL Good,我很高兴:)当你被允许接受的时候,别忘了接受能解决你问题的答案。@Maximum_prime_是_463035818,只是为了完全确保类型匹配。一般来说,这不是问题,但没有坏处。明确自己想要什么类型也很好。@Maximum_prime_是_463035818当然,正确的方法是使用一个范围库,或者编写一些代码,让人们可以为(auto[i,elem]:data)编写
,避免索引类型的混乱:)
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>

std::map<std::string, std::set<size_t>> index(const std::vector<std::string>& data) noexcept 
{
    std::map<std::string, std::set<size_t>> res;
    for (size_t idx = 0u; idx < data.size(); idx++) 
    {
        res[data[idx]].insert(idx);
    }
    return res;        
}

int main()
{
   const std::vector<std::string> data={"chair","desk","table","chair","chair","desk"};
   auto map = index(data);

   for( auto it = map.begin(); it != map.end(); ++it )
   {
      std::cout << (*it).first << " ->";
      for( auto it_id = (*it).second.begin(); it_id != (*it).second.end(); it_id++)
      {
        std::cout << " " << *it_id;
      }
      std::cout << "\n";
   }
}