【问题标题】:How to read from keyboard a variable number of integers in C++?如何在 C++ 中从键盘读取可变数量的整数?
【发布时间】:2018-12-06 12:38:20
【问题描述】:

我需要从键盘读取可变数量的整数,以便我可以使用它们中的每一个。

首先我认为我可以使用类似的东西

    int myinteger;
    for (int i=0; i<MAX_NUMBER_OF_INTEGERS; i++){
    cin >> myinteger;
    //What I want to do with that Integer
    }

但后来我意识到如果 MAX_NUMBERS_OF_INTEGERS = 10 我必须写 10 个整数。但我想要的是我可以传递“1 2 3”“1 2 3 4”(例如)而不需要写 10 个整数。

【问题讨论】:

  • 恕我直言,最简单的方法是将一个值定义为输入标记的结尾。可能是,假设 -1 不是与您的问题有关的有效整数,则类似于“输入 -1 以完成”。或者,您可以读取整数,直到输入失败,例如for (int value; cin &gt;&gt; value;) { /* store value */ }.
  • 你可以使用 while (cin >> myinteger) { // do something with myinteger } 并让用户在 Linux 上按 ctrl-d 或在 Windows 上按 ctrl-z 来结束输入。
  • 谢谢你们,但我认为这对我不起作用,因为“用户”是检查我的程序是否有效并输出正确结果的在线法官
  • @Uwunt 那么您必须修改您的问题以准确说出您想要的内容。我们 3 个人都认为您不想要固定的最大值,因此我们为您提供了允许用户输入任意数量的数字并表示已完成的方法。
  • @Uwunt 老实说,这仍然很不清楚。你想在哪里传递最大数量?作为函数参数还是您希望用户通过输入输入最大数量?使用我的解决方案,您可以通过键入非数字字符来中止 inout。所以你不必写尽可能多的 MAX 值。如果这不是你想要的,你必须更精确

标签: c++ cin


【解决方案1】:

自从提出这个问题并给出了一个很好的答案后,这个问题似乎发生了一些变化。这只是用来回答新问题。

#include <iostream>
#include <sstream>
#include <vector>

const int MAX_NUMBERS_OF_INTEGERS = 10;

int main() {
    std::string line;
    std::cout << "Enter at most " << MAX_NUMBERS_OF_INTEGERS << " ints, separated by spaces: ";
    std::getline(std::cin, line);

    // create a string stream of the line you entered
    std::stringstream ss(line);

    // create a container for storing the ints
    std::vector<int> myInts;

    // a temporary to extract ints from the string stream
    int myInteger;

    // extract at most MAX_NUMBERS_OF_INTEGERS ints from the string stream
    // and store them in the container
    while(myInts.size()<MAX_NUMBERS_OF_INTEGERS && ss>>myInteger) myInts.push_back(myInteger);

    std::cout << "Extracted " << myInts.size() << " integer(s)\n";

    // loop through the container and print all extracted ints.
    for(int i : myInts) {
        std::cout << i << "\n";
    }
    // ... or access a certain int by index
    if(myInts.size() > 2)
        std::cout << "The third int was: " << myInts[2] << "\n";

}

【讨论】:

    【解决方案2】:
    std::vector<int> read_ints;
    int _temp;
    for(;;)
    {        
        cin >>_temp;
        if(!cin.good()) {
            break;
        }
        else {
            read_ints.push_back(_temp);
        }
    }
    

    我还没有测试过这个解决方案,但是它应该从 cin 读取任意数量的整数,直到你输入一个整数以外的东西。如果您不需要保存结果,也可以跳过矢量部分中的保存。如果您想保存任意数量的整数,这只是相关的。

    编辑:澄清后,您的解决方案可能如下所示:

     int MAX_CHARS = 10;
     int my_int;
     cin >> setw(MAX_CHARS) >> my_int;
    

    setw 限制输入字符的数量,但你必须包含 iomanip 标头

    如果要访问每个数字,请使用此函数将 int 转换为 int 向量:

    vector <int> integerToArray(int x)
    {
        vector <int> resultArray;
        while (true)
        {
            resultArray.insert(resultArray.begin(), x%10);
            x /= 10;
            if(x == 0)
                return resultArray;
        }
     }
    

    然后您可以使用索引访问每个数字,例如第一个数字

     vectory<int> resultArray = integerToArray(my_int);
     int digit = resultArray[0];
    

    Source

    【讨论】:

    • 我认为这对我不起作用,因为我的程序应该能够读取 3 个数字,5 个数字,......并定义了 MAX 并对它们中的每一个进行操作跨度>
    • 我以为您的目标是不指定 MAX 值?至少那是我从你的问题中读到的。如果不是,请说明您的具体目标是什么。
    • 这看起来不错,只有一个问题。如何使用输入的每个数字?
    • @Uwunt 再次更新
    【解决方案3】:

    从一行中读取所有数字的一种方法是将它们限制为最大整数个数,即使用 std::getline() 将该行转换为字符串,然后在循环中使用 istringstream。

    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int main() {
        std::string line; 
        std::getline (std::cin,line); 
        std::istringstream iss(line);
    
        int myInt;
    
        for(int i=0;(iss >> myInt) && (i < MAX_NUMBER_OF_INTEGERS);++i ) {
            std::cout << myInt << ' ';
        }
    
        return 0;
    }
    

    注意:我没有在代码中定义MAX_NUMBER_OF_INTEGERS。我可以在使用之前用const int MAX_NUMBERS_OF_INTEGERS = 10; 定义它,或者可能是预处理器定义甚至是命令行参数。我把这个留给用户。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2022-10-30
      • 2020-05-30
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多