I've started a little toy project as a way to teach myself C++ and have hit a wall. In the code below the highlighted line is causing a build error saying:
welcome.cc:65:26: error: cannot convert 'std::_Rb_tree_iterator >' to 'char' in assignment current = result.first;
and I'm not sure why. The goal is to create a Markov chain for English placenames, the intent of the line in question is meant to update the current letter in the generated placename with a randomly selected value of the previous one, my random selection method is taken from this question. As I say, new to C++ and no idea what I've done wrong.
int main(int argc, char**argv) {
string line;
ifstream myfile;
std::multimap<char, char> m;
myfile.open ("C:\\Users\\james\\Desktop\\placenames.txt");
if (myfile.is_open()){
while ( getline (myfile,line) )
{
for(std::string::size_type i = 0; i < line.size(); ++i) {
std::cout<<line[i]<<std::endl;
m.insert(std::pair<char, char>(line[i], line[i+1]));
}
}
for (std::multimap<char, char>::iterator it = m.begin();it != m.end(); ++it)
cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
myfile.close();
// for( auto it = m.begin(), end = m.end();it != end; it = m.upper_bound(it->first))
// cout << (*it).first << ' ' << m.count( (*it).first) << endl;
auto current = 'A';
std::string name = "A";
typedef std::multimap<const char, char>::iterator MMAPIterator;
for (int j=0; j<8; ++j){
std::pair<MMAPIterator, MMAPIterator> result = m.equal_range(current);
std::size_t sz = std::distance(result.first, result.second);
std::size_t idx = std::rand() % sz;
std::advance(result.first, idx);
current = result.first; <------THIS LINE
//name+=current;
}
cout << name;
}
else cout << "Unable to open file";
return 0;
}
I'm aware its a pretty naive implementation of a markov chain, btw. As I say, intent was to play around with c++ rather than anything else.
You have a dual layer of pairs here, firstly the equal_range
function returns a range, so your
std::size_t sz = std::distance(result.first, result.second);
Tells you how many items are in the range, to get an element of that range (your pair of chars) you need to
auto random_element = result.first;
auto the_const_char = random_element.first;
auto the_non_const_char = random_element.second;