Effectivestlnotes 130104012029 Phpapp02
Effectivestlnotes 130104012029 Phpapp02
UTTAM GANDHI,
SOFTWARE DEVELOPER
Pre-requisites
C++
Data Structure
Templates
Smart Pointers
Constant Time, Logarithmic, Linear
¡ Algorithms
¡ Iterators
¡ Retrieving
¡ No slicing
¡ vw.reserve(maxNumWidgets)
¡ Why
¢ Splice operation moves elements from one list to other without
copying elements
¢ If, size operation has to be constant then every list operation
should update size
¢ This means, splice operation will take linear time
void doSomething()
{
vector<Widget*> vwp;
for (int i = 0; i < SOME_MAGIC_NUMBER; ++i)
vwp.push_back(new Widget);
… // use vwp
}
This is a leak
void doSomething()
{
vector<Widget*> vwp;
… // as before
for (vector<Widget*>::iterator i = vwp.begin();
i != vwp.end(),
++i) {
delete *i;
} still there can be leak because of exception
Uttam Gandhi, Software Developer
http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24 Content last revised on 1/4/13
Use delete for new element in container 3/3
void doSomething()
{
typedef boost::shared_ ptr<Widget> SPW;
vector<SPW> vwp;
for (int i = 0; i < SOME_MAGIC_NUMBER; ++i)
vwp.push_back(SPW new Widget)
… // use vwp
} // no Widgets are leaked here, not
// even if an exception is thrown
//in the code above
Uttam Gandhi, Software Developer
http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24 Content last revised on 1/4/13
Choosing Erasing Options
List
¡ c. remove(1963);
Associative container
¡ c.erase(1963);
List
¡ c. remove_if(1963);
AssocContainer<int> c;
for (AssocContainer<int>::iterator i = c.begin(); i !=
c.end(); ){
if (badValue(*i)) c.erase(i++);
else ++i;
}
¡ If there is a match erase is done using i
¡ i is incremented as a side-effect (before erase is executed)
Solution
¡ Disable ref counting by changing pre-processor variable, may
not be portable
¡ Find alternative string implementation
Vector
¡ void doSomething(const int* pInts, size_t numlnts);
¡ Frankly, if you're hanging out with people who tell you to use
v.begin() instead of &v[0], you need to rethink your social
circle.
¡ v can be modified but no new element should be added
String
¡ void doSomething(const char *pString);
¡ doSomething(s.c_str());
vector<Contestant>(contestants).swap(contestants);
¡ ssp.insert(newstring("Anteater"));
¡ ssp.insert(newstring("Wombat"));
¡ ssp.insert(newstring("Penguin"));
struct DereferenceLess {
template <typename PtrType>
bool operator()(PtrType pT1, PtrType pT2) const
value, because we expect them
{
return *pT1 < *pT2;
}
};
map::operator[]
¡ Provides add/update functionality
Update
¡ m[k] = v;
÷ Requires no object creation, deletion
¡ m.insert( IntWidgetMap::value_type(k, v)).first->second = v;
÷ Temporary object of Widget is create, destroyed
Conversions
Inserter
¡ Insert at specified position
partial_sort
¡ e.g. Get best 20 widgets sorted
partition/stable_partition
¡ Partition on a criterion
Efficiency
¡ Algorithms are often more efficient than the loops
programmers produce.
Correctness
¡ Writing loops is more subject to errors than is calling
algorithms.
Maintainability
¡ Algorithm calls often yield code that is clearer and more
straightforward than the corresponding explicit loops.
Efficiency
¡ Check of end() is done only once
Why
¡ Member functions are faster
Example
¡ lower_bound
¡ upper_bound
¡ equal_range
¡ count
binary_search
¡ Widget w; //value to search for
lower_bound
¡ Returns iterators to first match, if not found returns position,
where the element should be inserted
equal_range
¡ Returns pair of iterator pointing to first and last occurrence of
match
¡ If not found, both iterator return same position, where the
element should be inserted
get rid of all the elements in the vector whose value is
less than x, except that elements preceding the last
occurrence of a value at least as big as y should be
retained