C++ 如何在保持整数数组顺序的同时填充整数数组?
我正在尝试做一个简单的练习,我想从用户输入中填充一个int数组,并保持输入有序,这样就不需要在用户完成后对数组进行排序 假设数组的状态如下:{3,5,7,8,9,-,-,-}(-meansempty) 现在,在这种状态下,例如,如果输入6,arr[1]之后的所有元素都应该向前移动一个位置,以便6可以放置在arr[2]中C++ 如何在保持整数数组顺序的同时填充整数数组?,c++,arrays,sorting,console-application,C++,Arrays,Sorting,Console Application,我正在尝试做一个简单的练习,我想从用户输入中填充一个int数组,并保持输入有序,这样就不需要在用户完成后对数组进行排序 假设数组的状态如下:{3,5,7,8,9,-,-,-}(-meansempty) 现在,在这种状态下,例如,如果输入6,arr[1]之后的所有元素都应该向前移动一个位置,以便6可以放置在arr[2]中 #include "stdafx.h" #include <iostream> using namespace std; int main() { bool
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
bool ok = true;
int x // the input
, n = 0 // to keep track of numbers already in the array
, i, j // to iterate in loops
, arr[10];
cout << "Enter 10 numbers: \n";
while (cin >> x) {
if (n == 0) { arr[n] = x; n++; } // for the first entry.
else if (x < arr[0]) { // is x lower than the first element in the array?
for (i = n; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = x; n++;
}
else if (x > arr[n - 1]) { // is x greater than the top of already addded
// elements to the array?
arr[n] = x; n++;
}
else { // when x is in between of elements. Also I think the problem is here.
for (i = 0; i < n && ok; i++)
if (x > arr[i] && x < arr[i + 1]) {
for (j = n; j > i + 1; j--)
arr[j] = arr[j - 1];
ok = false;
}
arr[i + 1] = x; n++;
}
if (n == 10) break; // if you reached to end of the array, break while.
}
for (i = 0; i < 10; i++)
cout << arr[i] << " ";
cin.get();
cin.get();
}
#包括“stdafx.h”
#包括
使用名称空间std;
int main()
{
bool ok=true;
int x//输入
,n=0//以跟踪数组中已存在的数字
,i,j//在循环中迭代
,arr[10];
cout>x){
如果(n==0){arr[n]=x;n++;}//用于第一个条目。
否则,如果(x0;i--)
arr[i]=arr[i-1];
arr[0]=x;n++;
}
如果(x>arr[n-1]){//x大于已添加的
//数组中的元素?
arr[n]=x;n++;
}
else{//当x位于元素之间时。我认为问题就在这里。
对于(i=0;iarr[i]&&xi+1;j--)
arr[j]=arr[j-1];
ok=假;
}
arr[i+1]=x;n++;
}
if(n==10)break;//若到达数组的末尾,则在中断时中断。
}
对于(i=0;i<10;i++)
cout代替原始数组,您可以将std::vector
与std::upper_bound
结合使用,并对每个用户输入使用以下构造:
template<typename T>
typename std::vector<T>::iterator insertInOrder(std::vector<T> &v, T const &val){
auto it = std::upper_bound(v.begin(), v.end(), val);
v.insert(it, val);
return it;
}
模板
typename std::vector::迭代器插入器(std::vector&v,T const&val){
auto it=std::上限(v.begin(),v.end(),val);
v、 插入(it,val);
归还它;
}
我想这是你的家庭作业,你不允许在向量::insert()
中使用向量,也不允许使用其他合适的标准容器或算法
我建议使用一种通用方法来简化算法:尝试在数组中找到x较慢的第一个元素。如果找到,请在正确的位置插入x,否则,将其添加到末尾:
while (cin >> x) {
for (i=0, ok=false; i<n; i++ ) { // iterate for the general case
if (x < arr[i]) { // find first wher x is lower
for (j = n; j>i; j--) // move remaining elements
arr[j] = arr[j - 1];
arr[i] = x;
n++;
ok = true; // we've found the place and inserted x
break;
}
}
if (!ok) { // if we didn't insert until now, we have to add it at the end
if (n<arrsize)
arr[n++] = x;
else cerr << "Array full "<<endl;
}
}
while(cin>>x){
对于(i=0,ok=false;ii;j--)//移动其余元素
arr[j]=arr[j-1];
arr[i]=x;
n++;
ok=true;//我们找到了位置并插入了x
打破
}
}
如果(!ok){//如果我们现在还没有插入,我们必须在末尾添加它
如果(n(arrsize是一个定义为10的常数)。这里的问题是,for循环的递增步骤总是在前一个执行条件为真时执行
for (i = 0; i < n && ok; i++) // i is incremented
if (x > arr[i] && x < arr[i + 1]) {
for (j = n; j > i + 1; j--)
arr[j] = arr[j - 1];
ok = false;
}
arr[i + 1] = x; n++;
您的阵列看起来像:
[1] [10][2]您可以尝试:
for (i = 0; i < n && ok; i++)
if (x > arr[i] && x < arr[i + 1]) {
for (j = n; j > i + 1; j--)
arr[j] = arr[j - 1];
// All elements moved - get out of the for-loop
break;
}
arr[i + 1] = x; n++;
下一个输入是3,if语句将永远不会变为true,程序将失败。数组对于这种事情来说是不好的。。
考虑使用链表,
创建包含值和指向下一个节点的指针的节点类:
class Node{
int number;
Node *nextNode;
}
还有一些代码让List类处理主题,
您会发现在betwin中插入一个节点和两个其他节点要容易得多,
而不是将阵列移动一千次
更好的信息->也许这会有所帮助,所以
#include <iostream>
#include <algorithm>
using namespace std;
void inputInOrder(int *arr, int num){
// Start from the end of array.
// If newly added element is smaller then the element before it swap them.
for(int i(num); i > 0; i--)
if(arr[i] < arr[i-1])
std::swap(arr[i], arr[i-1]);
else break;
}
int main()
{
int n;
cout << "Number of elements: ";
cin >> n;
int *arr = new int[n];
int input(0);
for(int i(0); i < n; ++i){
cout << "Insert element: ";
cin >> input;
arr[i] = input; // you add a new element to the end of array
// call a function that will move newly added element to the right place
inputInOrder(arr, i);
}
for(int i(0); i < n; ++i)
cout << arr[i] << " ";
delete[] arr;
return 0;
}
#包括
#包括
使用名称空间std;
无效输入顺序(int*arr,int num){
//从数组的末尾开始。
//如果新添加的元素较小,则替换之前的元素。
for(int i(num);i>0;i--)
if(arr[i]n;
int*arr=新的int[n];
int输入(0);
for(int i(0);i输入;
arr[i]=input;//在数组末尾添加新元素
//调用将新添加的元素移动到正确位置的函数
输入顺序(arr,i);
}
for(int i(0);i 你所说的是“容器”。你可以用二叉树来做,但最好的是AVL树。你仍然可以这样做,但用树来代替更有效。stl中已经有容器了,请参阅Pustovalov注释。为什么不使用std::set自动排序它的值呢?或者你必须这样做使用C样式数组?std::set arr;while(cin>>x)arr.insert(x)
3行。@pierreemanuellallemant AVL不是最好的。RB更快。@PaulMcKenzie std::multisetKind的人在这里给我写了一些非常好的答案/想法,但我把你的作为答案,因为你指出的是我代码的确切错误。谢谢。是的,break;
!!!谢谢。不,我知道。从一开始我只是瞄准了对于不相等的数字。
if (x > arr[i] && x < arr[i + 1]) {
2, 3, 4
class Node{
int number;
Node *nextNode;
}
#include <iostream>
#include <algorithm>
using namespace std;
void inputInOrder(int *arr, int num){
// Start from the end of array.
// If newly added element is smaller then the element before it swap them.
for(int i(num); i > 0; i--)
if(arr[i] < arr[i-1])
std::swap(arr[i], arr[i-1]);
else break;
}
int main()
{
int n;
cout << "Number of elements: ";
cin >> n;
int *arr = new int[n];
int input(0);
for(int i(0); i < n; ++i){
cout << "Insert element: ";
cin >> input;
arr[i] = input; // you add a new element to the end of array
// call a function that will move newly added element to the right place
inputInOrder(arr, i);
}
for(int i(0); i < n; ++i)
cout << arr[i] << " ";
delete[] arr;
return 0;
}