C++ 使用ignore()跳过getline的/n时出现问题

C++ 使用ignore()跳过getline的/n时出现问题,c++,C++,我注意到一些关于忽略()的帮助。我有一个包含三行文本的文本文件 AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS\n MATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC\n SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.\n 我需要把它读入字符串数组,句子如下: AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR.\n ARAS MAT

我注意到一些关于忽略()的帮助。我有一个包含三行文本的文本文件

AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS\n
MATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC\n
SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.\n
我需要把它读入字符串数组,句子如下:

AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR.\n
ARAS MATTIS IUDICIUM PURUS SIT AMET FERMENTUM.\n
AONEC SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.\n
我得到的是:

AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR\n
TIS IUDICIUM PURUS SIT AMET FERMENTUM\n
D ODIO OPERAE, EU VULPUTATE FELIS RHONCUS\n
有什么想法吗? 谢谢

void sakinais(){
sak=0;
iffv;
fv.open(“tekstas.txt”);

如果(fv.fail())不像最初想象的那么容易

当然,有很多可能的解决方案。我向您展示了3种复杂度越来越高的解决方案,但都使用相同的算法

  • 我们将完整的文本文件读入
    std::string
  • 我们将所有新行“\”替换为空格“”
  • 我们将所有点空间“.”序列替换为“.\n”
  • 我们用定界符把全文分成了几个句子
  • 请注意,第三种解决方案不需要任何循环,而是使用现代C++语言元素和算法。但是,这里不需要解释,因为没有人会使用它。 请参阅:

    
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <iterator>
    #include <regex>
    #include <algorithm>
    
    std::istringstream fv{ R"(AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS
    MATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC
    SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.
    )" };
    
    // either 1 or 2 or 3
    #define COMPLEXITY 1
    
    #if COMPLEXITY == 1
    
    void replace(std::string& text, const std::string search, const std::string& replace) {
        // Search, if the search string is in the text at all
        size_t pos = text.find(search);
    
        // Could we find it?
        while (pos != std::string::npos) {
    
            // We found it. Replace found text
            text.replace(pos, search.length(), replace);
    
            // Check, if there are more search strings in the text
            pos = text.find(search, pos);
        }
    }
    int main() {
    
        // Here we will store the content of the complete text file
        std::string text;
        char c;
    
        // Read all characters from the text file and store them in one strinf
        while (fv.get(c)) 
            text.push_back(c);
    
        // Replace all '\n' by space
        replace(text, "\n", " ");
    
        // Replace all ". " by ".\n"
        replace(text, ". ", ".\n");
    
        // Put in stringstream for extraction with getline
        std::istringstream iss(text);
    
        // Here we will store all sentences
        std::vector<std::string> sentences;
    
        // Read all sentences from stringstream
        std::string line;
        while(std::getline(iss,line)) 
            sentences.push_back(line);
    
        // Show output
        for (const std::string& s : sentences) 
            std::cout << s << "\n";
    
        return 0;
    }
    
    #elif COMPLEXITY == 2
    
    
    std::string& replace(std::string& text, const std::string search, const std::string& replace) {
        for (size_t pos{ text.find(search) }; pos != std::string::npos; pos = text.find(search, pos)) {
            text.replace(pos, search.length(), replace);
        }
        return text;
    }
    
    int main() {
    
        // Here we will store the content of the complete text file
        std::string text;
    
        // Read all characters from the text file and store them in one string
        for (char c{}; fv.get(c); text.push_back(c)) ; // Empty loop body
    
        // Replace all '\n' by space and replace all ". " by ".\n"
        replace(replace(text, "\n", " "), ". ", ".\n");
    
        // Put in stringstream for extraction with getline
        std::istringstream iss(text);
    
        // Here we will store all sentences
        std::vector<std::string> sentences;
    
        // Read all sentences from stringstream
        for (std::string line; std::getline(iss, line); sentences.push_back(line));  // Empty body
    
        // Show output
        for (const std::string& s : sentences)  std::cout << s << "\n";
    
        return 0;
    }
    
    #elif COMPLEXITY == 3
    
    std::regex dotSpace(R"(\. )");
    
    int main() {
    
        // Read the complete text file into one string
        std::string text(std::istreambuf_iterator<char>(fv), {});
    
        // Replace all '\n' by space andf replace all ". " by ".\n"
        text = std::regex_replace(std::regex_replace(text, std::regex("\n"), " "), dotSpace, ".\n");
    
        // Get sentences
        std::vector<std::string> sentences(std::sregex_token_iterator(text.begin(), text.end(), dotSpace, -1), {});
    
        // Show debug output
        std::copy(sentences.begin(), sentences.end(), std::ostream_iterator<std::string>(std::cout);
    
        return 0;
    }
    #endif
    
    
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    std::istringstream fv{R“(环境保护协会)
    紫荆
    在奥迪奥歌剧院,欧盟放纵猫科动物朗卡斯。
    )" };
    //1、2或3
    #定义复杂性1
    #如果复杂性==1
    void replace(std::string&text,const std::string search,const std::string&replace){
    //搜索,如果搜索字符串在文本中
    size\u t pos=text.find(搜索);
    //我们能找到它吗?
    while(pos!=std::string::npos){
    //我们找到了。替换找到的文本
    text.replace(pos,search.length(),replace);
    //如果文本中有更多搜索字符串,请选中
    pos=文本。查找(搜索,pos);
    }
    }
    int main(){
    //在这里,我们将存储完整文本文件的内容
    std::字符串文本;
    字符c;
    //从文本文件中读取所有字符并将其存储在一个strinf中
    while(fv.get(c))
    文本。推回(c);
    //用空格替换所有“\n”
    替换(文本“\n”和“);
    //将所有“.”替换为“.”\n
    替换(文本“.”,“\n”);
    //放入stringstream以使用getline进行提取
    std::istringstream iss(文本);
    //这里我们将存储所有的句子
    向量句;
    //阅读stringstream中的所有句子
    std::字符串行;
    while(std::getline(iss,line))
    句子。向后推(线);
    //显示输出
    for(const std::string&s:句子)
    
    std::cout这里有一种在代码中添加注释的方法来解释这些步骤

    #include <algorithm> // replace_copy
    #include <iostream>
    #include <iterator>  // istreambuf_iterator, back_inserter
    #include <string>
    #include <vector>
    
    #include <sstream> // istringstream for demo
    
    std::vector<std::string> sakiniais(std::istream& fv) {
        std::vector<std::string> retval;
        std::string all;
    
        // copy chars from the istream "fv" into the string "all", replacing '\n' with ' '
        std::replace_copy(
            std::istreambuf_iterator<char>(fv), std::istreambuf_iterator<char>{},
            std::back_inserter(all), '\n', ' ');
    
        // find '.' and store substrings in the vector<string> "retval"
        for(size_t begin=0, end = all.find(".");
            end < all.size();
            begin = end, end = all.find(".", begin))
        {
            ++end; // include '.' in substring
    
            // store substring
            retval.emplace_back(all.substr(begin, end - begin));
    
            // skip space following a '.'
            if(end < all.size() && all[end] == ' ') ++end;
        }
    
        return retval;
    }
    
    int main () {
        std::istringstream file(
            "AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS\n"
            "MATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC\n"
            "SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.\n"
        );
    
        auto strs = sakiniais(file);
    
        for(const std::string& s : strs) {
            std::cout << '>' << s << "<\n"; // show string enclosed with '>' and '<'
        }
    }
    
    #包括//替换#副本
    #包括
    #包括//istreambuf\u迭代器、back\u插入器
    #包括
    #包括
    #包括//用于演示的istringstream
    标准::Sakinais病媒(标准::istream和fv){
    std::向量检索;
    std::字符串all;
    //将istream“fv”中的字符复制到字符串“all”中,将“\n”替换为“”
    标准::替换副本(
    std::istreambuf_迭代器(fv),std::istreambuf_迭代器{},
    标准::背面插入器(全部),'\n','';
    //查找“.”并将子字符串存储在向量“retval”中
    对于(size\u t begin=0,end=all.find(“.”);
    endAONEC使用奥迪奥歌剧,欧盟秃鹫猫科动物RHONCUS<
    
    主要问题是使用
    !eof()
    作为循环条件。
    eof()
    在文件结束后实际尝试读取之前不会返回
    true
    。执行:
    while(getline(fv,sakinai[sak],'.'){…}
    @Ted lynmo在我尝试
    while(getline(fv,sakinai[sak],'.'))之后{fv.ignore('\n');out将要跳过的字符数作为第一个参数。您至少需要2个(
    \n
    )。提供一个
    tekstas.txt
    的示例,您得到的输出和想要的输出。您可以使用
    istringstream
    在代码中创建文本文件。示例:那么,换行符将转换为空格?
    #include <algorithm> // replace_copy
    #include <iostream>
    #include <iterator>  // istreambuf_iterator, back_inserter
    #include <string>
    #include <vector>
    
    #include <sstream> // istringstream for demo
    
    std::vector<std::string> sakiniais(std::istream& fv) {
        std::vector<std::string> retval;
        std::string all;
    
        // copy chars from the istream "fv" into the string "all", replacing '\n' with ' '
        std::replace_copy(
            std::istreambuf_iterator<char>(fv), std::istreambuf_iterator<char>{},
            std::back_inserter(all), '\n', ' ');
    
        // find '.' and store substrings in the vector<string> "retval"
        for(size_t begin=0, end = all.find(".");
            end < all.size();
            begin = end, end = all.find(".", begin))
        {
            ++end; // include '.' in substring
    
            // store substring
            retval.emplace_back(all.substr(begin, end - begin));
    
            // skip space following a '.'
            if(end < all.size() && all[end] == ' ') ++end;
        }
    
        return retval;
    }
    
    int main () {
        std::istringstream file(
            "AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR. ARAS\n"
            "MATTIS IUDICIUM PURUS SIT AMET FERMENTUM. AONEC\n"
            "SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.\n"
        );
    
        auto strs = sakiniais(file);
    
        for(const std::string& s : strs) {
            std::cout << '>' << s << "<\n"; // show string enclosed with '>' and '<'
        }
    }
    
    >AMBITIONI DEDISSE SCRIPSISSE IUDICARETUR.<
    >ARAS MATTIS IUDICIUM PURUS SIT AMET FERMENTUM.<
    >AONEC SED ODIO OPERAE, EU VULPUTATE FELIS RHONCUS.<