gpt4 book ai didi

c++ - 修改 istringstream 中的变量数

转载 作者:行者123 更新时间:2023-11-30 02:26:10 28 4
gpt4 key购买 nike

我需要根据先前输入确定的可变数量的变量在一行中获取输入。

如果你在前面的函数中得到的输入是 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) 但那不起作用

最佳答案

具体解决方案

首先回答你的具体问题:

I need to get input in a line based on a modifiable number of variables that are determined from an earlier input.

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;
}
}

理论

为什么是 vector

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

为什么要流

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

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

关于c++ - 修改 istringstream 中的变量数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43184179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com