1

我需要根据从早期输入确定的可修改数量的变量在一行中获取输入。

如果您在较早的函数中获得的输入是 1,则代码将类似于

std::string istring;
std::getline(std::cin, istring);
std::istringstream stream(istring);
while (stream >> a)
{
   statement;
}

是否可以根据您的输入为 while 循环创建条件?因此,如果输入例如为 5,则其行为类似于

while (stream >> a >> a >> a >> a >> a)
{
   statement;
}

我试过做while ((stream>>a)*number),但这不起作用

4

1 回答 1

3

具体解决方案

首先回答您的具体问题:

我需要根据从早期输入确定的可修改数量的变量在一行中获取输入。

C++ 中的解决方案是 a istringstreamwhile循环和 a的组合vector

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

int main() {

    unsigned int earlierInput = 5;  // this is the earlier input
    std::vector<int> numbers;  // The type of vector also controls what
                               // data type you want the user to give
                               // you. Because the stream will try to
                               // the user input into that type.

    while (numbers.size() != earlierInput) {

        // Here we ask the user for input and convert the input
        // to a stream, like that we can try to convert the user
        // input into the data type we want.
        std::string istring;
        std::cout << "Please type in " << earlierInput << " numbers: ";
        std::getline(std::cin, istring);
        std::istringstream reader(istring);

        // read as many numbers as possible.
        for (int number; reader >> number;) {
            numbers.push_back(number);
        }

        if (numbers.size() != earlierInput) {
            // if not enough numbers empty vector and print
            // error message
            std::cout << "Not the right amount of numbers!";
            std::cout << "Try again!" << std::endl;
            numbers.clear();
        }
    }

    // If we get here the user typed in the right amount of numbers
    std::cout << "You typed the following numbers:" << std::endl;
    for (const int& i : numbers) {
        std::cout << i << std::endl;
    }
}

这仅适用于一个用户输入。如果您想询问用户任意次数来执行此操作(例如,您想询问用户 10 次以获得 5 个数字),那么您需要再次应用上述规则并因此叠加:

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

int main() {

    unsigned int amountOfInputs = 2;  // this is the amount of times
                                      // you want the user to type
                                      // something in
    unsigned int earlierInput = 5;  // this is the earlier input


    std::vector<std::vector<int>> allNumbers; // Now we need a vector in
                                // a vector to store the results.
                                // The type of the inner vector also
                                // controls what data type you want the
                                // user to give you.

    while (allNumbers.size() != amountOfInputs) {
        std::vector<int> numbers; // This is the vector as in the example
                                  // above.

        while (numbers.size() != earlierInput) {
            // Here we ask the user for input and convert the input
            // to a stream, like that we can try to convert the user
            // input into the data type we want.
            std::string istring;
            std::cout << "Please type in " << earlierInput << " numbers: ";
            std::getline(std::cin, istring);
            std::istringstream reader(istring);

            // read as many numbers as possible.
            for (int number; reader >> number;) {
                numbers.push_back(number);
            }

            if (numbers.size() != earlierInput) {
                // if not enough numbers empty vector and print
                // error message
                std::cout << "Not the right amount of numbers!";
                std::cout << "Try again!" << std::endl;
                numbers.clear();
            }
        }

        // If we get here the user typed in the right amount of numbers
        // and we can save them and clear the array for using it again.
        allNumbers.push_back(numbers);
        numbers.clear();
    }

    std::cout << "You typed the following numbers:" << std::endl;
    unsigned int round = 1;
    for (auto numbersOfRound : allNumbers) {
        std::cout << "For round " << round++ << ": ";
        for (auto i: numbersOfRound) {
            std::cout << i;
        }
        std::cout << std::endl;
    }
}

理论

为什么是向量

要保存任意数量的数据并能够访问它,您必须使用动态分配的数组或类似的东西。这是因为在编译时您不知道在运行时需要多少变量,因此您无法为所有变量命名。

为什么选择流

从理论上讲,流可能是无限长的字符串或数据(另请参见此处)。因此,用户输入是stream因为理论上它可能是无限长的。尽管这在实践中并不适用。

要从流中提取信息,必须使用>>运算符,也称为提取运算符。此运算符不支持vectors作为 (rhs) 操作数,如此处文档中所述。它仅支持基本数据类型(这就是我们int number在上面的示例中需要一个临时变量的原因)。

于 2017-04-03T12:50:44.110 回答