substr and find of string
-
Hello everyone, Just two bacis questions about how to program in an elegant way. 1. About string.find, whether using string::npos or -1 to check no match find is more elegant? 2. About string.substr, whether checking return value is null or not is an elegant way to know if substr method is successful? thanks in advance, George
-
Hello everyone, Just two bacis questions about how to program in an elegant way. 1. About string.find, whether using string::npos or -1 to check no match find is more elegant? 2. About string.substr, whether checking return value is null or not is an elegant way to know if substr method is successful? thanks in advance, George
1. About string.find, it would be elegant to use string::npos instead of -1. 2. About string.substr, Actually it return basic_string and this class has a function called empty which returns true if the string object contains no characters; false if it has at least one character. I guess this would be good practice.
Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job
-
Hello everyone, Just two bacis questions about how to program in an elegant way. 1. About string.find, whether using string::npos or -1 to check no match find is more elegant? 2. About string.substr, whether checking return value is null or not is an elegant way to know if substr method is successful? thanks in advance, George
"Elegance" has nothing to do with your questions. You need a choice between several syntactically correct choices before you can start thinking about coding "elegance". 1.
string::npos
is correct,-1
is wrong. 2.string::substr
throws an exception if it fails. It will never returnNULL
(assuming your STL implementation isn't broken). -
1. About string.find, it would be elegant to use string::npos instead of -1. 2. About string.substr, Actually it return basic_string and this class has a function called empty which returns true if the string object contains no characters; false if it has at least one character. I guess this would be good practice.
Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job
Thanks Karismatic, 1.
Karismatic wrote:
1. About string.find, it would be elegant to use string::npos instead of -1.
Why? More words please? 2. I have performed more experiments, is it more correct to handle out of range exception? I found if the parameter is not legal, out of range exception will be thrown. I am surprised why each possible exception from substr method is not documented in MSDN? regards, George
-
"Elegance" has nothing to do with your questions. You need a choice between several syntactically correct choices before you can start thinking about coding "elegance". 1.
string::npos
is correct,-1
is wrong. 2.string::substr
throws an exception if it fails. It will never returnNULL
(assuming your STL implementation isn't broken).Thanks markkuk, 1. Why -1 is wrong? From definition it should be the same as npos, any comments? 2. About the exception, I am surprised why each possible exception from substr method is not documented in MSDN? Where could we find what exceptions will be thrown in a specific STL method? regards, George