Hello,
I am parsing a string, and comparing it with a vector of strings.
But, I get an error code because I cannot compare the two types..
How can I compare the two correctly?
Please see my code..
1 2 3 4 5 6 7 8 9 10 11
sprintf(rhs_char,"%s",rhsi->c_str() );
rhs_string=rhs_char;
for (int i=0;i<rhs_string.length();i++)
{
for(j=pi->varList.begin();j!=pi->varList.end();j++)
{
string test = j.c_str();
if(test==rhs_string[i])
printf("\n\t!!!!!!!!!!\n\tHELLO\n\t!!!!!!!!!!!\n");
}
}
The line string test = j.c_str(); is invalid
I even tried string test = j, and char test = j, etc but with no luck.
"The error" is not a sufficient problem description.
But in this case it's obvious - you're trying to compare a string with something that is probably a character.
What do you even want to do?
You don't have to loop through both strings checking each character individually to see if 2 strings are equal, if thats what your trying to o. You can just use the operator= of std::string.
No. I do not want to compare both strings. I want to compare each character.
If the say character 1 in string1 is equal to character 8 in string2.. do the following.
contain appropriate strings, you need to specify which strings in each container you're comparing and which characters in those strings you're comparing.
For instance, to test the 8th character in the first string of s2 against the 1st character of the second string in s1:
1 2 3 4
if ( s1[1][0] == s2[0][7] )
{
// do stuff
}
Assuming you have iterators into s1 and s2 at the strings you want to compare:
1 2 3 4 5 6
if ( (*s1_iterator)[0] == (*s2_iterator)[7]) )
// ...
// alternately, using checked access:
if ( s1_iterator->at(0) == s2_iterator->at(7) )
// ...