public member function of string

Jan 5, 2010 at 9:09am
Is there an easier way of determining what kind of close brace comes first in an input string without converting the string into array?
My code goes like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
		size_t pos0 = input.find("]");
		size_t pos1 = input.rfind("[",pos0);
		size_t pos2 = input.find("}");
		size_t pos3 = input.rfind("[",pos2);
                size_t pos4 = input.find(">");
		size_t pos5 = input.rfind("[",pos4);
		size_t pos6 = input.find(")");
		size_t pos7 = input.rfind("[",pos6);

                if(pos0>pos2)
                    first = pos2;
                else
                    first = pos0;
                if (first >pos4)
                    first = pos4
                ...and so on...

		int posLen = pos0 - pos1 + 1;
		if((pos0!= string::npos)&&(pos1!= string::npos))
			input.erase(pos1,posLen);
		else if((pos0 == string::npos)||(pos1 == string::npos)){
			return false;	
		}
                ... and so on


should I use strncmp for each ],},>,) brace starting from the first character of the input string? Thanks.
Last edited on Jan 5, 2010 at 9:11am
Jan 5, 2010 at 12:45pm
There is an STL algorithm for this:
http://cplusplus.com/reference/algorithm/find_first_of/
Jan 6, 2010 at 3:36am
Thanks moorecm. I used find_first_of method of string.
Jan 6, 2010 at 1:42pm
Even better! Nice find. Here's the link:
http://www.cplusplus.com/reference/string/string/find_first_of/
Topic archived. No new replies allowed.