[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi] When should std::distance (not) be used?
From: |
Greg Chicares |
Subject: |
[lmi] When should std::distance (not) be used? |
Date: |
Fri, 20 Jan 2017 10:21:17 +0000 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0 |
AFAICT, std::distance() should be used only when truly needed.
lmi uses std::distance() only twice. Here:
template<typename InputIterator, typename T>
bool each_equal(InputIterator first, InputIterator last, T const& t)
{
return std::distance(first, last) == std::count(first, last, t);
}
it's unavoidable because we have no container, only iterators. But
in multiple_cell_document::parse(), where we push_back() each
element of an xmlwrapp "container" into a std::vector:
xml::const_nodes_view const subelements(i.elements());
v.reserve(std::distance(subelements.begin(), subelements.end()));
for(auto const& j : subelements)
shouldn't xml::const_nodes_view::size() be used instead of distance()
- v.reserve(std::distance(subelements.begin(), subelements.end()));
+ v.reserve(subelements.size());
because it's less verbose than distance(begin, end) and no slower?
Or is there some subtlety I'm missing here?
The related function std::advance() is used only once in lmi:
std::vector<double>::const_iterator corr = CompleteGptCorridor().begin();
LMI_ASSERT
( static_cast<unsigned int>(IssueAge)
<= CompleteGptCorridor().size()
);
std::advance(corr, IssueAge);
GptCorridor.assign
(corr
,CompleteGptCorridor().end()
);
and there I think it's better to do
- std::advance(corr, IssueAge);
GptCorridor.assign
- (corr
+ (corr + IssueAge
,CompleteGptCorridor().end()
);
Making that less generic is IMO advantageous: changing these vectors to
lists would be such a bad idea, that it's good to produce an error if
anyone ever tries to do that.
- [lmi] When should std::distance (not) be used?,
Greg Chicares <=