关于std::string相等的比较问题

anuosix 2009-09-10 05:34:18
有两个string
u和v,运行中的值都是“jane”
然后我用if(u==v)来进行比较,比较结果是不相等,为什么呢?
然后我换了u.compare(v),还是不行,结果还是不正确,望高手告知
注:是unicode模式
...全文
5537 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
anuosix 2009-09-10
  • 打赏
  • 举报
回复
找到原因了,弱智问题:)
struTmp.u=qstrArr[0].toStdWString();
改成
struTmp.u=qstrArr[0].trimmed().toStdWString();
就可以了

谢谢大家,结贴!!!
zgjxwl 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 anuosix 的回复:]
函数的功能是从一个文件中读取每一行(每行包含两个字符串,用逗号隔开),然后统计字符对的个数,我这里是分开统计的,如果一行两个字符串相同,则用一个三元组(u,u,num)表示,不相同使用(u,v,num)表示,u和v代表字符串
[/Quote]

你最好单步跟踪到那里看值到底是不是相同的。
anuosix 2009-09-10
  • 打赏
  • 举报
回复
函数的功能是从一个文件中读取每一行(每行包含两个字符串,用逗号隔开),然后统计字符对的个数,我这里是分开统计的,如果一行两个字符串相同,则用一个三元组(u,u,num)表示,不相同使用(u,v,num)表示,u和v代表字符串
anuosix 2009-09-10
  • 打赏
  • 举报
回复
这是这个函数的全部代码,谢谢大家!!!
void COperatePairs::countPairTimes(const string &strFileName)
{
QFile qfileToRead(QString::fromStdWString(strFileName));
qfileToRead.open(QIODevice::ReadOnly);
qfileToRead.at(0);
QString qstrOldLine("");
QString qstrNewLine("");
QString qstrU;
QString qstrV;
string strU(_T(""));
string strV(_T(""));
int iNum=0;
string strCurrentMode(_T(""));
if (qfileToRead.atEnd()==false)
{

qstrNewLine=qstrOldLine=qfileToRead.readLine();
struTriad struTmp;
QStringList qstrList=qstrNewLine.split(",",QString::SkipEmptyParts);
QString qstrArr[2];
QStringList::Iterator it=qstrList.begin();
int i=0;
for (;it!=qstrList.end();it++)
{
qstrArr[i]=*it;
i++;
}

struTmp.u=qstrArr[0].toStdWString();
struTmp.v=qstrArr[1].toStdWString();
struTmp.iNum=1;
if (struTmp.u==struTmp.v)
{
m_vtrUU.push_back(struTmp);
strCurrentMode=_T("UU");
}
else
{
m_vtrUV.push_back(struTmp);
strCurrentMode=_T("UV");
}
}
while (qfileToRead.atEnd()==false)
{
size_t currentVtrUVSize=m_vtrUV.size();
size_t currentVtrUUSize=m_vtrUU.size();
qstrNewLine=qfileToRead.readLine();
if (qstrNewLine==qstrOldLine)
{
if (strCurrentMode==_T("UU"))
{
m_vtrUU[currentVtrUUSize-1].iNum++;
}
else if (strCurrentMode==_T("UV"))
{
m_vtrUV[currentVtrUVSize-1].iNum++;
}
}
else
{
struTriad struTmp;
QStringList qstrList=qstrNewLine.split(",",QString::SkipEmptyParts);
QString qstrArr[2];
QStringList::Iterator it=qstrList.begin();
int i=0;
for (;it!=qstrList.end();it++)
{
qstrArr[i]=*it;
i++;
}
struTmp.u=qstrArr[0].toStdWString();
struTmp.v=qstrArr[1].toStdWString();
struTmp.iNum=1;

if (struTmp.u.compare(struTmp.v)==0)
{
m_vtrUU.push_back(struTmp);
strCurrentMode=_T("UU");
}
else
{
m_vtrUV.push_back(struTmp);
strCurrentMode=_T("UV");
}

}
qstrOldLine=qstrNewLine;
}
qfileToRead.close();
}
zgjxwl 2009-09-10
  • 打赏
  • 举报
回复
应该不会啊。。。。

// StringEqual.cpp
// compile with: /EHsc
// Illustrates how to use the operator== to test for
// equality of a basic_string variable and a
// null-terminated string. It also illustrates how to
// use the operator== to test for equality of two
// basic_string variables.
//
// Functions:
//
// operator== returns true if the basic_string and the null-
// terminated string are equal.
// operator== returns true if both basic_strings are equal.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

void trueFalse(int x)
{
cout << (x? "True": "False") << endl;
}

int main()
{
string S1="ABC";
string S2="ABC";
string S3="DEF";
string S4; //This specifies an empty initial-controlled sequence.
char CP1[]="abc";
char CP2[]="DEF";
char *CP3 = NULL;

cout << "S1 is " << S1 << endl;
cout << "S2 is " << S2 << endl;
cout << "S3 is " << S3 << endl;
cout << "S4 is" << S4 << endl;
cout << "CP1 is " << CP1 << endl;
cout << "CP2 is " << CP2 << endl;

cout << "S1==CP1 returned ";
trueFalse(S1==CP1); // False (calls function 1)

cout << "S1==CP2 returned ";
trueFalse(S1==CP2); // False (calls function 1)

cout << "CP1==S1 returned ";
trueFalse(CP1==S1); // False (calls function 2)

cout << "CP2==S1 returned ";
trueFalse(CP2==S1); // False (calls function 2)

cout << "S1==S2 returned ";
trueFalse(S1==S2); // True (calls function 3)

cout << "S1==S3 returned ";
trueFalse(S1==S3); // False (calls function 3)

cout << "S1==S4 returned ";
trueFalse(S1==S4); // False (calls function 3)

// Following use of the operator will cause the program to
// crash since CP3 is NULL.
// cout << "S1==CP3 returned ";
// trueFalse(S1==CP3);
}
hua_zhixing_ 2009-09-10
  • 打赏
  • 举报
回复
应该不会吧,把你的代码帖上看看
icosagon 2009-09-10
  • 打赏
  • 举报
回复
先把QString类型的u和v比较一下吧,QString内置是utf-8的编码,有可能u和v字符串来源的编码不同
anuosix 2009-09-10
  • 打赏
  • 举报
回复
struTmp.u=qstrArr[0].toStdWString();
struTmp.v=qstrArr[1].toStdWString();
struTmp.iNum=1;

if (struTmp.u.compare(struTmp.v)==0)
{
m_vtrUU.push_back(struTmp);
strCurrentMode=_T("UU");
}

qstrArr是qt的QString的数组,u和v为结构体struTmp的string类型的成员,这样写出来的代码在执行的时候,明明相等的字符串却不相等
判断相等的那条语句最初使用的是 if(struTmp.u==struTmp.v)
yshuise 2009-09-10
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
std::string u = "jane";
std::string v("jane");
if(u==v)std::cout<<"good"<<std::endl;

return 0;
}

good
请按任意键继续. . .

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧