C++ 中的向量,桶排序:分段错误 [英] Vectors in C++ , bucket sort : Segmentation fault
问题描述
在我的代码中,我试图实现桶排序,在我的实现中我尝试使用向量,但不幸的是我最终遇到了向量函数方面的错误.
In my code, I am trying to implement bucket sort and in my implementation I have tried using vectors, but unfortunately I end up with errors with respect to vector functions.
代码:
#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() 的更正,但现在在运行我的代码时,我遇到了分段错误.有什么建议吗?
Edit : I have introduced the correction with respect to the push_back() but now upon running my code I hit a segmentation fault. Any suggestions?
推荐答案
push_back
是 vector
的方法,而不是元素.要追加元素(向量大小增加 1),请使用 array.push_back(123)
.给元素赋值使用 array[i] = 123
.
push_back
is method of vector
, not element. To append element (with increasing vector size by 1) use array.push_back(123)
. To assign something to element use array[i] = 123
.
如果你想用赋值来填充向量,你必须先调整向量的大小:array.resize(count)
.
If you want to fill vector using assignment, you have to resize vector first: array.resize(count)
.
要排序,写sort(array.begin() , array.end())
.
bucket[i][j]
完全不正确:bucket
是一维向量.您可能希望 bucket
成为 vector
.
bucket[i][j]
is totally incorrect: bucket
is one dimensional vector. You probably want bucket
to be vector<vector<float>>
.
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]
元素.
You have only count
elements in bucket
array but ask for count * array[i]
element.
这篇关于C++ 中的向量,桶排序:分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!