I am attempting to print out all of the elements in each vector from a multiset of vectors. The build is failing but the error is occurring somewhere in a header file, I’m afraid I don’t really understand the error codes at all. Any help would be greatly appreciated! Here is the error:
error: invalid conversion from 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >* const' to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >*'
And here is the code causing the problems.
multiset<vector < string > > setOfRules;
vector<string> testing,testing2;
testing.push_back("bar");
testing.push_back("foo");
testing2.push_back("foo2");
testing2.push_back("bar2");
setOfRules.insert(testing);
setOfRules.insert(testing2);
for (multiset< vector <string > >::iterator myIterator = setOfRules.begin();
myIterator!=setOfRules.end();
++myIterator)
{
for (vector< string >::iterator myOtherIterator = ( *myIterator ).begin();
myOtherIterator != ( *myIterator ).end();
++myOtherIterator)
{
cout << *myOtherIterator << " " ;
}
cout << endl;
}
C++11 Standard claims that for associative containers where the value type is the same as the key type, both iterator and const_iterator are constant iterators.
So actually
multiset<vector<string>>::iteratorisconst_iterator. The same trick withstd::setiterators – they are all consts.This means that
vector<string>::iterator myOtherIterator = *myIterator.begin()statement will fail because you try obtain non-const iterator from const object (given by*myIteratorwhich isconst).To fix you need to use
vector<string>::const_iterator: