Can and will someone please give me an explanation and some simple examples on the use of the
definition or function?Code:string::size_type
By the way, which is it a definition or a funcition?
Can and will someone please give me an explanation and some simple examples on the use of the
definition or function?Code:string::size_type
By the way, which is it a definition or a funcition?
Last edited by kes103; 04-28-2003 at 11:37 AM.
A function has parameters(or void) and a return type.
Btw, your subject doesn't match your message. This is what MSDN says about size_type:
basic_string::size_type
typedef A::size_type size_type;
The unsigned integer type describes an object that can represent the length of any controlled sequence.
There doesn't appear to be anything called string::string_type.
Last edited by 7stud; 04-28-2003 at 10:55 AM.
I meant to type "size_type". Thanks, I've corrected it now.
So, where are the examples?
Last edited by kes103; 04-28-2003 at 11:45 AM.
Code:string::size_type index = str.find("XXX"); if (index < 0) cerr << "No porn located!\n";
if(index < string::npos) cerr<<"No Vin Desil either" << endl;
int i = str.find("ugly hack");
if(i<0) cerr << "No hacks but this one" << endl;
This is still a little fuzzy to me. The :: (scope resolution operator) tells me (and compiler) that size_type is within the scope of the string class. Considering:
index is a string of size_type, which is big or long enogh to "represent the length of any controlled sequence". Is this the correct interpretation?Code:string::size_type index = str.find("XXX");
and,
"string::npos" in
does what?Code:if(index < string::npos) cerr<<"No Vin Desil either" << endl; int i = str.find("ugly hack"); if(i<0) cerr << "No hacks but this one" << endl;
I would appreciate more explanation and examples.
string::size_type does not mean that size_type is a string.index is a string of size_type
class string is defined this way:
template class basic_string is declared this way:Code:typedef basic_string<char> string;
this is the declaration of type size_type:Code:template<class E, class T = char_traits<E>, class A = allocator<T> > class basic_string { ... };
A is the instantiated type of allocator. template class allocator is used to manage storage allocation and freeing for arrays of type T. allocator<T>::size_type is simply an unsigned int describing the length of the array that the allocator is managing.Code:typedef A::size_type size_type;
this is the definition of npos:
as you can see for yourself, it is simply the value -1.Code:static const size_type npos = -1;
This is almost correct. The implemnetors are taking advantage of the fact that they know size_type is unsigned, and that they are using a 2's compliment system. Thus -1 = 0xFFFFFFFF or the largest possible 32 bit unsigned value. However it's best to forget that you knew this, consider.Originally posted by LuckY
allocator<T>::size_type is simply an unsigned int describing the length of the array that the allocator is managing.
this is the definition of npos:
Code:static const size_type npos = -1;
as you can see for yourself, it is simply the value -1.
unsigned i = -1; // ok(though should warn)
if(i == -1) // true
if(-1 < 0) // true
if(i < 0) // FALSE
A big part of the reason for providing size_type's and npos is so that you don't accedentally introduce unexcpected comparisons between signed and unsigned types.
Code:#include <iostream> using namespace std; using string::size_type; int main() { size_type number = 10; cout<<number<<endl; return 0; }Code:#include <iostream> using namespace std; int main() { string::size_type number = 10; cout<<number<<endl; string sentence = "Matrix Reloaded"; if(sentence.find('y') == string::npos) cout<<"Character not found"<<endl; //string::npos is a large number that's returned //to indicate not found. return 0; }
Last edited by 7stud; 04-29-2003 at 05:33 PM.