[英]How to remove the leading white spaces but keep middle spaces in C++ `ifstream`?
For example, I have a file with the following contents: 例如,我有一个包含以下内容的文件:
Hello John Smith
Hello Jack Brown
OK I love you
Note that each sentence has some leading white spaces. 请注意,每个句子都有一些前导空格。 I want to use
std::fstream
to read them line by line, and want to remove the leading white spaces but keep the spaces between the words in a sentence. 我想使用
std::fstream
逐行读取它们,并想删除开头的空格,但保留句子中单词之间的空格。
My desired output should be as follows: 我期望的输出如下:
Hello John Smith
Hello Jack Brown
OK I love you
I also find this post gives many trivial methods to my question. 我还发现这篇文章为我的问题提供了许多简单的方法。 However, I think none of them is elegant in terms of modern C++.
但是,我认为它们在现代C ++方面都不是优雅的。 Is there any more elegant means?
还有更优雅的方法吗?
std::ifstream file("input.txt");
std::string line;
while(std::getline(file,line))
{
auto isspace = [](unsigned char ch) { return std::isspace(ch); };
//find the first non-space character
auto it = std::find_if_not(line.begin(), line.end(), isspace);
line.erase(line.begin(), it); //erase all till the first non-space
std::cout << line << "\n";
}
Note that we could just pass std::isspace
as third argument to std::find_if_not
, but there are overloads of std::isspace
which causes compilation error — to fix this you can use cast though, as: 请注意,我们可以将
std::isspace
作为第三个参数传递给std::find_if_not
,但是std::isspace
重载会导致编译错误-要解决此问题,您可以使用std::find_if_not
,例如:
auto it = std::find_if_not(line.begin(),
line.end(),
static_cast<int(*)(int)>(std::isspace));
which looks ugly. 看起来很丑。 But because of the function type in the cast, the compiler is able to figure out which overload you intend to use in the code.
但是由于类型转换中的函数类型,编译器能够找出您打算在代码中使用哪个重载 。
As a complement to Nawaz' answer: it's worth pointing out that Boost has a String_Algo library, with (along with a lot of other things) functions like trim
, which will simplify the code a lot. 作为对Nawaz答案的补充:值得指出的是,Boost有一个String_Algo库,它具有(连同很多其他东西)函数
trim
,这将大大简化代码。 If you're doing any text processing at all, and you can't or don't want to use Boost, you should implement something similar yourself for your toolkit (eg a function MyUtils::trim, based on Nawaz' algorithms). 如果您根本不进行任何文本处理,或者您根本无法使用Boost,则应该为自己的工具箱实现类似的功能(例如,基于Nawaz算法的MyUtils :: trim函数)。
Finally, if you may need someday to handle UTF-8 input, then you should look into ICU. 最后,如果您可能需要一天来处理UTF-8输入,则应该研究ICU。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.