如何在 C++ 中读取输入字符串,忽略空格
How to read an input string in c++, ignoring spaces
我的任务是创建一个程序,将一种测量单位转换为另一种测量单位。因此,如果用户输入 5 千克,它会将其转换为等值的磅数。要求规定用户输入必须全部在一行中读取,并且输入中任意位置的任意数量的 space 不得消除功能。我的问题是:如果用户在实际双 'mass' 值之间输入一堆 space,然后在测量单位的不同单词之间输入,例如 'short tonnes',我怎样才能消除间距,使其显示为 "shorttonnes"?
到目前为止我的代码是:
double mass; string unit;
cout << "Enter mass: ";
cin >> mass;
cin.ignore();
cin >> unit;
if (unit == "short tonnes" || unit == "sh tn")
{
//convert to long tonnes
}
我的代码可以在输入双质量值之前忽略白色 space,并在输入字符串的第一部分之前忽略白色 space。但我无法弄清楚如何让我的程序读取 "short tonnes" 只是 "shorttonnes" (不管单词之间的 spaces 的数量),以便我可以进行字符串比较和根据需要转换质量值。预先感谢您的帮助,请不要讨厌我,我正处于学习 C++ 的第一周。
再次向您道歉。 Post 编辑,我认为我最初的评论是正确的。按照其他人的建议获取带有 getline
的行。然后将行放在std::istringstream
中。使用 >>
读取 stringstream
中的单词,并将它们与 +
运算符连接起来,直到您组合出您要查找的标记之一。 >>
的默认行为是为您丢弃所有空格。
所以...
std::string line;
while (std::getline(cin, line)) // reads in a line
{
std::istringstream stream(line);
std::string spaceless;
std::string word;
while (stream >> word) // get words on the line without any whitespace
{
spaceless += word; // assemble without spaces
if (spaceless == "shorttonnes")
{
//do something with spaceless
}
else if (spaceless == "Someothertoken")
{
// do something else with spaceless
}
else if ( ... )
{
// rinse, repeat
}
}
}
这是一个简单的、精简的连接示例:https://ideone.com/hGLr6f
您可以使用其他技术代替 if
/else if
链。 std::map
个字符串和函数指针,但这是另一个问题。
std::copy_if()
是做你想做的另一种方式。学习它的工作原理非常值得,因为除了这个特定问题之外,它还可以在许多其他地方找到用途。
如果您编写此 space 删除例程,它将成为您可以重复使用的组件,如果您需要在不同的情况下再次解决此问题。
void StripSpaces(std::string &str)
{
std::vector<char> temp;
temp.resize(str.length());
auto pred = [] (char &c)
{
return c != ' ';
};
auto temp_end = std::copy_if(str.begin(), str.end(), temp.begin(), pred);
str = std::string(temp.begin(), temp_end);
}
这会从您的字符串中就地删除 spaces。
vector<char>
用于保存结果值。它首先调整为输入字符串的长度,知道结果不能比输入长。
pred
是一个 lambda,它只接受一个字符作为参数,returns 一个 bool
当且仅当该字符不是 [=39= 时才为真].
std::copy_if
完成繁重的工作。它会将源字符串中的字符复制到 temp
,但前提是该字符的 pred
returns 为真。它 returns 一个迭代器,指向我们复制的最后一个字符之后的元素。这是,非常方便地用作 temp.end()
的同义词,如果您想将 temp
视为保存结果字符串。
最后一件事是从temp
中的数据重建参数string
。
我的任务是创建一个程序,将一种测量单位转换为另一种测量单位。因此,如果用户输入 5 千克,它会将其转换为等值的磅数。要求规定用户输入必须全部在一行中读取,并且输入中任意位置的任意数量的 space 不得消除功能。我的问题是:如果用户在实际双 'mass' 值之间输入一堆 space,然后在测量单位的不同单词之间输入,例如 'short tonnes',我怎样才能消除间距,使其显示为 "shorttonnes"? 到目前为止我的代码是:
double mass; string unit;
cout << "Enter mass: ";
cin >> mass;
cin.ignore();
cin >> unit;
if (unit == "short tonnes" || unit == "sh tn")
{
//convert to long tonnes
}
我的代码可以在输入双质量值之前忽略白色 space,并在输入字符串的第一部分之前忽略白色 space。但我无法弄清楚如何让我的程序读取 "short tonnes" 只是 "shorttonnes" (不管单词之间的 spaces 的数量),以便我可以进行字符串比较和根据需要转换质量值。预先感谢您的帮助,请不要讨厌我,我正处于学习 C++ 的第一周。
再次向您道歉。 Post 编辑,我认为我最初的评论是正确的。按照其他人的建议获取带有 getline
的行。然后将行放在std::istringstream
中。使用 >>
读取 stringstream
中的单词,并将它们与 +
运算符连接起来,直到您组合出您要查找的标记之一。 >>
的默认行为是为您丢弃所有空格。
所以...
std::string line;
while (std::getline(cin, line)) // reads in a line
{
std::istringstream stream(line);
std::string spaceless;
std::string word;
while (stream >> word) // get words on the line without any whitespace
{
spaceless += word; // assemble without spaces
if (spaceless == "shorttonnes")
{
//do something with spaceless
}
else if (spaceless == "Someothertoken")
{
// do something else with spaceless
}
else if ( ... )
{
// rinse, repeat
}
}
}
这是一个简单的、精简的连接示例:https://ideone.com/hGLr6f
您可以使用其他技术代替 if
/else if
链。 std::map
个字符串和函数指针,但这是另一个问题。
std::copy_if()
是做你想做的另一种方式。学习它的工作原理非常值得,因为除了这个特定问题之外,它还可以在许多其他地方找到用途。
如果您编写此 space 删除例程,它将成为您可以重复使用的组件,如果您需要在不同的情况下再次解决此问题。
void StripSpaces(std::string &str)
{
std::vector<char> temp;
temp.resize(str.length());
auto pred = [] (char &c)
{
return c != ' ';
};
auto temp_end = std::copy_if(str.begin(), str.end(), temp.begin(), pred);
str = std::string(temp.begin(), temp_end);
}
这会从您的字符串中就地删除 spaces。
vector<char>
用于保存结果值。它首先调整为输入字符串的长度,知道结果不能比输入长。
pred
是一个 lambda,它只接受一个字符作为参数,returns 一个 bool
当且仅当该字符不是 [=39= 时才为真].
std::copy_if
完成繁重的工作。它会将源字符串中的字符复制到 temp
,但前提是该字符的 pred
returns 为真。它 returns 一个迭代器,指向我们复制的最后一个字符之后的元素。这是,非常方便地用作 temp.end()
的同义词,如果您想将 temp
视为保存结果字符串。
最后一件事是从temp
中的数据重建参数string
。