S
subramanian100in
I wrote the following program for learning purpose only.
consider the following program named x.cpp :
#include <cstdlib>
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
int main()
{
cout << "numeric_limits< vector<char>::size_type >::max() = "
<< numeric_limits< vector<char>::size_type >::max()
<< endl;
cout << "numeric_limits< vector<char>::difference_type >::max
() = "
<< numeric_limits< vector<char>::difference_type >::max()
<< endl;
vector<char> v((numeric_limits< vector<char>::size_type >::max() -
1)/2 + 3, 100);
cout << "vector size = " << v.size() << endl;
vector<char>::const_iterator first = v.begin();
vector<char>::const_iterator last = v.end();
cout << "vector<char>::difference_type = " << last - first <<
endl;
return EXIT_SUCCESS;
}
I compiled it with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
and ran it under RedHat Linux on a Pentium Dual Core processor-based
machine.
It produced the following output:
numeric_limits< vector<char>::size_type >::max() = 4294967295
numeric_limits< vector<char>::difference_type >::max() = 2147483647
vector size = 2147483650
vector<char>::difference_type = -2147483646
The actual difference between last and first would be the size of the
vector which is 2147483650. Since the vector<type
name>::difference_type is a signed quantity whose maximum on my
machine and the implementation that I am using, is displayed to be
2147483647, I happen to see a wrong value displayed for 'last -
first'.
Why is the difference_type not large enough to hold the distance
between any two iterators ?
Kindly explain.
Thanks
V.Subramanian
consider the following program named x.cpp :
#include <cstdlib>
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
int main()
{
cout << "numeric_limits< vector<char>::size_type >::max() = "
<< numeric_limits< vector<char>::size_type >::max()
<< endl;
cout << "numeric_limits< vector<char>::difference_type >::max
() = "
<< numeric_limits< vector<char>::difference_type >::max()
<< endl;
vector<char> v((numeric_limits< vector<char>::size_type >::max() -
1)/2 + 3, 100);
cout << "vector size = " << v.size() << endl;
vector<char>::const_iterator first = v.begin();
vector<char>::const_iterator last = v.end();
cout << "vector<char>::difference_type = " << last - first <<
endl;
return EXIT_SUCCESS;
}
I compiled it with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
and ran it under RedHat Linux on a Pentium Dual Core processor-based
machine.
It produced the following output:
numeric_limits< vector<char>::size_type >::max() = 4294967295
numeric_limits< vector<char>::difference_type >::max() = 2147483647
vector size = 2147483650
vector<char>::difference_type = -2147483646
The actual difference between last and first would be the size of the
vector which is 2147483650. Since the vector<type
name>::difference_type is a signed quantity whose maximum on my
machine and the implementation that I am using, is displayed to be
2147483647, I happen to see a wrong value displayed for 'last -
first'.
Why is the difference_type not large enough to hold the distance
between any two iterators ?
Kindly explain.
Thanks
V.Subramanian