Question

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;
}
Was it helpful?

Solution

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>>::iterator is const_iterator. The same trick with std::set iterators - 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 *myIterator which is const).

To fix you need to use vector<string>::const_iterator:

for(multiset<vector<string>>::const_iterator myIterator = setOfRules.begin(); 
    myIterator != setOfRules.end(); ++myIterator)
{
   for(vector<string>::const_iterator myOtherIterator = *myIterator.begin();     
      myOtherIterator != *myIterator.end(); ++myOtherIterator)
   {
      cout << *myOtherIterator << " ";
   }
   cout << endl;
}

OTHER TIPS

having a plain multiset<XXX>::iterator myIterator is wrong, because if you do a *myIterator = ...; then you'll have wrecked the multiset.

use const_iterator instead

for (multiset< vector <string > >::const_iterator myIterator = setOfRules.begin(); myIterator!=setOfRules.end(); ++myIterator) {

My guess, and it's only a guess because you're not showing all the necessary code to know, is that you're using the wrong iterator type. It appears based on the compiler vomit that you need to be using const_iterator because your objects are constant.

You can't assign to the key part of a associative container (like multiset). This means your vector is const. You need const iterator to iterate it.

The multiset appears to be non-const. You do not need a const iterator to navigate it. The const nature of the value type it contains will protect against assigning to the key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top