std::getline "blends"字符串放在一起
std::getline "blends" strings together
我正试图从.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()
方法现在根据需要输出。
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 错误:在"生成字符串"之前应为";"
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 将图形属性与 std::unique_ptr 捆绑在一起
- 在将字符串放入字符串向量时遇到问题?
- 有没有办法在C++中将字符串和整数相乘?
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 编译器如何将链表中的地址字符串在一起?
- 如何将两个 jlong 数据类型转换为 jstring,然后将两个字符串连接在一起以便从 JNI 将字符串返回给 jav
- 通过字符串迭代并将每个循环中的先前字符组合在一起
- C 将2个字符串合并在一起
- 以简单且更少的代码方式将字符串添加在一起
- C++将三个char*字符串连接在一起
- 在Visual C++中使用MessageBoxW来输出添加在一起的多个字符串
- 试图将字符串的两个元素组合在一起会得到错误的结果(c++)
- 试图创建一个函数,将两个大小不等的字符串向量中的单词连接在一起
- 布尔值转换为字符串,并将boolalpha与一个方法组合在一起,而不是在打印时单独键入它
- 将两个基本字符串连接在一起
- 将2个字符串连接在一起