Converting vector<string>::const_iterator to character

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.

Any ideas? Thanks.
Last edited on
By dereferencing it?!

string test = *j;
string test = *j; doesn't work either..
Actually, that works, but the comparison doesn't.
The part that says:

if(test==rhs_string[i])

is what encounters the error
"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?
Last edited on
I want to compare the two, if they are equal, I want to execute a function..

So, if I change string test=*j;

to
1
2
3
string test=*j;
char *test_char=&(test[0]);
char *test_char2=&(rhs_string[i]);


I can compare the two, but they never turn out to be equal, even when they are..
You're comparing the addresses of two characters. Obviously, they cannot ever be equal.
This would be correct:
if ((*j)[0]==rhs_string[i])...

If you just care about the first character, why do you have a vector of strings?
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.

1
2
3
4
string one, two;

if (one==two)
something();
Its not the safest way but try casting them i.e.

 
std::string(rhs_string[i]).


Why are you using C style strings to start with?

I avoid C style strings so I'm not an expert but you could also try using stringstream to convert them.

something like:
 
if ( test == CharToString(rhs_string[i]) ) ... 

where
1
2
3
4
5
6
7
#include <sstream>
std::string CharToString(char c) 
{
   std::stringstream ss;
    ss << c;
    return ss.str();
}
Well, I have two vectors of strings, and I want to compare each character in both strings... which explains the two loops
Well, I have two vectors of strings, and I want to compare each character in both strings

In other words, you want to compare both strings.
So do what ModShop said. Get rid of the first loop and compare with if (test==rhs_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.

That's what I meant.
Assuming:

1
2
std::vector<std::string> s1 ;
std::vector<std::string> s2 ;


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) )
    // ... 



Thanks cire.

That's exactly what I was looking for.
Topic archived. No new replies allowed.