c++ - garbage values for vector push_back -


i'm trying assign array's values vector. seems working fine 1 vector, when second, i'm getting garbage values. cout number , know it's correct, it's not assigning correctly. don't understand because it's working fine first vector.

int sorted[] = {0,1,2,3,4,5,6,7,8,9,10};  // make 2 smaller arrays, untill base case size;  void split(int *datain, int datasize){     // new data broken 2 vectors each half of      // original array. these size firsthalfsize , secondhalfsize.          int firsthalfsize;      int secondhalfsize;          vector<int> firsthalf;      vector<int> secondhalf;          // test see if array in odd or        bool isodd;      if (datasize%2 == 1){         isodd = true;        }else if (datasize%2 == 0){         isodd = false;      }        // determine length of new vectors     // second half firsthalf + 1 if odd.          firsthalfsize = datasize/2;      if (isodd){         secondhalfsize = firsthalfsize + 1;      }else if (!isodd){         secondhalfsize = firsthalfsize;      }                // assign first half of datain[] firsthalf vector         cout << "firs: " << firsthalfsize << endl;     (int = 0; < firsthalfsize; i++){         cout << "a: " << datain[i] << endl;// make sure have right number          firsthalf.push_back(datain[i]);// assign             cout << "v: " << firsthalf[i] << endl;// make sure assigned correctly        }        // same second half       cout << "second: " << secondhalfsize << endl;        (int = firsthalfsize; < (firsthalfsize+secondhalfsize); i++){         cout << "a: " << datain[i] << endl;          secondhalf.push_back(datain[i]);             cout << "v: " << secondhalf[i] << endl;      }     }   int main(void){     split(sorted, sizeof(sorted)/sizeof(int));       return 0; } 

this result. can see first vector push_back went fine , array values (after "a: ") correct.

firs: 5 a: 0 v: 0 a: 1 v: 1 a: 2 v: 2 a: 3 v: 3 a: 4 v: 4 second: 6 a: 5 v: -805306368 a: 6 v: 2 a: 7 v: -805306368 a: 8 v: 0 a: 9 v: 0 a: 10 v: 0 

in second case, indexing firsthalfsize.

you need cout values starting index 0. example:

cout << "v: " << secondhalf[i-firsthalfsize] << endl;  

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -