To begin with, the solution to this question needs to be very efficient.
My problem is that I need to compare certain parts of two APT_String
s (a DataStage type). Namely, the strings are surnames that contain a space char, such as "Brown Marrow". However, the names are in a 30 byte field, with the remaining space filled with space chars.
To put it short, I need to find the first instance of two consecutive space chars (to signal the end of the useful surname). My solution is
bool foundit = false;
for (int ind = 0; ind<=q_array[i].LAST_NAME.length() &&;foundit; ind++) {
if (q_array[i].LAST_NAME[ind] == ' ' && q_array[i].LAST_NAME[ind+1] == ' ') {
cout<<"two spaces in a row at char " << ind << endl;
foundit = true;
}
}
Unfortunately, APT_String doesn't appear to have a find(), or I would use that. Does anyone have a better or more efficient way to do this?
Again, if you want to know if certain characters are in a string, you need to iterate over it. No other chance, Even find
and strcmp
and whatelse does this. No chance around this loop stuff.
Now, you can always wrap that APT_String
in your own myAPT_String
class which also has a size_t string_size
member, which you need to manage, and use that to jump to the end of the string.
A 30 byte field cannot take long to scan, can it?
If looking for the end of the name, I would start at the end of the string and scan in reverse until the first non-space.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.