简体   繁体   中英

Weird error with std::lower_bound

I'm working on a mass spectrometry database for a professor of mine for a paper. This is also my first post here although the site has been very helpful.

I am getting the following error using GNU CPP 4.8 with flags:

CXX = g++
BOOSTDIR = /home/user/boost_1_60_0/ #Uses boost_1_60_0 lirary.
CXXFLAGS = -std=c++11 -g -ggdb -rdynamic -D_GLIBCXX_DEBUG -Wall -Wextra -I$(BOOSTDIR)
LDFLAGS = -L/home/user/boost_1_60_0/stage/lib -lboost_system -lboost_filesystem -lboost_iostreams

Which results in this:

/usr/include/c++/4.8/bits/stl_algo.h:2438:error: elements in iterator range 
    [__first, __last) are not partitioned by the predicate __comp and value     
    __val.

Objects involved in the operation:
iterator "__first" @ 0x0x7ffd8a308c30 {
type = N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIP11cache_tableNSt9__cxx19986vectorIS3_SaIS3_EEEEENSt7__debug6vectorIS3_S7_EEEE (mutable iterator);
  state = dereferenceable (start-of-sequence);
  references sequence with type `NSt7__debug6vectorI11cache_tableSaIS1_EEE' @ 0x0x7ffd8a308c30
}
iterator "__last" @ 0x0x7ffd8a308c60 {
type = N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIP11cache_tableNSt9__cxx19986vectorIS3_SaIS3_EEEEENSt7__debug6vectorIS3_S7_EEEE (mutable iterator);
  state = past-the-end;
  references sequence with type `NSt7__debug6vectorI11cache_tableSaIS1_EEE' @ 0x0x7ffd8a308c60
}
Aborted (core dumped)

Here is the relevant method call:

void db::get_range(double mz_min, double mz_max, double rt_min, double rt_max, std::vector<dp> & ilist){    
    //Get valid rt ranges. //OPTIMIZE
    auto rt_low = std::lower_bound(tlist.begin(), tlist.end(), rt_min, rt_less_than());     
    //Iterate from low end until out of range or end of list.
    for(auto a = rt_low; (a != tlist.end() && (*(*a).begin()).rt_max < rt_max); a++){
        //Get valid mz ranges. //OPTIMIZE                       
        auto mz_low = std::lower_bound((*a).begin(), (*a).end(), mz_min, mz_less_than()); //This call is what is throwing the error.

        for(auto b = mz_low; (b != (*a).end() && (*(*a).rbegin()).mz_max < mz_max); b++){

            std::clock_t access_time = std::clock();
            (*b).get(mz_min, mz_max, rt_min, rt_max, ilist, access_time);
            tqueue.push(std::make_pair(&(*b), access_time));
            trim();
        }
    }
}

Here is some of the supporting code:

        //Comparator to check rt ranges.
        struct rt_less_than{
            bool operator()(std::vector<cache_table> & p, double s) const{ 
                return p[0].rt_max < s; 
            }
            bool operator()(double p, std::vector<cache_table> & s) const{ 
                return p < s[0].rt_max; 
            }
        };

        //Comparator to check mz ranges.
        struct mz_less_than{
            bool operator()(cache_table & p, double s) const{ 
                return p.mz_max < s; 
            }
            bool operator()(double p, cache_table & s) const{ 
                return p < s.mz_max; 
            }
        };

Now this whole thing was working until I had to refactor much of the code. I am not the most experienced with cpp and this error makes it sound like I am not fulfilling some template requirement for the function and yet when I check the class name inside the mz_lower_than() object it appears to be fine. Checking inside the call to mz_lower_than() found that it will throw an error after roughly 90 calls on a vector that is of size 99 for this test set.

Googling this error has found that a few others have seen it but after the first 3 pages it seems no one really had an answer. I get the feeling its an exotic error caused by something extremely simply but I cannot seem to figure out what it would be.

The program is very large so I only pasted what seemed to be the minimum amount of code required to give you an idea of what is going on. I can of course paste more code if required.

Can anyone help me here? Thanks!

You can only run lower_bound on a sequence that's sorted by the same criteria used in your comparison functor. Since you don't show any sort in your code I must assume that your data is unsorted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM