具体解决方案
首先回答您的具体问题:
我需要根据从早期输入确定的可修改数量的变量在一行中获取输入。
C++ 中的解决方案是 a istringstream
、while
循环和 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
在上面的示例中需要一个临时变量的原因)。