【问题标题】:Appending to map with set<size_t> in it附加到带有 set<size_t> 的地图
【发布时间】:2023-03-09 21:35:01
【问题描述】:

嘿,我的导师(有点)给了我这个任务,我无法解决它。

所以基本上我得到的const vector&lt;string&gt; &amp;data 充满了字符串,我需要检查它们的位置,比如它们在那个向量中的什么位置,所以这里是一个例子:

获取输入:data={"chair","desk","table","chair","chair","desk"}

我的输出应该是:{"chair" -&gt;{0,3,4},"desk"-&gt;{1,5} , "table"-&gt;{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 以便如前所述工作?

【问题讨论】:

  • 你忘了说你的代码出了什么问题。
  • 我正在寻求建议,我的代码没有任何问题需要帮助,了解如何在我的代码中实现此任务,如何获取当前字符串的索引并将其附加到我的集合中
  • 什么索引?向量中的位置?
  • 是的,我就是这个意思

标签: c++ hashmap


【解决方案1】:

您可以使用基于索引的循环很容易地实现这一点:

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;
}

【讨论】:

  • decltype(data.size()) ?为什么?
  • @MICHAEL 很好,我很高兴 :) 当您被允许接受时,不要忘记接受可以解决您问题的答案。
  • @largest_prime_is_463035818 只是为了完全确定类型匹配。一般不是问题,但也没有什么坏处。明确说明一个人到底想要什么类型也很好。
  • @largest_prime_is_463035818 当然,正确的方法是使用范围库,或者编写一些代码,让人们可以编写for (auto [i, elem] : data) 并避免使用索引类型: )
  • 不,我们知道vector::size 基本上总是返回;只需 for (std::size_t i = 0; i &lt; data.size(); ++i) 就可以了,不要再不必要地混淆大家了!
【解决方案2】:
#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";
   }
}

所以你的问题可以用索引函数中看到的单行来解决 - 但为什么?

您将所有这些不同的标准容器类型作为输入是有原因的 - 它们中的每一个都有一些保证,可以在文档中查找。

向量元素在内存(序列容器)中是连续的。

而 map 和 set 是关联的并且具有唯一的键。

请参阅容器属性的文档(我链接到向量 - 但其余的也存在于左侧的树中)。 http://www.cplusplus.com/reference/vector/vector/

课程是知道使用哪个工具(容器)来解决特定任务

一旦你确定了......确保项目是唯一的,或者如何对向量中的每个奇数元素进行排序应该是轻而易举的事。

【讨论】:

    猜你喜欢
    • 2014-10-03
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2021-04-15
    • 1970-01-01
    相关资源
    最近更新 更多