Changeset 3404


Ignore:
Timestamp:
Apr 3, 2015, 9:36:12 AM (8 years ago)
Author:
Peter
Message:

new function to reduce capacity in a vector

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility.cc

    r3390 r3404  
    219219  long double xld = 8.0;
    220220  suite.add(suite.equal(utility::log2(xld), 3.0));
     221
     222  std::vector<std::string> vec(1, "text");
     223  utility::clear(vec);
     224  suite.add(vec.size()==0);
    221225
    222226  test_basename(suite);
  • trunk/yat/utility/stl_utility.h

    r3295 r3404  
    466466
    467467  /**
     468     \brief reduce size and capacity to zero
     469
     470     The standard provides a member function clear(void), which clears
     471     the contents of the vector i.e. sets the size to zero. However,
     472     the member function might leave the capacity unchanged and
     473     sometimes, when it's desiribale to save memory usage e.g., it
     474     preferable to use this function, which reduces the capacity to zero.
     475
     476     \since new in yat 0.13
     477   */
     478  template<typename T>
     479  void clear(std::vector<T>& vec)
     480  {
     481    std::vector<T> other;
     482    vec.swap(other);
     483  }
     484
     485
     486  /**
    468487     Same functionality as map::operator[] but the function does not
    469488     modify the map and the function throws if key does not exist in
Note: See TracChangeset for help on using the changeset viewer.