Tuesday, 15 September 2015

push_back not adding numbers to vector c++ -



push_back not adding numbers to vector c++ -

i'm trying debug mergesort program, seems problematic somewhere within "merge" function when seek utilize "push_back" add together values either vector "left" or "right" "mergedlist". next excerpt gdb debugging session (followed finish programme code below that)

this occurs @ first phone call merge; able access values of "vector left" using "print left[0]" , got value expected (all numbers in vector "left" between 1-50000), after executing line of code when = 0:

"mergedlist.push_back(left[i]);" , using debugger print mergedlist[0], appears adding left[0] unsuccessful.

am misunderstanding push_back? or vectors? sorry if question unclear--please allow me know how improve it!

gdb debugging session:

(gdb) print left[0] $1 = (int &) @0x100104760: 14108 (gdb) print mergedlist[0] $2 = (int &) @0x7fff5fbfdbf0: 1066800 (gdb) (gdb) info locals t = 0 = 0 mergedlist = { <std::_vector_base<int,std::allocator<int> >> = { _m_impl = { <std::allocator<int>> = { <__gnu_cxx::new_allocator<int>> = {<no info fields>}, <no info fields>}, members of std::_vector_base<int,std::allocator<int> >::_vector_impl: _m_start = 0x7fff5fbfdbf0, _m_finish = 0x7fff5fbfdc68, _m_end_of_storage = 0x7fff5fbfdb90 } }, <no info fields>} j = 0 sizeofleft = 1 sizeofright = 1 next = 1 (gdb) -uuu:**-f1 *gud-p1* bot l28 (debugger:run)-------------------------------------- ------------------------------------------------------------------------------------------------------------------ while (iss >> n) { v.push_back(n); } } homecoming v; } vector<int> merge(vector<int> left, vector<int> right){ int = 0; int j = 0; int sizeofleft = left.size(); int sizeofright = right.size(); vector<int> mergedlist; while (i < sizeofleft || j < sizeofright){ if (i < sizeofleft && j < sizeofright){ if (left[i] < right[j]) { mergedlist.push_back(left[i]); => i++; }else{ mergedlist.push_back(right[j]); j++; -uu-:---f1 main.cpp 21% l47 (c++/l abbrev)-------------------------------------------------------------------------------------------------------------------------------------------------------- #complete mergesort programme #include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> using namespace std; vector<int> getnums(){ ifstream infile("/users/christinedeist/documents/algorithms/practice/testproject/integerarray.txt"); string line; vector<int> v; while (getline(infile, line)) { istringstream iss(line); int n; while (iss >> n) { v.push_back(n); } } homecoming v; } vector<int> merge(vector<int> left, vector<int> right){ int = 0; int j = 0; int sizeofleft = left.size(); int sizeofright = right.size(); vector<int> mergedlist; while (i < sizeofleft || j < sizeofright){ if (i < sizeofleft && j < sizeofright){ if (left[i] < right[j]) { mergedlist.push_back(left[i]); i++; }else{ mergedlist.push_back(right[j]); j++; } }else if (i < sizeofleft){ mergedlist.push_back(left[i]); i++; }else if (j < sizeofright){ mergedlist.push_back(right[j]); j++; } } homecoming mergedlist; } vector<int> sortvector(vector<int> nums){ int sizeofnums = nums.size(); if (sizeofnums == 1){ homecoming nums; } vector<int> left; vector<int> right; int midpoint = sizeofnums/2; (int = 0; < midpoint; i++){ left.push_back(nums[i]); } (int j = midpoint; j < sizeofnums; j++){ right.push_back(nums[j]); } left = sortvector(left); right = sortvector(right); homecoming merge(left, right); } int main (int argc, char *argv[]) { vector<int> nums = getnums(); vector<int> sorted = sortvector(nums); for(int = 0; < nums.size(); i++){ cout << nums[i] <<endl; } homecoming 0; }

this code lacks error checking should still work. think logic should work fine. did seek debugging code not producing sorted out put. @ end when sort elements not printing sorted elements ones read.

because gdb session has non-null values pointers in vector i.e. _m_start = 0x7fff5fbfdbf0, _m_finish = 0x7fff5fbfdc68, _m_end_of_storage = 0x7fff5fbfdb90 there elements pushed vector. thus, file must open , read successfully.

c++ vector mergesort push-back

No comments:

Post a Comment