【问题标题】:Initialization and passing an array初始化并传递数组
【发布时间】:2016-08-31 03:25:21
【问题描述】:

我正在尝试创建一个将使用一维数组进行绘制的程序,但是我很难仅使用一个 cin 语句来初始化数组。用户应该看起来像的示例输入

1<space>2<space>34<space>3<space>2<space>1<space>0<space>10


#include<iostream>
using namespace std;
/*---------------------------------------------------------------------------------------
Prototypes
These are the prototype function(s) that will be used to to draw the row and columns
---------------------------------------------------------------------------------------*/
void draw(int nums);
//---------------------------------------------------------------------------------------
int main(){
    const int MAX = 100;
    int chart[MAX];
    int nums;

    cout << "Enter numbers for the chart" << endl;
    cin >> nums;
    draw(nums);

return 0;
}

void draw(int nums) {
     cout << endl;
     int row;

     for (row = 0; row < nums; ++row) {
         cout << "*" << endl;
     }
}

如何使用给定的示例输入初始化数组,然后将其传递给用于绘制的函数

【问题讨论】:

    标签: c++ arrays function initialization


    【解决方案1】:

    这是一个简单的(也许是不安全的,但为了安全起见再次不要使用 std::cin)实现,它似乎适用于读取数字:

    #include <iostream>
    #include <list>
    #include <sstream>
    int main()
    {
        std::cout << "Input numbers: ";
        // get input line
        std::string input;
        std::getline(std::cin, input);
        std::stringstream ss(input);
        // read numbers
        std::list<int> numbers;
        while(ss) {
            int number;
            ss >> number;
            ss.ignore();
            numbers.push_back(number);
        }
        // display input
        for(const auto number: numbers) {
            std::cout << number << std::endl;
        }
        return 0;
    }
    

    这是一个示例运行:

    $ ./a.out
    Input numbers: 1 2 3 4
    1
    2
    3
    4
    

    【讨论】:

      【解决方案2】:

      我认为您需要解析来解码输入。类似于以下内容:

      void parse(const std::string& input, int output[], int MaxNum)
      {
          // parse the integer from the string to output.
      }
      
      int main(){
          ......
          std::string input;
          cout << "Enter numbers for the chart" << endl;
          cin >> input;
          parse(input, chart, MAX);
          ......
      }
      

      【讨论】:

        【解决方案3】:

        这是一个程序版本,它允许您在stringstream 的帮助下输入一系列数字,其中只有一个cin 行,但唯一的区别是它将输入存储在vector 中。然后它根据输入绘制直方图。

        只需按两次&lt;ENTER&gt; 键,让程序知道您已完成输入数字。

        #include <iostream>
        #include <iterator>
        #include <vector>
        #include <algorithm>
        #include <sstream>
        using namespace std;
        
        vector<int> Vector;
        string line;
        
        void drawchart(int max);
        
        
        int main() {
        
            cout<<"Chart drawing program ( Histogram) \n";
            cout<<"Enter a series of numbers. \n";
            cout<<"Seperate with a space, press <ENTER> TWICE to end input \n";
            cout<<" (e.g  2 3 4 5 6)  >  ";
        
            if(!getline(cin, line)) return 1;
            istringstream iss(line);
        
            copy( istream_iterator<int>(iss), istream_iterator<int>(),  back_inserter(Vector));
        
            copy(Vector.begin(), Vector.end(), ostream_iterator<int>(cout, ", "));
        
            cout<<"\nDrawing chart.. \n\n";
        
            drawchart( Vector.size() );
        
        
            cout<<"Press ANY key to close.\n\n";    
            cin.ignore();cin.get();
        
        return 0;
        }
        
        // draws a chart or hjistogram
        void drawchart(int max){
            for( int i = 0; i < max ; i++){
                for(int j = 0; j < Vector[i]; j++)  cout << "*";
                cout << endl;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-17
          • 2020-04-05
          • 1970-01-01
          • 2019-01-08
          • 2017-02-10
          • 1970-01-01
          相关资源
          最近更新 更多