Hi.
I had to implement a bucket sort algorithm for my class and am running into trouble with vectors.
I've already implemented the program (so I'm not asking for others to do it for me) and have tracked down the bug to my use of the STL vector container.
Code:
Stuct Supplier{...};
...
vector<Supplier> * bucketSort(vector<Supplier> * v, int n, int d){
vector< vector< Supplier > > buckets (N_BUCKETS vector< Supplier > (N_DATA));
...
Supplier tmpS = v->at(i);
buckets.at(str.at(d)).push_back(tmpS);
...
}
Running the program gives me
Code:
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Program received signal SIGABRT, Aborted.
and asking "where" in gdb gives
Code:
Program received signal SIGABRT, Aborted.
0x9003d66c in kill ()
(gdb) where
#0 0x9003d66c in kill ()
#1 0x9010e8cf in raise ()
#2 0x9010d422 in abort ()
#3 0x90b4539c in __gnu_cxx::__verbose_terminate_handler ()
#4 0x90b43602 in __gxx_personality_v0 ()
#5 0x90b43640 in std::terminate ()
#6 0x90b43754 in __cxa_throw ()
#7 0x90b05483 in std::__throw_out_of_range ()
#8 0x000033a7 in std::vector<std::vector<Supplier, std::allocator<Supplier> >, std::allocator<std::vector<Supplier, std::allocator<Supplier> > > >::_M_range_check (this=0xbffff528, __n=57) at /usr/include/c++/4.0.0/bits/stl_vector.h:518
#9 0x000036d0 in std::vector<std::vector<Supplier, std::allocator<Supplier> >, std::allocator<std::vector<Supplier, std::allocator<Supplier> > > >::at (this=0xbffff528, __n=57) at /usr/include/c++/4.0.0/bits/stl_vector.h:536
#10 0x000024db in bucketSort (v=0xbffff810, n=1000, d=3) at bucket.cpp:101
#11 0x00002969 in main (argc=1, argv=0xbffff89c ",???") at bucket.cpp:54
So it seems that I'm getting an out of bounds error on the "push_back" part of
Code:
buckets.at(str.at(d)).push_back(tmpS);
Any ideas?
FYI:
gcc version 4.0.1 (Apple Computer, Inc. build 5367)
GNU gdb 6.3.50-20050815 (Apple version gdb-573)