0

Update—this question may be related (Use iterator to call the non-static function in STL Set)

I am creating a program that scans and parses a text file, creates a database, and evaluates queries based on schemes and facts. My data structure is as follows:

Relation
    Scheme
    set<Tuple>

Where Scheme and Tuple inherit from std::vector<std::string>. The Scheme and every Tuple should have the same number of elements, and I repeatedly need to remove the values at a certain index in all three. For example, if I have:

Scheme
    C D H
Tuples
    EE200 F 10AM
    EE200 M 10AM
    EE200 W 1PM

organized so:

  C='EE200' D='F' H='10AM'
  C='EE200' D='M' H='10AM'
  C='EE200' D='W' H='1PM'

After removal at index 0 of every vector I should have:

  D='F' H='10AM'
  D='M' H='10AM'
  D='W' H='1PM'

I have written this code to localize the problem with an example. I first erase the index in the Scheme (which is essentially a vector of strings) and then loop through every Tuple in the set and attempt to remove the value at the index. In my example I forwent creating classes and instead just used std::string or std::vector<std::string>. The problem is here:

    for(set<vector<string>>::iterator t = tempTuples.begin(); t != tempTuples.end(); ++t)
    {
        // how do I erase the element at t?  
    }

Where tempTuples is the set of Tuple objects (here just vectors of strings). (*t).erase((*t).begin()) gives an error (no matching member function for call to 'erase') when inserted at this point. How do I remove the value at an index here?

Community
  • 1
  • 1
intcreator
  • 4,206
  • 4
  • 21
  • 39

1 Answers1

2

If you look at the error, you can see that the problem is that you have a const_iterator. This is because set automatically returns const iterators, as explained in this question.

Community
  • 1
  • 1
vukung
  • 1,824
  • 10
  • 23
  • Okay, so can I fix it by making and modifying a copy and replacing the original element all in my `for` loop? – intcreator Jul 30 '15 at 14:45
  • That would get rid of the error, but copying the vector twice every time you want to remove something seems a bit excessive. Maybe you should try some other data representation. – vukung Jul 30 '15 at 14:52
  • I thought so too, but I'm required to use `std::set` for this project. However, the vectors I'll be copying will be pretty short (only 2–5 strings each) so I'm not that worried about performance in this case. – intcreator Jul 30 '15 at 14:55