Channels ▼


Living By the Rules: Part II

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.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';

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.