我需要更改什么才能使'n'成为数组大小

编程入门 行业动态 更新时间:2024-10-24 16:21:25
本文介绍了我需要更改什么才能使'n'成为数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

#include < iostream > #include < cstdlib > #include < vector > #define SIZE 5 使用 namespace std; class queue { int arr [SIZE]; int front,rear; public : queue() { front = 0 ; rear = 0 ; } void enqueue( int n); void dequeue(); void display(); }; void queue :: enqueue( int n) { if (front ==(rear + 1)%SIZE) cout<< 队列溢出..<< endl<< endl; else { arr [rear] = n; 后++; } } void queue :: dequeue() { if (front == rear) cout<< 队列下溢。先排队。<< endl; else { cout<< 已移除<< arr [front]<< endl; front ++; } } void queue :: display() { cout<< 队列包含:<< endl; for ( int i = front; i< rear; i ++) cout< ;< << arr [i]; cout<< endl; } int main() { queue q; int n,n2; vector< int> SZ; cout<< 输入数组大小:; cin>> N; sz.push_back(n); for ( int i = 0 ; i< sz.size(); i ++) do { cout<< 1.Enqueue< < ENDL; cout<< 2.出列<< endl; cout<< 3.退出<< endl; cout<< 输入选项:; cin>> n2; switch (n2) { case 1 : cout<< 输入数字:; cin>> n; q.enqueue(n); q.display(); break ; case 2 : q.dequeue(); q.display(); break ; case 3 : 断裂; 默认: cout<< 您的选择超出范围。<< endl; break ; } } while (n2!= 3 ); return 0 ; }

大家好,我是c ++的新手,有点混淆这段代码。 如您所见,此程序中数组的大小定义为5.我需要更改的代码部分 n (用户输入)成为数组大小? 这是我用户使用vector输入数组大小的代码。

vector< int> SZ; cout<< 输入数组大小:; cin>> N; sz.push_back(n); for ( int i = 0 ; i< sz.size(); i ++)

对于此代码

for(int i = 0; i< sz.size(); i ++)

我是否需要使用 size_t int 没问题? 我将非常感谢您提供的任何帮助。

解决方案

这是很多方法之一,而且非常直接。 更改

int arr [SIZE];

int * arr = NULL;

cout<< 输入数组大小:; cin>> N; arr =( int *)calloc(n, sizeof ( INT )); for ( int i = 0 ; i< n; i ++) { // 做点什么 arr [i] = i + 1 ; }

别忘了致电

免费(arr);

在你的范围的最后。

使用 C ++ ,我会选择 vector< int> 而不是 C -like数组。你知道,向量会动态增长,所以你不必担心它的上限。

Quote:

vector< int> sz; cout<< 输入数组的大小:; cin>> n; sz.push_back(n); for(int i = 0; i< sz.size(); i ++)

这是错误的( sz.size()是 1 )。改为使用

vector< int> v; // 用于存储整数的'dyanmic array' size_t n; cin>> N; for ( size_t i = 0 ; i< n; ++ i) v.push_back( / * 你喜欢什么int * / );

[update] 如果你真的不想用 vector< int> 然后你必须(如已经建议的那样)动态分配数组,例如

cout<< 输入数组大小:; cin>> N; int * arr = new int [N]; // .. // ...使用数组 // .. // 执行清理 delete [] arr;

[/ update]

#include <iostream> #include <cstdlib> #include <vector> #define SIZE 5 using namespace std; class queue { int arr[SIZE]; int front, rear; public: queue() { front=0; rear=0; } void enqueue(int n); void dequeue(); void display(); }; void queue::enqueue(int n) { if (front==(rear+1)%SIZE) cout<<"Queue overflow.."<<endl<<endl; else { arr[rear]=n; rear++; } } void queue::dequeue() { if(front==rear) cout<<"Queue underflow. Enqueue first."<<endl; else { cout<<"Removed "<<arr[front]<<endl; front++; } } void queue::display() { cout<<"The Queue contains:"<<endl; for(int i=front; i<rear; i++) cout<<" "<<arr[i]; cout<<endl; } int main() { queue q; int n,n2; vector <int> sz; cout << "Enter the size of array: "; cin >> n; sz.push_back(n); for (int i = 0; i<sz.size(); i++) do { cout<<"1. Enqueue"<<endl; cout<<"2. Dequeue"<<endl; cout<<"3. Exit"<<endl; cout<<"Enter choice: "; cin>>n2; switch(n2) { case 1: cout<<"Enter number: "; cin>>n; q.enqueue(n); q.display(); break; case 2: q.dequeue(); q.display(); break; case 3: break; default: cout<<"Your choice is out of range."<<endl; break; } } while(n2!=3); return 0; }

Hi everyone, I'm new to c++ and a little confuse with this code. As you can see, the size of array in this program is define as 5. What part of the codes I need to change to make n (user input) become the array size? Here's my code for array size input by user using vector.

vector <int> sz; cout << "Enter the size of array: "; cin >> n; sz.push_back(n); for (int i = 0; i<sz.size(); i++)

And for this code

for (int i = 0; i<sz.size(); i++)

do I need to use size_t or int is fine? I will be grateful for any help you can provide.

解决方案

This is one of many ways to do it and it is pretty straight forward. Change

int arr[SIZE];

to

int* arr = NULL;

cout << "Enter the size of array: "; cin >> n; arr = (int*)calloc(n, sizeof(int)); for (int i=0; i<n; i++) { // Do something arr[i] = i + 1; }

Don't forget to call

free(arr);

at the end of your scope.

Using C++, I would choose a vector<int> instead of a C-like array. You know, a vector dynamically grows, so you haven't to bother about its upper limit.

Quote:

vector <int> sz; cout << "Enter the size of array: "; cin >> n; sz.push_back(n); for (int i = 0; i<sz.size(); i++)

That is wrong ( sz.size() is 1). Use instead

vector <int> v; // the 'dyanmic array' for storing integers size_t n; cin >> n; for (size_t i=0; i<n; ++i) v.push_back( /* whatever int you like */);

[update] If you really don't want to use vector<int> then you have to (as already suggested) dynamically allocate the array, e.g.

cout << "Enter the size of array: "; cin >> n; int * arr = new int[n]; //.. //... use the array //.. // perform cleanup delete [] arr;

[/update]

更多推荐

我需要更改什么才能使'n'成为数组大小

本文发布于:2023-11-13 00:59:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   能使   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!