gpt4 book ai didi

c++ - "detects_simple_anagram": memory access violation at address 0x00000000: no mapping at fault address 中的 fatal error

转载 作者:行者123 更新时间:2023-11-30 01:51:20 24 4
gpt4 key购买 nike

当我编译以下用于检测字谜的代码时,我遇到了名义上的错误。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com