修改istringstream中的变量数

时间:2017-04-03 12:03:18

标签: c++ istringstream sstream

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

如果您在早期函数中获得的输入为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),但这不起作用

1 个答案:

答案 0 :(得分:3)

特定解决方案

首先回答您的具体问题:

  

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

C ++中的解决方案是istringstreamwhile循环和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;
    }
}

理论

为什么载体

要保存任意数量的数据并能够访问它,您必须使用动态分配的数组或类似的任何内容。这是因为在编译时你不知道在运行时需要多少变量,因此你不能给它们全部命名。

为什么要流

理论上,流可能是无限长字符串或数据(另请参阅here)。因此,用户输入stream,因为它理论上可能是无限长的。虽然这不适用于实践。

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