比较C++中链表中的两个字符串
所以我有这个任务,我需要 1-从341行的文本文件中读取 2-列出341人的姓名前30个字符 3-有些名称是重复的 因此,我试图做的是在创建名称时将其链接到列表之前,检查名称是否已包含在内 在做了一些测试之后,我意识到我比较的两个字符串都作为列表的名字打印出来,似乎忽略了列表的其余部分。当我删除对布尔函数的调用时,列表打印得很好 我是一名学生,正在开始学习,所以我担心我可能会错过一些步骤?非常感谢您的帮助和指导!!多谢各位比较C++中链表中的两个字符串,c++,C++,所以我有这个任务,我需要 1-从341行的文本文件中读取 2-列出341人的姓名前30个字符 3-有些名称是重复的 因此,我试图做的是在创建名称时将其链接到列表之前,检查名称是否已包含在内 在做了一些测试之后,我意识到我比较的两个字符串都作为列表的名字打印出来,似乎忽略了列表的其余部分。当我删除对布尔函数的调用时,列表打印得很好 我是一名学生,正在开始学习,所以我担心我可能会错过一些步骤?非常感谢您的帮助和指导!!多谢各位 #include <iostream> #include &
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>
using namespace std;
struct Pays
{
char nom[20+1]; /* nom du pays */
Pays *suivant; /* pointeur sur le prochain pays */
} ;
struct Emploi
{
char nom[29+1]; /* nom de l’emploi */
Emploi *suivant; /* pointeur sur le prochain emploi */
} ;
struct Espion
{
std:: string nom; /* nom de l’espion(ne) */
Pays *listePays; /* tête de liste des pays visités */
Emploi *listeEmploi; /* tête de liste des emplois occupés */
Espion *suivant; /* pointeur sur le prochain espion */
} ;
bool trouverNomListe(Espion* ptr, std ::string nomAChercher)// returns true if the name is already on the list
{
if(ptr->suivant == NULL)
return false;
else
if(ptr->nom.compare(nomAChercher) == 0){
std:: cout<< ptr->nom << std::endl;// checking if the function is comparing the strings
return true;
}
else
trouverNomListe(ptr->suivant, nomAChercher); //keeps looking thru the list
}
int main()
{
const int MAX_NOM = 30, MAX_PAYS = 20, MAX_EMPLOI = 29;
char nomFichier[50] = "espion.txt";
int n = 0;
std:: string infoEspion;
Espion *actu = NULL;
Espion *debut = NULL;
Espion *fin = NULL;
Espion *nouveau = NULL;
std :: ifstream aLire;
aLire.open(nomFichier, std::ios::in);
if(!aLire.is_open()){
exit(EXIT_FAILURE);
}
while(std::getline(aLire, infoEspion))
{
if(debut == NULL){// if the list is empty
nouveau = new Espion;
nouveau -> nom = infoEspion.substr(0,30);
nouveau -> suivant = NULL;
debut = nouveau;
actu = nouveau;
fin = nouveau;
std::cout << actu -> nom<< std::endl;
}
else
if(trouverNomListe(debut, infoEspion.substr(0,30)) == false )
{
nouveau = new Espion;
nouveau -> nom = infoEspion.substr(0,30);
nouveau -> suivant = NULL;
actu -> suivant = nouveau;
fin = nouveau;
actu = nouveau;
std::cout << actu -> nom<< std::endl;
}
}
aLire.close();
system("pause");
return 0;
}
trouverNomListe函数是递归的,这是不正确的。它应该简单地按顺序浏览列表,例如
bool trouverNomListe(Espion* ptr, std ::string nomAChercher)// returns true if the name is already on the list
{
while (ptr)
{
if(ptr->nom.compare(nomAChercher) == 0)
return true;
ptr = ptr->suivant;
}
return false;
}
你的代码还有其他几个问题,但这似乎是主要问题。将字符数组与std::string混合,为什么?这不是我的意图,这是老师给出的。但从我所调查的情况来看,我似乎应该使用字符串。。这就是我想学的。。。