I'm trying to find the median of vectors of (x,y) points using nth_element
cv::Point2f medOffset;
vector<float> tempOffsetsX = offsetsX;
int medLoc = tempOffsetsX.size()/2;
nth_element(tempOffsetsX.begin(), tempOffsetsX.begin()+medLoc, tempOffsetsX.end());
// sort(tempOffsetsX.begin(), tempOffsetsX.end());
medOffset.x = tempOffsetsX[medLoc];
vector<float> tempOffsetsY = offsetsY;
****** debug out line 1 *********
nth_element(tempOffsetsY.begin(), tempOffsetsY.begin()+medLoc, tempOffsetsY.end());
// sort(tempOffsetsY.begin(), tempOffsetsY.end());
medOffset.y = tempOffsetsY[medLoc];
****** debug out line 2 *********
tempOffsetsX is working just fine but, occasionally, tempOffsetsY is giving very strange results after nth_element. Here is sample output at the marked debug lines
tempOffsetsY1: 5.184135 -1.564125 3.751759 0.221855 -0.742348 1.737648
tempOffsetsY2: -0.742348 -1.564125 -8885092352.000000 -8850636800.000000 0.000000 0.000000
The results are pretty repeatable until I recompile, at which point the specifics change the the general problem remains. Clearly the vector is getting corrupted somehow but I can't think of how.
Also, if I use sort instead of nth_element it works without problem. For debugging, I tried doing a sort and then nth_element which worked just fine. So somehow the reordering that happens inside nth_element is getting messed up but I can't think of how.
Any ideas how this is happening?
edit - More info about my environment. I'm running Arch Linux. I just did a system update. I should note that this same code did work without problem before the update, and this is the first time I've ran it after the update. But that's a gap of several days and I'm always hesitant to point at system libraries for what is usually my own problem.
[]$ uname -r
3.11.6-1-ARCH
[]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-4.8.2/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-install-libiberty --disable-multilib --disable-libssp --disable-werror --enable-checking=release
Thread model: posix
gcc version 4.8.2 (GCC)
[]$ pacman -Qi glibc
Name : glibc
Version : 2.18-8
....
Quite likely you're running into a recent bug in libstdc++, which broke the nth_element function. This has since been fixed, but some Linux releases shipped with the broken version (e.g. Ubuntu 13.10)
A patch and discussion of the bug can be found on the GCC tracker: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58800