您如何知道输入流何时到达一行中的最后一个字? 我正在阅读C++中的IFFILE输入。输入是由标签分隔的一组单词组成的,所以我在读类似于“word1 word2 word3”的东西,比如stream>>w1>>w2>>w3;我需要知道我什么时候能说出最后一句话,那么我该怎么做呢?字数是可变的,但应该始终是偶数。此外,最后一个字是包含最后一个字还是最后一个字?
最简单(也是最常见)的解决方案是使用您如何知道输入流何时到达一行中的最后一个字? 我正在阅读C++中的IFFILE输入。输入是由标签分隔的一组单词组成的,所以我在读类似于“word1 word2 word3”的东西,比如stream>>w1>>w2>>w3;我需要知道我什么时候能说出最后一句话,那么我该怎么做呢?字数是可变的,但应该始终是偶数。此外,最后一个字是包含最后一个字还是最后一个字?,c++,C++,最简单(也是最常见)的解决方案是使用 std::getline,然后使用 std::istringstream: std::string line; while ( std::getline( std::cin, line ) ) { std::istringstream s( line ); std::vector<std::string> words; std::string word; while ( s >> word ) {
std::getline
,然后使用
std::istringstream
:
std::string line;
while ( std::getline( std::cin, line ) ) {
std::istringstream s( line );
std::vector<std::string> words;
std::string word;
while ( s >> word ) {
words.push_back( word );
}
// ...
}
std::字符串行;
while(std::getline(std::cin,line)){
std::IStringStreams s(线);
向量词;
字符串字;
while(s>>word){
单词。推回(单词);
}
// ...
}
最简单(也是最常见)的解决方案是使用
std::getline
,然后使用
std::istringstream
:
std::string line;
while ( std::getline( std::cin, line ) ) {
std::istringstream s( line );
std::vector<std::string> words;
std::string word;
while ( s >> word ) {
words.push_back( word );
}
// ...
}
std::字符串行;
while(std::getline(std::cin,line)){
std::IStringStreams s(线);
向量词;
字符串字;
while(s>>word){
单词。推回(单词);
}
// ...
}
读取ifstream
并使用算法std::copy
将其推送到向量,如下所示:
std::ifstream stream("input.txt");
std::vector<std::string> vec;
//replace stream with std::cin for reading from console
std::copy(std::istream_iterator<std::string>(stream),
std::istream_iterator<std::string>(),
std::back_inserter(vec));
std::ifstream流(“input.txt”);
std::vec;
//用std::cin替换流以从控制台读取
std::copy(std::istream_迭代器(流),
std::istream_迭代器(),
标准:背向插入器(vec);
这需要一个EOF来终止。Ctrl+Z或Ctrl+D取决于windows或linux
根据建议,您可以使用C++11的初始值设定项列表,如下所示:
std::vector<std::string> vec{std::istream_iterator<std::string>{stream},
std::istream_iterator<std::string>{}};
std::vector vec{std::istream_迭代器{stream},
std::istream_迭代器{};
读取ifstream
并使用算法std::copy
将其推送到向量,如下所示:
std::ifstream stream("input.txt");
std::vector<std::string> vec;
//replace stream with std::cin for reading from console
std::copy(std::istream_iterator<std::string>(stream),
std::istream_iterator<std::string>(),
std::back_inserter(vec));
std::ifstream流(“input.txt”);
std::vec;
//用std::cin替换流以从控制台读取
std::copy(std::istream_迭代器(流),
std::istream_迭代器(),
标准:背向插入器(vec);
这需要一个EOF来终止。Ctrl+Z或Ctrl+D取决于windows或linux
根据建议,您可以使用C++11的初始值设定项列表,如下所示:
std::vector<std::string> vec{std::istream_iterator<std::string>{stream},
std::istream_iterator<std::string>{}};
std::vector vec{std::istream_迭代器{stream},
std::istream_迭代器{};
我建议mmap()
该文件。之后,您的虚拟地址空间中就有了整个文件,您可以像一个大的字符数组一样随意地检查它。特别是对于这样的操作,您必须返回几个步骤,这是最合适的方法。作为额外的奖励,它也是最快的…我建议mmap()
文件。之后,您的虚拟地址空间中就有了整个文件,您可以像一个大的字符数组一样随意地检查它。特别是对于这样的操作,您必须返回几个步骤,这是最合适的方法。作为额外的奖励,它也是最快的…使用或@vBx When逐行阅读?在输入失败之前,不应测试eof
。他在寻找行的结尾,而不是文件的结尾。而是逐行阅读,何时使用或@vBx?在输入失败之前,不应测试eof
。他正在查找行的结尾,而不是文件的结尾。最后一个单词是\n还是\n?@higgs241否,在解析流中的标记时,将跳过所有空格字符,包括换行符和回车符。每行的最后一个标记将包含该行的最后一个字。@higgs241std::getline
提取'\n'
,但不会将其放入缓冲区,因此它永远不会出现在s
中。(当然,如果是这样,它将被视为空白,就像空格或制表符一样,因此永远不会出现在单词中)我明白了,我想我只是对单词的提取感到困惑。最后一个单词是包含\n还是\n?@higgs241不,所有空白字符,包括换行符和回车符,将在分析流中的令牌时跳过。每行的最后一个标记将包含该行的最后一个字。@higgs241std::getline
提取'\n'
,但不会将其放入缓冲区,因此它永远不会出现在s
中。(当然,如果有,它将被视为空白,就像一个空格或制表符一样,因此永远不会出现在word
中)我明白了,我想我只是对单词的提取感到困惑。或者简单地使用std::vec{std::istream_迭代器{stream},std::istream_迭代器{}构建向量
。除了采购订单希望逐行阅读单词外,他可以知道一行中有多少单词。这将读取整个文件,并释放他想要捕获的信息。或者简单地使用std::vector vec{std::istream_iterator{stream},std::istream_iterator{}
构建向量。除了PO希望逐行读取单词外,他可以知道行中有多少单词。这会读取整个文件,并释放他想要捕获的信息。它也是不可移植的,他不需要备份。既然istream
能帮他做,他为什么还要做额外的解析呢?@JamesKanze这是POSIX标准。是的,有一些专有系统不支持合理的标准,但它们有类似的设施。我相信有很多方法可以将缓冲区中的任何字符串传递给字符串流或类似的字符串流进行解析,还是我错了?问题的主题非常琐碎,可以自己完成,而不会以任何方式使代码膨胀。我最近自己编写了一个完整的解析器,并将整个文件作为vi的一个长片段进行访问