【问题标题】:c++ map of vector in structurec++ 结构中向量的映射
【发布时间】:2014-10-31 10:55:50
【问题描述】:

我想做的程序是一个字谜查找器 从字典文件和字符串输入中,函数“anagrams”应该返回一个向量(每个大小从 1 到最大值一个),该向量在字典中找到的匹配所有可能与输入的子词组合的词向量字谜 当我使用函数 createdictionary 创建新字典时,我将每个字谜放在一个字符串向量中 但是,当我想在我的 anagrams 函数中检查这些 anagrams 时,我不知道如何访问它(第 74 行) 在 uniqueAnagram 中,我有所有可能的子字谜,一切都很好,但是使用

  if (dict.words.find(it->second)){
    cout << dict.words.find(it->second)->second[0] << endl;
  }

在循环中(作为测试表达式是否真的写了正确的单词)导致我出现这个错误,我不明白为什么:

在函数'std::vector, std::allocator >, std::allocator, > std::allocator > > >, std::allocator std::char_traits, std::allocator >, std::allocator std::char_traits, std::allocator > > > > > anagrams(const std::string&, const > Dictionary&, int)':| 错误:无法转换 'dict->Dictionary::words.std::map<_key _tp _compare _alloc>::find [with _Key = std::basic_string, std::allocator >, _Tp = 标准::向量, std::allocator >, std::allocator, std::allocator > > >, _Compare = 标准::少, std::allocator > >, _Alloc = std::allocator

在过去的 10 个小时里我一直被困在这个问题上,我不能再忍受了,我真的不知道如何解决这个问题 谢谢你的帮助,你会救我的命

//anagrams.h
#ifndef ANAGRAMS_H_INCLUDED
#define ANAGRAMS_H_INCLUDED

#include <vector>
#include <string>
#include <map>

using namespace std;

struct Dictionary{
  map<string, vector<string> > words;
};

Dictionary createdictionary(const string&);
vector<vector<string> > anagrams(const string&, const Dictionary&, int);

#endif // ANAGRAMS_H_INCLUDED

//anagrams.cpp
#include "anagrams.h"
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

string sortString(string input);

Dictionary createdictionary(const string& filename){
    Dictionary dictionary;
    ifstream ifs;
    ifs.open(filename.c_str());
    string word;

    while (ifs >> word){
      string sortedWord = sortString(word);
      (dictionary.words[sortedWord]).push_back(word);
    }
  return dictionary;
}

string sortString(string input){
  vector<char> vectorWord(input.begin(), input.end());
  sort(vectorWord.begin(), vectorWord.end());
  string sortedInput(vectorWord.begin(), vectorWord.end());
  return sortedInput;
}


vector<vector<string> > anagrams(const string& input, const Dictionary& dict, int max){
  vector<vector<string> > anagrams;
  size_t n = input.length();
  for (int r = 0; r < max + 1; r++){
    vector<bool> v(n);
    fill(v.begin() + r, v.end(), true);
    map<string, string> uniqueAnagram;

    do {
      string word;
        for (size_t i = 0; i < n; ++i) {
            if (!v[i]) {
                word = word + input[i];
            }
        }
        word = sortString(word);
        uniqueAnagram[word] = word;
    } while (next_permutation(v.begin(), v.end()));
    vector<string> tempAnagram;
    for(map<string, string>::iterator it = uniqueAnagram.begin(); it != uniqueAnagram.end(); it++){
      if (dict.words.find(it->second)){
        cout << dict.words.find(it->second)->second[0] << endl;
      }
    }
    sort(tempAnagram.begin(), tempAnagram.end());
    anagrams.push_back(tempAnagram);
  }


  vector<char> vectorWord(input.begin(), input.end());
  sort(vectorWord.begin(), vectorWord.end());
  string sortedWord(vectorWord.begin(), vectorWord.end());



//  cout << (dict.words.find(sortedWord)->second)[0] << endl;
  return anagrams;
}

//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "anagrams.h"

using namespace std;

int main()
{
    string filename = "C:/dictionary.txt";
    Dictionary dictionary = createdictionary(filename);
    vector<vector<string> > anagram = anagrams("llohe", dictionary, 5);
    return 0;
}

【问题讨论】:

  • 您声称有问题的代码甚至不在您的代码示例中。
  • 抱歉,我一直在进行大量编辑以尝试修复它,但我丢失了它我正在使用没有鼠标的笔记本电脑,所以有点困难
  • 糟糕,我以为我删除了它……我将它用于一些测试。我复制了错误的其余部分,但我不知道如何在此处换行

标签: c++ vector map iterator


【解决方案1】:

map::find 返回一个迭代器,而不是布尔值。如果未找到密钥,则返回映射的结束迭代器。所以不是

if (dict.words.find(it->second))

你想要的

if (dict.words.find(it->second) != dict.words.end())

或

if (dict.words.count(it->second) != 0)

存储迭代器会更有效,这样您就不需要两次查找密钥:

auto found = dict.words.find(it->second);
if (found != dict.words.end()) {
    cout << found->second[0] << endl;
}

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多