Thread: Basic String Iteration Help, Best Solution?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Basic String Iteration Help, Best Solution?

    I've written a sort of... parser, for lack of a better term. What this simple function does is take a string, look for ANY AND ALL control codes (denoted by starting with "[" and ending with "]"), and pass them on to a seperate function to be executed, right before deleting them from the string.

    Now, I've noticed that whenever I use string::erase, it throws off any further iterations through the string. Observe:

    Code:
    	string str = "Hey! It's a[1][01 45 00 01] test string.";
    
    	for(uint i = 0; i < str.length(); i++){
    		if(str.at(i) == '['){
    			const uint eb = str.find_first_of(']');
    			if(eb != max_int and eb > i){ //begin capture sequence
    				executeControlCode(str.substr(i + 1, (eb - i) - 1));
    				str.erase(i, (eb - i) + 1);
    			}
    		}
    	}
    Output:

    "Hey! It's a[01 45 00 01] test string."

    It captures the first control code properly, but not the second one... help with this?

    EDIT: Wait a minute, if I place any sort of character between the 1st and 2nd control code, it captures both of them properly; why?!?
    Last edited by Shokwav; 08-02-2012 at 02:38 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    after cutting out the control string i will be incremented by the for loop. If you don't have any chars between control strings i will point to the char past the begin of the next control string.
    simple fix
    Code:
    	for(uint i = 0; i < str.length(); i++){
    		if(str.at(i) == '['){
    			const uint eb = str.find_first_of(']');
    			if(eb != max_int and eb > i){ //begin capture sequence
    				executeControlCode(str.substr(i + 1, (eb - i) - 1));
    				str.erase(i, (eb - i) + 1);
                                    --i; // <<---
    			}
    		}
    	}
    why don't you use string::npos to check for not found ??

    Kurt

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    30
    Yeah, that worked, thanks.

    Oops, I thought npos was private.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also you should not assume that std::string.find_first_of() returns a uint. You should be using a size_t, which is what this function returns. A size_t is not necessarily the same as a unsigned int, it could be any unsigned type.

    Jim

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    30
    Not sure what you mean; size_t typedefs to unsigned int?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    typically, size_t is a typedef (possibly built-in) to unsigned long, and is guaranteed to be at least sizeof(void*) bytes wide, because it must, by definition, be capable of holding on to the size of things in bytes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need solution for assigning char string to malloc
    By killua1234 in forum C Programming
    Replies: 13
    Last Post: 04-21-2010, 01:07 AM
  2. Basic string question
    By pobri19 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2009, 01:17 AM
  3. Really basic string traversal
    By INFERNO2K in forum C Programming
    Replies: 3
    Last Post: 08-02-2007, 01:37 PM
  4. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  5. BASIC problem... no solution?
    By CodeMonkey in forum C++ Programming
    Replies: 10
    Last Post: 01-21-2003, 07:14 PM