Reputation: 175
I am trying to make a simple machine learning algorithm that takes in a body of text, parses it into a set and a list, gets context from a map of type , vector >, the list being of any size M, and then outputs a body of text resembling the input text. The problem came when I switched the map from type > to the type above, and the compiler throws me an error with a big, long error code that, from what I gather, means I did something wrong. (At least I assume it's that because asking me how my day was seems unlikely)
I output my list, set, map to separate files, and I've tried nesting my iteration loops differently, but the output looks all jacked up for the map.
//context: state is a list<string>, M is currently 2, wordList is my input text which has been parsed, and outputFile is my ofstream.
for (int i = 0; i < M; ++i) {
state.push_back("");
}
for (list<string>::iterator it = wordList.begin(); it != wordList.end(); ++it) {
wordMap[state].push_back(*it);
//this is the main code that is broken
outputFile.open(fileMap.c_str());
for (map<list<string>, vector<string> >::iterator itm = wordMap.begin(); itm != wordMap.end(); ++itm) {
for (list<string>::iterator itl = itm->first.begin(); itl != itm->first.end(); ++itl) {
outputFile << " " << *itl;
}
for (vector<string>::iterator itv = itm->second.begin(); itv != itm->second.end(); ++itv) {
outputFile << ", " << *itv;
}
outputFile << endl;
}
outputFile.close();
state.push_back(*it);
state.pop_front();
}
I expected an output to be similar to:
I am, legend, happy, hungry, tired, ...
with "I am" being the key and the rest being the values given with punctuation added.
Instead what I am getting is something like:
I am, legend
I am,
I am, happyhungry
I am, tired, thirst, warm, going-to-punch-my-computer, ...
And this is the error code my compiler gives me:
error: conversion from ‘std::list<std::basic_string<char> >::const_iterator
{aka std::_List_const_iterator<std::basic_string<char> >}’
to non-scalar type ‘std::list<std::basic_string<char> >::iterator
{aka std::_List_iterator<std::basic_string<char> >}’ requested
for (list<string>::iterator itl = itm->first.begin(); itl != itm->first.end(); ++itl)
Upvotes: 0
Views: 72
Reputation: 67723
... conversion from ... const_iterator to ... iterator ... requested
So you're trying to use an iterator
on a const container, instead of a const_iterator
.
This is because the key in a std::map
(or any associative container) is const. You can just use the newer for (auto &ref : container)
form to avoid typing out all the long iterator type names anyway. It's less effort and less error-prone.
Upvotes: 1