Thread: string::size_type definition

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    string::size_type definition

    Can and will someone please give me an explanation and some simple examples on the use of the
    Code:
    string::size_type
    definition or function?
    By the way, which is it a definition or a funcition?
    Last edited by kes103; 04-28-2003 at 11:37 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    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.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
    string::size_type index = str.find("XXX");
    if (index < 0)
      cerr << "No porn located!\n";

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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;

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    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:
    Code:
    string::size_type index = str.find("XXX");
    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?

    and,
    "string::npos" in
    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;
    does what?

    I would appreciate more explanation and examples.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    index is a string of size_type
    string::size_type does not mean that size_type is a string.

    class string is defined this way:
    Code:
    typedef basic_string<char> string;
    template class basic_string is declared this way:
    Code:
    template<class E,
        class T = char_traits<E>,
        class A = allocator<T> >
        class basic_string {
        ...
        };
    this is the declaration of type size_type:
    Code:
    typedef A::size_type size_type;
    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.

    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.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.
    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.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM