我试图从一个.txt文件读取数据,并将其内容放入链接列表中,每个节点包含两个字符串。由于.txt文件中的某些字符串包含空格,因此我宁愿将它们保留为空格而不是空格,因此我使用std :: getline()。std :: getline“混合”在一起的字符串
我格式化为这样的.txt文件:
“旅游,娱乐&体育管理”
“TRS”
“人类学”
“蚁族”
等等,但没有每行之间的空白行。链表有一个print()方法,用这种格式打印数据:data1/data2;数据1 /数据2:
所以,当我打印一个节点与数据1 ==“旅游,娱乐&体育管理”和数据2 ==“TRS”所需的输出是:
“旅游,娱乐&体育管理“/”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 ;
}
5页的散文(格式不好),没有代码?是什么赋予了? – 2014-10-05 21:49:22
听起来像你的节点没有正确地为每个字符串分配独立的存储空间。节点是否包含'std :: string'? '炭[]'? char *'? – 2014-10-05 21:50:40
我想通常使用getline()方法可能只是一个怪癖,而我并不知道。你想看什么代码?您只需要询问:) – 2014-10-05 21:51:44