Using find algorithm to identify array cell

Jul 1, 2012 at 2:21pm
Hi guys
I currently have a program with the following string:
string keyboard="qwertyuiopasdfghjkl0zxcvbnm000";

and the second string:
string input="a";

when I try to find the value of input[0] ('a') using the find algorithm I am not able to retrieve the cell in the keyboard string the letter a is saved in:

find(keyboard.begin(), keyboard.end(), input[0]);

if i put * in front of the find function it just returns the letter itself and if i use & instead it returns the memory address of the letter within keyboard.

is there any way i can instead get the cell of keyboard, the letter a is stored in? (using the find function or any other algorithm)

i could just use a for loop running through keyboard in order to search the cell but i think there might be something more efficient in the library :S
greetings

//EDIT:
for the sake of clarity: if i search for 'a' it is supposed to return a value of 10 since a is in the tenth cell of keyboard

//EDIT2:
sorry, this was supposed to go into the beginners forum -.-
Last edited on Jul 1, 2012 at 2:30pm
Jul 1, 2012 at 2:33pm
find return something, you need to assign it to a variable, print it out if you want to see what it is
Jul 1, 2012 at 2:44pm
std::find returns an iterator value of which is equal to input[0] in case of success or keyboard.end() in case of failure.
It is much better to use member functions of class string for this purpose. For example

1
2
3
4
auto pos = keyboard.find( inpuut[0] );

if ( pos != std::string::npos ) std:;cout << "The target character is in position " << pos << std::end;
else std::cout << "Character " << input[0] << " is not found\n";

Last edited on Jul 1, 2012 at 2:45pm
Jul 1, 2012 at 2:46pm
I'm assuming the find you're using is the one found in the algorithm library, which if I'm not misstaken returns an iterator (not the letter) starting at the position of where you can find 'a'.

Most likely the find you want to use is the one built in to the string class
see: http://cplusplus.com/reference/string/string/find/
it will return the index of the first occurence of a given string.
Jul 1, 2012 at 2:48pm
didn't know there was a find in string as well -.- thank you, i will give it a try later

//EDIT:
yep, works just the way i hoped it would ;)
thanks guys
Last edited on Jul 1, 2012 at 2:51pm
Topic archived. No new replies allowed.