Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
basic_string::difference_type

basic_string::difference_type

A type that provides the difference between two iterators that refer to elements within the same string.

typedef typename allocator_type::difference_type difference_type;

The signed integer type describes an object that can represent the difference between the addresses of any two elements in the controlled sequence.

For type string, it is equivalent to ptrdiff_t.

// basic_string_diff_type.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;
   string str1 ( "quintillion" );
   cout << "The original string str1 is: " << str1 << endl;
   basic_string <char>::size_type indexChFi, indexChLi;

   indexChFi = str1.find_first_of ( "i" );
   indexChLi = str1.find_last_of ( "i" );
   basic_string<char>::difference_type diffi = indexChLi - indexChFi;

   cout << "The first character i is at position: "
        << indexChFi << "." << endl;
   cout << "The last character i is at position: "
        << indexChLi << "." << endl;
   cout << "The difference is: " << diffi << "." << endl;
}
The original string str1 is: quintillion
The first character i is at position: 2.
The last character i is at position: 8.
The difference is: 6.

Header: <string>

Namespace: std

Show:
© 2015 Microsoft