I've been working at this issue for hours, now, and I have no idea what's going on. For some reason, whenever I try to print out the value of my two vectors dict
and sorted_dict
, I receive varying inputs. For example, I seem to be unable to print out both the 5th element in dict
at the same time as the zeroth element in sorted_dict
(the letters in dict[5] become re-arranged), but the values print fine when I am accessing the fifth element of both vectors.
I looked through my code, and I can't find a place where I modified either of these vector values after I set them in the load_values_from_file
method. Despite the fact that I've worked with vectors before, I'm assuming this boils down to my ignorance about how they work. I've posted relevant code below with output:
vector<string> dict;
vector<string> sorted_dict;
vector<bool> seen;
size_t max_length = 0;
vector<Anagram> ana_set = {};
string strip_word(string w)
{
string word = w;
sort(word.begin(), word.end());
for(size_t i = 0; i < strlen(word.c_str()); i++)
{
if(65 <= word[i] && word[i] <= 90)
word[i] = (int)word[i] + 32;
}
return word;
}
bool load_values_from_file(const string &filename) {
ifstream input_file(filename.c_str());
if (!input_file) {
cerr << "Error: Cannot open file '" << filename << "'." << endl;
return false;
}
input_file.exceptions(ifstream::badbit);
string line;
vector<string> data;
try {
while (getline(input_file, line)) {
data.push_back(line);
}
input_file.close();
} catch (const ifstream::failure &f) {
cerr << "Error: An I/O error occurred reading '" << filename << "'.";
return false;
}
for(string val : data)
{
dict.push_back(val);
sorted_dict.push_back(strip_word(val));
}
for(size_t i = 0; i < dict.size(); i++)
{
seen.push_back(false);
}
return true;
}
void print_sorted()
{
for(string vals : dict)
{
cout << vals << endl;
}
}
Anagram find(string word, string sorted_w) {
vector<int> perms = {};
struct Anagram ana = { word, perms, word.length(), { word } };
for(size_t i = 0; i < dict.size(); i++)
{
if(dict[i] != word)
{
if(sorted_w == sorted_dict[i])
{
ana.perms.push_back(i);
cout << i << "," << ana.perms[ana.perms.size()-1] << endl;
seen[i] = true;
} else {
/********************************************
WHERE I TEST VALUES OF DICT AND SORTED_DICT
*********************************************/
cout << dict[i] << "|" << sorted_dict[i] << endl;
}
} else {
seen[i] = true;
}
}
return ana;
}
int main(int argc, char * const argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <filename>" << endl;
return 1;
}
string filename(argv[1]);
if (!load_values_from_file(filename)) {
return 1;
}
for(size_t i = 0; i < dict.size(); i++)
{
if(!seen[i])
{
Anagram ana = find(dict[i], sorted_dict[i]);
if(ana.perms.size() != 0 && ana.length >= max_length)
{
vector<string> other_words;
other_words = sort_words(ana);
for(string val : other_words)
{
ana.perm_words.push_back(val);
}
ana_set.push_back(ana);
max_length = ana.length;
}
}
}
...
return 0;
}
输出值
At the location in my code where I test the vectors dict
and sorted_dict
(marked above), when I replace dict[i] with dict[5] and sorted_dict[i] with sorted_dict[0], this is printed out each time:
aetce|
...missing the zeroth entry in sorted_dict completely, and butchering the dict
value.
当我将索引替换为5(均为i)时,会收到以下正确输出:
trace|acert
当我尝试使用其他索引时,我收到第一个结果(缺少第二个值)。有时,甚至会丢失'|'。我真的不知道会出什么问题,但是我怀疑这与我的“ strip_word”类有关吗?另外,顺便说一句,“ strip_word”仅对字母进行排序,并通过其ASCII值将其强制为小写。我还包括了程序读取的文本文件:
dict.txt
tea
home
eat
ate
react
trace