【发布时间】: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,给你错误。