我在特定功能中使用stricmp有问题,在其他功能中,除此功能外,它都能正常工作。问题是,即使它比较相同的字符串(char *),也不会返回0。这可能是什么问题?(很抱歉,我会尝试对其进行格式化)是以下代码:
Employee* CityCouncil::FindEmp(list<Employee*> *lst, char* id)
{
bool continue1 = true;
Employee* tmp= NULL;
char* tmpId = NULL, *tmpId2 = NULL;
list<Employee*>::iterator iter = lst->begin();
if ((id == NULL) || (lst->empty()==true))
return NULL;
while ((iter != lst->end()) && (continue1)){
tmp = (Employee*)(*iter);
tmpId = (*tmp).GetId();
if(tmpId != NULL)
{
if(stricmp(tmpId,id) == 0)
continue1 = false;
}
if (continue1 == true)
iter++;
}
if (iter == lst->end())
return NULL;
return (Employee*)(*iter);
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个包含地图的模板.当我尝试创建该模板的实例时,我遇到了构造函数和析构函数的链接问题.另外,当我尝试在main中创建一个实例时,它会在调试时跳过该行,甚至不会在本地列表中显示它.它不编译"DataBase db;" 除非我在db之后添加"()".(这是我尝试在main中启动实例的方式).
代码:
H:
template <class keyVal,class searchVal, class T>
class DataBase
{
private:
map<keyVal,pair<searchVal,T*>*> DB;
public :
DataBase();
virtual ~DataBase();
};
Run Code Online (Sandbox Code Playgroud)
CPP:
#include "DataBase.h"
template <class keyVal,class searchVal, class T>
DataBase<keyVal,searchVal,T>::DataBase()
{}
template <class keyVal,class searchVal, class T>
DataBase<keyVal,searchVal,T>::~DataBase()
{}
Run Code Online (Sandbox Code Playgroud)
谢谢
我尝试比较2个char*相同的字符串,但其中一个字符串末尾包含一个空终结符.我一直在浏览互联网,并明白不推荐删除null终结符char,因为它会使字符串不稳定.我可以使用其他什么方法?
比较功能:
int StringCompare(const char* str1, const char* str2)
{
int size1 = strlen(str1), size2 = strlen(str2), min = 0, index =0;
bool bigger1 = true;
if(size1>size2)
min=size2;
else
min=size1;
for(index=0;index<min;index++)
{
if(str1[index]>str2[index])
return 1;
if(str1[index]<str2[index])
return (-1);
}
if(size1==size2)
return 0;
if(min==size1)
return (-1);
else
return 1;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!