push_back没有向vector c ++添加数字

时间:2013-02-10 20:38:47

标签: c++ vector mergesort push-back

我正在尝试调试我的mergesort程序,当我尝试使用“push_back”从向量“left”或“right”向“mergedList”添加值时,这似乎在“merge”函数内的某处出现问题”。以下是gdb调试会话的摘录(后面是我的完整程序代码)

这发生在第一次合并调用时;我能够使用“print left [0]”访问“向左”的值,并且我得到了一个我期望的值(向量“left”中的所有数字都在1-50000之间),但在执行之后当i = 0时的代码:

“mergedList.push_back(左[I]);”然后使用调试器打印mergedList [0],似乎添加left [0]是不成功的。

我误解了push_back吗?还是矢量?对不起,如果我的问题不清楚 - 请告诉我如何改进它!


GDB调试会话:


(gdb) print left[0]
$1 = (int &) @0x100104760: 14108
(gdb) print mergedList[0]
$2 = (int &) @0x7fff5fbfdbf0: 1066800
(gdb) 
(gdb) info locals
t = 0
i = 0
mergedList = {
  <std::_Vector_base<int,std::allocator<int> >> = {
    _M_impl = {
      <std::allocator<int>> = {
        <__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>},
       members of std::_Vector_base<int,std::allocator<int> >::_Vector_impl:
      _M_start = 0x7fff5fbfdbf0,
      _M_finish = 0x7fff5fbfdc68,
      _M_end_of_storage = 0x7fff5fbfdb90
    }
  }, <No data 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);
    }

    }

    return v;
}

vector<int> merge(vector<int> left, vector<int> right){

    int i = 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 PROGRAM



    #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);
        }

    }

    return v;
}

vector<int> merge(vector<int> left, vector<int> right){

    int i = 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++;
        }
    }


    return mergedList;

}

vector<int> sortVector(vector<int> nums){

  int sizeOfNums = nums.size();

    if (sizeOfNums == 1){
        return nums;
    }
    vector<int> left;
    vector<int> right;
    int midpoint = sizeOfNums/2;
    for (int i = 0; i < midpoint; i++){
        left.push_back(nums[i]);

    }
    for (int j = midpoint; j < sizeOfNums; j++){
        right.push_back(nums[j]);
    }
    left = sortVector(left);
    right = sortVector(right);
    return merge(left, right);

}

int main (int argc, char *argv[]) {

    vector<int> nums = getNums();
    vector<int> sorted = sortVector(nums);

    for(int i = 0; i < nums.size(); i++){
      cout << nums[i] <<endl;

    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

此代码缺少错误检查但仍应有效。我认为这种逻辑应该可行。您是否尝试调试此代码,因为它没有生成已排序的放置。最后,当您对元素进行排序时,您不打印已排序的元素,而是打印您阅读的元素。

因为你的gdb会话在向量中有指针的非空值,即_M_start = 0x7fff5fbfdbf0, _M_finish = 0x7fff5fbfdc68, _M_end_of_storage = 0x7fff5fbfdb90这个元素被推入向量。因此,文件必须打开并成功读取。