如何访问std :: string类型数据? [英] How to access the std::string type data?

查看:137
本文介绍了如何访问std :: string类型数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CString s=strLine;
int size=s.GetLength();
char* NotesStr;
std::vector<char> notesChar;
int c=0;
for(int n=0;n<size;n++)
{

         while((s[n]!=''.'')&&s[n]!=''T'')
	{
		notesChar.push_back(s[n]);
		c++;
		n++;
	}
	if(s[n]==''.'')
	{
		std::string NotesStr(notesChar.begin(),notesChar.end());
		for(int p=0;p<c;p++)
		{
			notesChar.pop_back();
						
		}
		c=0;
	}
	else if(s[n]==''T'')
	{
		std::string NotesStr(notesChar.begin(),notesChar.end());
		if(NotesStr==''1O'')//error comes here
		{
			MessageBox("Hi");
		}
		for(int p=0;p<=c;p++)
		{
			notesChar.pop_back();
						
		}
		c=0;
		break;
	}
}



上面的代码给了我以下错误
错误C2678:二进制"==":未找到采用std :: string类型的左操作数的运算符(或没有可接受的转换)



The above code is giving my the following error
error C2678: binary "==": no operator found which takes a left-hand operand of type std::string(or there is no acceptable conversion)

推荐答案

如果您通过以下方式进行更改,该怎么办:
what if you change it by:
if(NotesStr=="1O")


编译器错误的原因是,没有用于比较两个std::stringchar类型的值的运算符.如果您的目的确实是比较两个完整的字符串,那么您也应该将右侧的操作数也转换为std::string,因为实际上有一个定义的operator ==用于比较两个类型为std::string的操作数. />
也就是说,您的右侧操作数使用单个连字符,表示单个字符,而不是字符串.而且它甚至都不是有效字符.

您是说一个十进制(或十六进制)值为10的字符吗?如果是这样,则无法将整个字符串与单个字符进行比较,那么您的意图是什么?

还是您说的是字符串"10"?如果是这样,则比较将始终为false,因为要比较的字符串不包含''T'',这就是您之前验证的条件.那么,您要测试的意思是什么?
The reason for the compiler error is that there is no operator for comparing two values of type std::string and char. If your purpose really is to compare two full strings, then you should convert the operand on the right side to std::string as well, as there is in fact a defined operator == for comparing two operands of type std::string.

That said, your right hand side operand uses single hyphens, indicating a single character, not a string. And it isn''t even a valid character.

Did you mean a single character with the decimal (or hexadecimal?) value 10? If so, you cannot compare an entire string with a single character, so what was your intention?

Or did you mean the string "10"? If so, the comparison will always be false, as the string you compare to does not contain a ''T'', and that is the condition you verified just before. So what did you mean to test?


std::string t="1F";
 if(NotesStr.compare(1,t.length(),t))
 {
       MessageBox("Hi");
 }


这有效:)


This works:)


这篇关于如何访问std :: string类型数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆