c ++矢量结构的地图

时间:2014-10-31 10:55:51

标签: c++ vector map iterator

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

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

(作为测试,看看表达式是否实际写出正确的单词)导致我出现此错误,我不明白为什么:

  

在函数'std :: vector,std :: allocator&gt;,std :: allocator,&gt;中std :: allocator&gt; &GT; &gt;,std :: allocator std :: char_traits,std :: allocator&gt;,std :: allocator std :: char_traits,std :: allocator&gt; &GT; &GT; &GT; &GT; anagrams(const std :: string&amp;,const&gt; Dictionary&amp;,int)':.   错误:无法转换'dict-&gt; Dictionary :: words.std :: map&lt; _Key,_Tp,   _Compare,_ Alloc&gt; :: find [with _Key = std :: basic_string,std :: allocator&gt;,_Tp =   的std ::向量,   std :: allocator&gt;,std :: allocator,std :: allocator&gt; &GT; &gt;,_比较=   性病::少,   std :: allocator&gt; &gt;,_ 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;
}

1 个答案:

答案 0 :(得分:1)

map::find返回一个迭代器,而不是布尔值。如果未找到密钥,则返回映射的past-the-end迭代器。而不是

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