作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我编译以下用于检测字谜的代码时,我遇到了名义上的错误。
anagram_test.cpp
#include "anagram.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
using namespace std;
BOOST_AUTO_TEST_CASE(no_matches)
{
auto subject = anagram::ana("diaper");
auto matches = subject.matches({"hello", "world", "zombies", "pants"});
vector<string> expected;
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(detects_simple_anagram)
{
auto subject = anagram::ana("ant");
auto matches = subject.matches({"tan", "stand", "at"});
vector<string> expected{"tan"};
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(does_not_detect_false_positives)
{
auto subject = anagram::ana("galea");
auto matches = subject.matches({"eagle"});
vector<string> expected;
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(detects_multiple_anagrams)
{
auto subject = anagram::ana("master");
auto matches = subject.matches({"stream", "pigeon", "maters"});
vector<string> expected{"stream", "maters"};
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(does_not_detect_anagram_subsets)
{
auto subject = anagram::ana("good");
auto matches = subject.matches({"dog", "goody"});
vector<string> expected;
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(detects_anagram)
{
auto subject = anagram::ana("listen");
auto matches = subject.matches({"enlists", "google", "inlets", "banana"});
vector<string> expected{"inlets"};
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(detects_multiple_anagrams2)
{
auto subject = anagram::ana("allergy");
auto matches = subject.matches({"gallery", "ballerina", "regally", "clergy", "largely", "leading"});
vector<string> expected{"gallery", "regally", "largely"};
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(detects_anagrams_case_insensitively)
{
auto subject = anagram::ana("Orchestra");
auto matches = subject.matches({"cashregister", "Carthorse", "radishes"});
vector<string> expected{"Carthorse"};
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(does_not_detect_a_word_as_its_own_anagram)
{
auto subject = anagram::ana("banana");
auto matches = subject.matches({"Banana"});
vector<string> expected;
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
BOOST_AUTO_TEST_CASE(matches_accepts_string_arguments)
{
auto subject = anagram::ana("ant");
auto matches = subject.matches({"stand", "tan", "at"});
vector<string> expected{"tan"};
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());
}
#endif
anagram.h
#include <string>
#include <vector>
#include <map>
using namespace std;
class anagram
{
public:
string word;
vector<string> matches(vector<string> possibleMatches )
{
vector<string> returnValue;
map<char, int> letterCount;
for(char& c : word)
{
letterCount[c]+=1;
}
map<char, int> currWordLetterCount;
for(int i = 0; i < possibleMatches.size(); i++ )
{
currWordLetterCount.clear();
for( char& c: possibleMatches[i])
{
currWordLetterCount[c]+=1;
}
if(currWordLetterCount == letterCount)
{
returnValue[i] = possibleMatches[i];
}
}
return returnValue;
}
static anagram ana(string inputWord);
};
anagram anagram::ana(string inputWord)
{
anagram Anagram;
Anagram.word = inputWord;
return Anagram;
}
这是使用 Boost v1.55。我不知道从哪里开始寻找错误。我的 GCC 版本是 4.7.3。我很确定错误在 detects_simple_anagram
最佳答案
我只是快速浏览了您的代码,但这里有一个错误:
vector<string> returnValue;
// ...
returnValue[i] = possibleMatches[i];
您创建了一个空 vector ,但随后对其进行了索引,从而导致未定义的行为。我想你想要:
returnValue.push_back( possibleMatches[i] );
关于c++ - "detects_simple_anagram": memory access violation at address 0x00000000: no mapping at fault address 中的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209176/
当我编译以下用于检测字谜的代码时,我遇到了名义上的错误。 anagram_test.cpp #include "anagram.h" #define BOOST_TEST_MAIN #include
我是一名优秀的程序员,十分优秀!