在C+中使用向量而不是数组从最小到最大对数字进行排序+; p>所以我是C++的新手,我正在处理这个问题,它要求我用插入排序来排序数字,而不是用数组来排序。我已经使用数组进行了排序,然后我在代码中做了一些小改动,试图解决向量的问题。这是我现在得到的: #include <cstdlib> #include <iostream> #include <vector> using namespace std; void fill_array(vector<int> v,int size, int& number_used); void sort(vector<int> v, int number_used); void swap_values(int& v1, int& v2); int main() { cout << "This program sorts numbers from lowest to highest.\n"; vector<int> sample; int number_used; fill_array(sample,10,number_used); sort(sample,number_used); cout << "In sorted order the numbers are:\n"; for (int index =0; index < number_used; index++) cout << sample[index] << " "; cout << endl; system("PAUSE"); return 0; } void fill_array(vector<int> v, int size, int& number_used) { cout << "This program sorts numbers from lowest to highest.\n"; cout << "Enter up to " << size << " nonnegative whole number.\n" << "Mark the end of the list with a negative number.\n"; int index = 0,next; cin >> next; while ((next >= 0) && (index < size)) { v[index]=next; index++; cin >> next; } number_used = index; } void sort(vector<int> v, int number_used) { int index; int index_backwards; for(index=0;index<number_used;index++) { for(index_backwards=index;index_backwards>0;index_backwards--) { if(v[index_backwards-1]>v[index_backwards]) { swap_values(v[index_backwards-1], v[index_backwards]); } } } } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1=v2; v2=temp; } #包括 #包括 #包括 使用名称空间std; 空洞填充数组(使用向量v、整数大小、整数和数字); 无效排序(向量v,使用整数_); 无效交换_值(int&v1、int&v2); int main() { cout
当你像数组一样使用std::vector时,你是在误用它们。没有必要在向量的“一侧”传递大小,因为向量保持自己的大小 应通过引用传递向量,使用其在C+中使用向量而不是数组从最小到最大对数字进行排序+; p>所以我是C++的新手,我正在处理这个问题,它要求我用插入排序来排序数字,而不是用数组来排序。我已经使用数组进行了排序,然后我在代码中做了一些小改动,试图解决向量的问题。这是我现在得到的: #include <cstdlib> #include <iostream> #include <vector> using namespace std; void fill_array(vector<int> v,int size, int& number_used); void sort(vector<int> v, int number_used); void swap_values(int& v1, int& v2); int main() { cout << "This program sorts numbers from lowest to highest.\n"; vector<int> sample; int number_used; fill_array(sample,10,number_used); sort(sample,number_used); cout << "In sorted order the numbers are:\n"; for (int index =0; index < number_used; index++) cout << sample[index] << " "; cout << endl; system("PAUSE"); return 0; } void fill_array(vector<int> v, int size, int& number_used) { cout << "This program sorts numbers from lowest to highest.\n"; cout << "Enter up to " << size << " nonnegative whole number.\n" << "Mark the end of the list with a negative number.\n"; int index = 0,next; cin >> next; while ((next >= 0) && (index < size)) { v[index]=next; index++; cin >> next; } number_used = index; } void sort(vector<int> v, int number_used) { int index; int index_backwards; for(index=0;index<number_used;index++) { for(index_backwards=index;index_backwards>0;index_backwards--) { if(v[index_backwards-1]>v[index_backwards]) { swap_values(v[index_backwards-1], v[index_backwards]); } } } } void swap_values(int& v1, int& v2) { int temp; temp = v1; v1=v2; v2=temp; } #包括 #包括 #包括 使用名称空间std; 空洞填充数组(使用向量v、整数大小、整数和数字); 无效排序(向量v,使用整数_); 无效交换_值(int&v1、int&v2); int main() { cout,c++,arrays,sorting,vector,C++,Arrays,Sorting,Vector,当你像数组一样使用std::vector时,你是在误用它们。没有必要在向量的“一侧”传递大小,因为向量保持自己的大小 应通过引用传递向量,使用其size()成员函数而不是count(),并使用push_back()向其添加值。函数的签名应如下所示: // Pass by ref; no "size", no "number_used" void fill_array(vector<int> &v); // Pass by ref; no "number_used" void
size()
成员函数而不是count()
,并使用push_back()
向其添加值。函数的签名应如下所示:
// Pass by ref; no "size", no "number_used"
void fill_array(vector<int> &v);
// Pass by ref; no "number_used"
void sort(vector<int> &v);
//按参考传递;无“大小”,无“使用的编号”
空洞填充_数组(向量&v);
//通过参考;无“使用的编号”
无效排序(向量&v);
使用
v.push_-back(next)
而不是v[index]=next
内部fill_-array
实现。在fill_-array
函数中,没有正确添加已输入向量v的值。应将v[index]=next
更改为v.push_-back(next)
您可以更改
v[index]=next;
借
或者您可以将其保持为v[index]=next;并在声明向量时分配内存
vector<int> sample(10); // This allocates 10 element. DON'T use push_back in this case
你能行
std::swap(v[index_backwards-1], v[index_backwards]);
在填充向量之前,您需要在向量中分配空间。如果在函数中操作向量参数,请通过引用传递它们:
void fill_array(使用向量&v,int size,int&number);
OP的fill_array()可能需要一个大小参数,以指定要插入到向量中的值的数量。另一种方法是从cin读取,直到出现错误(例如EOF)但是触发的技术取决于系统,对用户来说并不总是显而易见的。@Peter因为OP提到此代码最初用于数组,所以我可以合理地确定,size
用于说明他通过的数组有多大。这也与size
参数用作数组的方式一致循环中的“限制器”。
vector<int> sample;
sample.reserve(10);
...
// Now fill it with push_back
swap_values(v[index_backwards-1], v[index_backwards]);
std::swap(v[index_backwards-1], v[index_backwards]);