【问题标题】:Vectors in C++ , bucket sort : Segmentation faultC ++中的向量,桶排序:分段错误
【发布时间】:2016-05-10 07:10:54
【问题描述】:

在我的代码中,我正在尝试实现桶排序,并且在我的实现中我尝试使用向量,但不幸的是,我最终遇到了向量函数方面的错误。

代码:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void bucket_sort(vector<float> & , int); //vector is passed by reference


int main(){

vector<float> array;
float element;
int count;


cout << "\nEnter the size of the vector : ";
cin >> count;

cout << "\nEnter the elements into the vector : ";

for(vector<float>::size_type i = 0 ; i < count ; i++){
    cin >> element;
    array[i].push_back(element);
}

bucket_sort(array , count);
}

void bucket_sort(vector<float> array, int count){

    vector<float> bucket;

    for(int i = 0 ; i < count ; i++){
        int bucket_index = count * array[i];
        bucket[bucket_index].push_back(array[i]);
    }

    for(int i = 0 ; i < count ; i++)
        sort(bucket[i].begin() , bucket[i].end());

    int index = 0;

    for(int i = 0 ; i < count ; i++)
        for(int j = 0 ; j < bucket[i].size() ; j++)
            array[index++].push_back(bucket[i][j]);
}

错误:

bucket_sort.cpp: In function ‘int main()’:
bucket_sort.cpp:24:12: error: request for member ‘push_back’ in ‘array.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(i)’, which is of non-class type ‘float’
   array[i].push_back(element);
            ^
bucket_sort.cpp: In function ‘void bucket_sort(std::vector<float>, int)’:
bucket_sort.cpp:36:24: error: request for member ‘push_back’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)bucket_index))’, which is of non-class type ‘float’
   bucket[bucket_index].push_back(array[i]);
                        ^
bucket_sort.cpp:40:18: error: request for member ‘begin’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
   sort(bucket[i].begin() , bucket[i].end());
                  ^
bucket_sort.cpp:40:38: error: request for member ‘end’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
   sort(bucket[i].begin() , bucket[i].end());
                                      ^
bucket_sort.cpp:45:33: error: request for member ‘size’ in ‘bucket.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)i))’, which is of non-class type ‘float’
   for(int j = 0 ; j < bucket[i].size() ; j++)
                                 ^
bucket_sort.cpp:46:19: error: request for member ‘push_back’ in ‘array.std::vector<_Tp, _Alloc>::operator[]<float, std::allocator<float> >(((std::vector<float>::size_type)(index ++)))’, which is of non-class type ‘float’
    array[index++].push_back(bucket[i][j]);
                   ^
bucket_sort.cpp:46:40: error: invalid types ‘float[int]’ for array subscript
    array[index++].push_back(bucket[i][j]);

编辑代码:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void bucket_sort(vector<float> & , int); //vector is passed by reference


int main(){

vector<float> array;
float element;
int count;


cout << "\nEnter the size of the vector : ";
cin >> count;

cout << "\nEnter the elements into the vector : ";

for(int i = 0 ; i < count ; i++){
    cin >> element;
    array.push_back(element);
}

bucket_sort(array , count);

cout << "\nSorted vector : ";

for(int i = 0 ; i < count ; i++)
    cout << array[i] << " ";
 }

void bucket_sort(vector<float>& array, int count){

vector<float> bucket[count];

for(int i = 0 ; i < count ; i++){
    int bucket_index = count * array[i];
    bucket[bucket_index].push_back(array[i]);
}

for(int i = 0 ; i < count ; i++)
    sort(bucket[i].begin() , bucket[i].end());

int index = 0;

for(int i = 0 ; i < count ; i++)
    for(int j = 0 ; j < bucket[i].size() ; j++)
        array.push_back(bucket[i][j]);

}

编辑: 我已经介绍了关于 push_back() 的更正,但现在在运行我的代码时,我遇到了分段错误。有什么建议吗?

【问题讨论】:

  • 您试图在向量的一个元素上调用push_back,而不是向量本身。
  • 你不能像array[i].push_back..那样做。你不是试图推入向量,而是推入向量的元素float,给你错误。

标签: c++ vector stl


【解决方案1】:

push_backvector 的方法,而不是元素。要附加元素(将向量大小增加 1),请使用 array.push_back(123)。要将某些内容分配给元素,请使用array[i] = 123

如果你想使用赋值填充向量,你必须先调整向量的大小:array.resize(count)

要排序,请写sort(array.begin() , array.end())

bucket[i][j] 完全不正确:bucket 是一维向量。您可能希望 bucket 成为 vector&lt;vector&lt;float&gt;&gt;


for(int i = 0 ; i < count ; i++){
    int bucket_index = count * array[i];
    bucket[bucket_index].push_back(array[i]);
}

bucket 数组中只有 count 元素,但要求 count * array[i] 元素。

【讨论】:

  • 非常感谢!编译成功。
【解决方案2】:

你的代码有很多问题。

主要问题如下:

你不能这样做:

    array[i].push_back(element);

你必须这样做:

    array.push_back(element);

稍后你也这样做:

    bucket[bucket_index].push_back(array[i]);

但这一次,你可能只需要:

    bucket[bucket_index] = array[i];

或者,如果您只想从名为“array”的向量中获取副本,您可以这样做:

    bucket = array;

如果你发表评论bucket_sort应该做什么,我可以提供未来的解释。

最后,我还建议你补充:

    using MyVector = vector<float>;

将保护您的大量输入。

您也可以定义函数以通过引用使用向量,因为否则它会被复制并且您可能不希望这样:

void bucket_sort(MyVector &array, int count);

【讨论】:

    【解决方案3】:

    使用array.push_back(element) 代替array[i].push_back(element)。元素将作为 for 循环索引自动到达该位置。

    因此,在循环结束时,数组将根据您的需要计算元素数量。

    在代码的任何地方都使用这个逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多