public member function
<locale>
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, bool& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, long& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, unsigned short& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, unsigned int& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, unsigned long& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, float& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, double& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, long double& val) const;
iter_type get (iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, void*& val) const;
Get numeric value
Parses the sequence of characters between
in and
end for a numerical value, and stores it into
v. For the process, it uses the formatting options selected in the object passed as
io (using its
ios_base::fmtflags value), and updates
err with the error status if necessary.
The extraction of the numerical value follows the same rules as
scanf with the appropriate
format specifier, extracting characters from the sequence until the character extracted cannot be part of a valid numerical expression or
end is reached. The next character in the sequence is pointed by the iterator returned by the function.
If the sequence of characters cannot produce a numerical value according to these rules and those specified by the
numpunct facet of
io's locale, the function sets
err to
ios_base::failbit.
If the function exhausts the sequence of characters (i.e., it reaches
end) during its operations,
ios_base::eofbit is set in
err (both
failbit and
eofbit may be set by an operation).
Otherwise,
ios_base::goodbit is set as
err's value, indicating success.
During its operation, the version of this function in the generic template simply calls the virtual protected member
do_get, which is the member function in charge of performing the actions described above.
Parameters
- in, end
- Iterators pointing to the beginning and ending characters of the sequence. The range used is [in,end), which contains all the characters between in and end, including the character pointed by in but not the character pointed by end.
iter_type is a member alias of the second template parameter of num_get (i.e., the facet's iterator type). This can be any input iterator. By default, this is an istreambuf_iterator, allowing implicit conversions from istream objects.
- str
- Object of a class derived from ios_base (generally an input stream object). It is only used to obtain formatting information.
- err
- Stream error state object, of type ios_base::iostate where the resulting state will be stored.
- val
- Object of a numerical type.
The function stores the value in it if it successfully extracted a number (i.e. if err's failbit flag is not set).
Return value
The next character in the sequence right after where the extraction operation ended.
iter_type is a member alias of the second template parameter of
num_get (i.e., the facet's iterator type).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// num_get example
#include <iostream>
#include <iterator>
#include <string>
#include <locale>
using namespace std;
int main ()
{
locale loc;
ios::iostate state;
float mypi,yourpi;
string number ("3.14159");
// get from string:
use_facet<num_get<char,string::iterator> >(loc).get (number.begin(), number.end(), cin, state, mypi);
cout << "Please, enter PI: ";
// get from istream:
use_facet<num_get<char> >(loc).get (cin, istreambuf_iterator<char>(), cin, state, yourpi);
if ( (mypi-yourpi>0.01) || (mypi-yourpi<-0.01) )
cout << "Wrong!" << endl;
else
cout << "Right!" << endl;
return 0;
}
|
Possible output:
Please, enter PI: 3.14
Right!
|
See also
- num_put::put
- Put numerical value (public member function)
- istream::operator>>
- Extract formatted data (public member function)