std::getline "blends"字符串放在一起

std::getline "blends" strings together

本文关键字:在一起 字符串 blends std getline      更新时间:2023-10-16

我正试图从.txt文件中读取数据,并将其内容放入每个节点包含两个字符串的链表中。因为.txt文件中的一些字符串包含空格,我宁愿让它们保持原样,也不愿删除空格,所以我使用std::getline()。

我的.txt文件格式如下:

"旅游、娱乐和体育管理"

"TRS"

"人类学"

"蚂蚁"

等等,但每行之间没有空行。链表有一个print()方法,它以以下格式打印数据:data1/data2;data1/data2:

因此,当我打印一个带有data1=="Tourism,Recreation&Sports Management"和data2=="TRS"的节点时,所需的输出是:

旅游、康乐及体育管理;

然而,我实际得到的是:

"TRS"主义,娱乐&体育管理"

但是,当我读取行并将它们分配给字符串,然后打印出这些字符串而不将它们插入链表时,我会得到正确的输出。那是

std::cout << data1 << std::endl;
std::cout << data2 << std::endl;

将正确输出:

"旅游、娱乐和体育管理"
"TRS"

什么东西?

编辑:链表头:

#ifndef _2DLL_H_
#define _2DLL_H_
#include <iostream>
#include <string>

class Llist{
  struct node{
    //node member var
    std::string department;
    std::string abv;
    node * next;
    //node member methods
    std::string search(std::string dep);
    void print();
    void remove(const std::string dep);
    void clear();
    //constructer
  node(const std::string dep , const std::string a):department(dep), abv(a), next(NULL){}
  };// end node
 public:
  //Llist member var
  node * head;
  //LList constructer & destructor
 Llist():head(NULL){}
  ~Llist(){clear();}
  //Llist member functions
  std::string search(const std::string dep);
  void insert(const std::string dep , const std::string a);
  void print();
  void remove(const std::string dep);
  void clear();
  const int operator[](unsigned int index)const;
};// end Llist

#endif //_2DLL_H_

链表.cpp

#include "2DLL.h"
#include <algorithm>

//=========INSERT==============
void Llist::insert(const std::string dep, const std::string a){ //will just prepend. Fuck the police;
  node * n = new node(dep , a);
  n->next = head;
  head = n;
}
//========PRINT=================
void Llist::print(){
  if(head==NULL){
    std::cout << "ERROR: List is empty" << std::endl;
  }
  else{
    head->print();
  }
}
void Llist::node::print(){
  if(next==NULL){
    std::cout << department << ";" << abv << std::endl;
  }
  else{
    std::cout << department << ";" << abv << " / " ;
    std::cout << std::endl;
    next->print();
  }
}
//=======REMOVE========
void Llist::remove(const std::string dep){
  if(head==NULL){
    std::cout << "ERROR: List is empty" << std::endl;
  }
  else{
    head->remove(dep);
  }
}
void Llist::node::remove(const std::string dep){
  if(next->department == dep){
    node * n = next;
    next = n->next;
    delete n;
  }
  else{
    next->remove(dep);
  }
}
//===========CLEAR()==================
void Llist::clear(){
  if(head==NULL){
    std::cout << "ERROR:List is empty" << std::endl;
  }
  else{
    head->clear();
    head = NULL;
  }
}
void Llist::node::clear(){
  if( this==NULL){
    return;    
  }
  else{
    next->clear();
    delete this;
  }
}
//=========OPERATOR=============
/*
const int Llist:: operator[] (unsigned int index) const{
  node * n = head;
  for(int i = 0 ; i < index && n!=NULL ; ++i){
    n=n->next;
  }
  return n->data;
}
*/
//========SEARCH====================
std::string Llist::search(std::string dep){
  if(head == NULL){
    std::cout << "ERROR: List is empty" << std::endl;
    return "ERROR";
  }
  else{
    //dep.erase(std::remove(dep.begin(),dep.end(),' '),dep.end());
    //std::cout << dep << std::endl;
    return head->search(dep);
  }
}
std::string Llist::node::search(std::string dep){
  if(department == dep){
      return abv;
  }
  else{
      return next->search(dep);
    }
  }

读取的实现

#include "genCollege.cpp"
#include "genDepartment.cpp"
#include "2DLL.cpp"
#include <ctime>
#include <fstream>
using namespace std;

int main(){
    std:: ifstream file;
    file.open("DepList.txt");
    std::string department;
    std::string abv;
    srand(time(0));
    /*for (int  i = 0 ; i < 30 ; i++){
        std::string college = genCollege();
        std::string department = genDepartment(college);
        std::cout << "College: "<< college << std::endl;
        std::cout << "t" << "Department: " << department << std::endl;
        std::cout << std::endl;
    }   */
    Llist list;
    while(file.is_open()){
        if(file.eof()){break;};
        std::getline(file , department);
        std::getline(file, abv);
        list.insert(department , abv);
    }
    //file.close();
    list.print();

    return 0 ; 
}

正如用户n.m所建议的那样,似乎因为我正在为Windows读取文本文件并在Ubuntu上运行程序,所以输出看起来是错误的。他逐字逐句地回答:

您为Windows创建了一个以rn作为行终止符的文本文件。您的程序要么在un*x上运行,要么无法以文本模式打开该文件。因此,在每个字符串的末尾都会得到r,这会扰乱您的终端窗口。

他建议我检查使用std::getline()后字符串中的最后一个字符是否为r,如果是,请将其从字符串中删除。在用std::getline() 获取这些字符串后,我只需制作一个子字符串就可以做到这一点

然后,我将新的子字符串插入到链表中,print()方法现在根据需要输出。