I did this excercise to get better at debugging, so i chose something i knew i could do without to much hassle. I was wrong though, and it bugs me, since it's really a quite simple task.
The program reads in some data from a textfile, sticks it in a vector containing a struct that fits the data, and then i'm trying to sort it ascendingly and print it to the screen, using selection sort, but it's not working. The problem here being that if i try to debug this fairly short program the debugger starts stepping into all the vector functions in the STL, which i obviously do not need to debug, and it makes it impossible for me to maintain overview and debug my own code.
My first question i guess is: How can i tell my debugger not to show me std::vector.size() and std::vector.at() and so on, i don't care about the STL functions and all those templates and long function calls in a small terminal window makes my head dizzy. Is there a way for me to tell the debugger to stay in my own cpp files?
I'm using MingW GCC and GDB on Windows XP 32-bit with MSYS.
The second question is, what is wrong with the code, i looked up some pseudo code on wiki and tried to replicate it using std::vector and c++, but it's not working:
The data.txt file looks like this:Code:#include <fstream> #include <iostream> #include <vector> struct Data { unsigned int DataMember; unsigned int LineNumber; } DataBuffer; int main() { std::ifstream DataStream("data.txt"); std::vector<Data> MyData; while( DataStream >> DataBuffer.LineNumber >> DataBuffer.DataMember ) { MyData.push_back(DataBuffer); std::cout << DataBuffer.LineNumber << " " << DataBuffer.DataMember << std::endl; } std::cout << std::endl << std::endl; unsigned int Size = MyData.size(); unsigned int SmallestElement; for(unsigned int i = 0; i < Size; ++i) { SmallestElement = i; for(unsigned int j = i+1; j < Size-1; ++j) { if(MyData[j].DataMember < MyData[SmallestElement].DataMember) { SmallestElement = j; } } std::swap(MyData[SmallestElement], MyData[i]); } for(unsigned int k = 0; k < Size; ++k) { std::cout << MyData[k].LineNumber << " " << MyData[k].DataMember << std::endl; } return 0; }
...the first digit here being the linenumber, and the second digit is the actual "data" itself.Code:1 1234 2 5678 3 9123 4 4567 5 8912 6 6324 7 9084 8 3287
The output i get on the screen after running it is sorted right in the first 3 or 4 elements, but the last couple of elements are left untouched, so something IS getting std::swapped, but it's not properly sorting the vector.
I'm certain this is something simple, i was certain i could do this easily, guess i was overconfident.
And i know about std::sort() but i want to be able to do something simple like this without using a prewritten function!