【问题标题】:Map as key in map地图作为地图中的关键
【发布时间】:2019-08-09 19:39:15
【问题描述】:

我正在尝试解决问题:编写一个方法来对字符串数组进行排序,以便所有字谜彼此相邻。

我解决这个问题的方法是:假设 vec 是我的字符串向量。我制作了第一个字符串的地图,在地图中找到它。如果映射中不存在,则插入字符串的映射作为键,将该字符串作为值(值字段是向量)。如果它存在于地图中,则将该字符串添加到值字段(这是一个向量)中。对所有字符串重复。

数据结构如下:

std::unordered_map<std::unordered_map<char,int>,std::vector<std::string>> map;
std::unordered_map<char, int> strMap;

for (auto each : vec)
{
    strMap = getMap(each);//getMap(string) will get the unordered_map of each string

    if (map.find(strMap) != map.end()) 
        map[strMap].push_back(each);
    else
        map.insert({ strMap, std::vector<std::string> {1,each} });
}

这可以吗?我收到错误: 错误 C2280 'std::hash<_kty>::hash(const std::hash<_kty> &)': 试图引用已删除的函数

【问题讨论】:

  • 相关:Compilation error related to map and unordered_map: “attempting to reference a deleted function”。使用键是按字母顺序排序的字符串的哈希会更容易,因此"bad""dab" 都在"abd" 存储桶中。
  • 您是否正在寻找解决问题的好方法(例如使用带有自定义比较方法的 std:sort)或有关嵌套地图的信息?
  • 您能否提供输入数据和预期输出数据的示例?不知何故我不清楚。请编辑您的问题。
  • std::hashunordered_map 禁用,因此它不能像这样存储在无序关联中。您必须提供自己的哈希函数。
  • 旁白:你只需要map[getMap(each)].push_back(each);,因为[]会在必要时构造一个空向量。

标签: c++ c++14


【解决方案1】:

很抱歉,我没有回答您关于地图方法的问题。

但因为我认为其他方法可能更好,所以我将向您展示不同的解决方案。

我将使用标准方法来找出一个词是否是另一个词的字谜。标准程序是: 对单词中的字母进行排序。如果两个单词的字母排序相同,那么我们就有了一个字谜。

非常简单的例子:

gartner --> aegnrrt 授予者 --> aegnrrt

那么,该怎么做:我们将制作原始数据的副本。此外,我们将索引存储到原始数据。然后,我们对复制列表中所有单词的字母进行排序。然后,对单词列表进行排序(使用排序后的字母)。结果,字谜将彼此相邻。

在一个额外的循环中,我们将检查相邻的值并设置一个标志,如果我们发现了一个字谜。

然后我们将结果展示给用户。

请查看附件代码。请注意:这是众多可能性之一。 . .

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <iterator>
#include <iomanip>

// Random Word list
std::vector<std::string> words{"Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua", "At", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "Stet", "clita", "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "Lorem", "ipsum", "dolor", "sit", "amet", "Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua", "At", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "Stet", "clita", "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "Lorem", "ipsum", "dolor", "sit", "amet", "Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua", "At", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "Stet", "clita", "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "Lorem", "ipsum", "dolor", "sit", "amet", "Duis", "autem", "vel", "eum", "iriure", "dolor", "in", "hendrerit", "in", "vulputate", "velit", "esse", "molestie", "consequat", "vel", "illum", "dolore", "eu", "feugiat", "nulla", "facilisis", "at", "vero", "eros", "et", "accumsan", "et", "iusto", "odio", "dignissim", "qui", "blandit", "praesent", "luptatum", "zzril", "delenit", "augue", "duis", "dolore", "te", "feugait", "nulla", "facilisi"};

// We want to copy the words into a list along with its index for sorting
struct WordWithIndex {
    WordWithIndex() {};
    WordWithIndex(const std::string&s, size_t i) : word(s), index(i) {};

    std::string word{};
    size_t index{};
    bool isAnagram{false};
};

int main()
{
    // Create a new vector and initialize the size
    std::vector<WordWithIndex> wordWithIndex(words.size());
    // COpy the word list into new vector
    std::transform(
        words.begin(), words.end(), wordWithIndex.begin(),
        [i=0U](const std::string& s) mutable { return WordWithIndex(s,i++);}
    );

    // Sort the letters in each word. Words with sorted letters, that are equal are an anagram
    std::for_each(wordWithIndex.begin(),wordWithIndex.end(),
        [](WordWithIndex& wwi) {std::sort(wwi.word.begin(),wwi.word.end());});

    // Now sort the word list with sort criteria : Words with sorted letters    
    std::sort( wordWithIndex.begin(), wordWithIndex.end(),
        [](WordWithIndex& wwi1, WordWithIndex& wwi2) {return wwi1.word < wwi2.word;}
    );

    // Find anagrams
    std::vector<WordWithIndex>::iterator wIter = wordWithIndex.begin();
    while (wIter != wordWithIndex.end()) {
        std::vector<WordWithIndex>::iterator adjacentFound = std::adjacent_find(
            wIter, wordWithIndex.end(),[](const WordWithIndex &a, const WordWithIndex &b){ return a.word == b.word;});
        if (adjacentFound !=  wordWithIndex.end()) {
            adjacentFound->isAnagram = true;
            ++adjacentFound;
            adjacentFound->isAnagram = true;
        }
        wIter = adjacentFound;
    }

    // Show result
    for (size_t i=0U; i < words.size(); ++i)
        std::cout << std::left << std::setw(4) << i << "\t" << std::setw(25) << words[wordWithIndex[i].index] << "\t\tIs Anagram:  " << std::boolalpha <<  wordWithIndex[i].isAnagram << "\n";

    return 0;
}

【讨论】:

    猜你喜欢
    • 2016-09-09
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多