FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
June 01, 2006

Living By the Rules: Part II

(Page 4 of 6)

Library Interface Improvements

Today, this isn't legal:

std::string file_name("test.txt"); std::ofstream file(file_name);

Instead, you have to go through this circumlocution:

std::ofstream file(file_name.c_str());

One of the library changes is to add std::string overloads for all of the C++ Standard Library functions that currently take only char*. You don't have to call c_str to get to the C++ Library functions.

The other interface change is to containers. Under the current Standard, if you need a const_iterator object that points into a container, you sometimes have to write a cast:

vector<int> vec; vec.push_back(1); vec.push_back(2); vec.begin(); // returns vector<int>::iterator ((const vector<int>&)vec).begin(); // returns vector<int>::const_iterator

With the new changes, there's a set of member functions that always returns const_iterator objects:

vec.cbegin(); // returns vector<int>::const_iterator
vec.cend();   // returns vector<int>::const_iterator
vec.crbegin();// returns vector<int>::const_reverse_iterator
vec.crend(); // returns vector<int>::const_reverse_iterator

Floating-Point Improvements

The template numeric_limits<Ty> has a new member, max_digits10. It gives the minimum number of base 10 digits that you need to write if you want to be sure that numbers that differ by numeric_limits<Ty>::epsilon() will be distinguished. For example, if you only write out one digit, then values such as 1.1f and 1.2f will be written as 1., and when you read them back in, they'll have the same value. In this case, two digits are sufficient to distinguish these values. More generally, numeric_limits<Ty>::max_digits10 digits are sufficient to distinguish any two floating-point values that differ by at least epsilon [8].

One problem you may have run into is that there's no easy way, after you've set several floating-point formatting flags, to clear them out so that you can write floating-point values with the default formatting. There's a new manipulator, defaultfloat, that does this for the various floatfield flags:

cout << scientific << 1.0 << defaultfloat << 1.0 << '\n';

Previous Page | 1 Living By the Rules: Part II | 2 Making Common Mistakes Legal | 3 Handy Extensions | 4 Library Interface Improvements | 5 C99 Compatibility | 6 Conclusion Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK