MRPT  2.0.1
stl_containers_utils.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <iostream>
12 #include <list>
13 #include <map>
14 #include <set>
15 #include <sstream>
16 
17 namespace mrpt::containers
18 {
19 /** \addtogroup stlext_grp
20  * @{ */
21 
22 /** Returns the index of the value "T" in the container "vect"
23  * (std::vector,std::deque,etc), or string::npos if not found.
24  */
25 template <class T, class CONTAINER>
26 size_t find_in_vector(const T& value, const CONTAINER& vect)
27 {
28  auto last = vect.end();
29  for (auto i = vect.begin(); i != last; ++i)
30  if (*i == value) return std::distance(vect.begin(), i);
31  return std::string::npos;
32 }
33 
34 /** Calls the standard "erase" method of a STL container, but also returns an
35  * iterator to the next element in the container (or ::end if none) */
36 template <class T>
37 inline typename std::list<T>::iterator erase_return_next(
38  std::list<T>& cont, typename std::list<T>::iterator& it)
39 {
40  return cont.erase(it);
41 }
42 //! \overload
43 template <class K, class V>
44 inline typename std::map<K, V>::iterator erase_return_next(
45  std::map<K, V>& cont, typename std::map<K, V>::iterator& it)
46 {
47  typename std::map<K, V>::iterator itRet = it;
48  ++itRet;
49  cont.erase(it);
50  return itRet;
51 }
52 //! \overload
53 template <class K, class V>
54 inline typename std::multimap<K, V>::iterator erase_return_next(
55  std::multimap<K, V>& cont, typename std::multimap<K, V>::iterator& it)
56 {
57  typename std::multimap<K, V>::iterator itRet = it;
58  ++itRet;
59  cont.erase(it);
60  return itRet;
61 }
62 //! \overload
63 template <class T>
64 inline typename std::set<T>::iterator erase_return_next(
65  std::set<T>& cont, typename std::set<T>::iterator& it)
66 {
67  auto itRet = it;
68  ++itRet;
69  cont.erase(it);
70  return itRet;
71 }
72 
73 /**\brief Return a STL container in std::string form.
74  *
75  * \param[in] t Template STL container (e.g. vector)
76  * \return String form of given STL container
77  */
78 template <class T>
79 std::string getSTLContainerAsString(const T& t)
80 {
81  std::stringstream ss;
82  for (auto& e : t) ss << e << ", ";
83  return ss.str();
84 }
85 /**\brief Print the given vector t.
86  *
87  * \param[in] t Template vector
88  */
89 template <class T>
90 void printSTLContainer(const T& t)
91 {
92  std::cout << getSTLContainerAsString(t) << "\n";
93 }
94 /**\brief Print the given STL container of containers t.
95  *
96  * \param[in] t Template STL container (containing other containers)
97  */
98 template <class T>
100 {
101  using namespace std;
102 
103  int i = 0;
104  for (typename T::const_iterator it = t.begin(); it != t.end(); ++i, ++it)
105  {
106  cout << "List " << i + 1 << "/" << t.size() << endl << "\t";
107  printSTLContainer(*it);
108  }
109 }
110 /**\brief Return contents of map in a string representation
111  *
112  * \param[in] m Template map
113  * \param[in] sep String that seperates visually each key and its value.
114  * Defaults to " => "
115  * \return std::string representation of map
116  * */
117 template <class T1, class T2>
118 std::string getMapAsString(
119  const std::map<T1, T2>& m, const std::string& sep = " => ")
120 {
121  std::stringstream ss;
122  for (auto& key_val : m)
123  ss << key_val.first << " => " << key_val.second << "\n";
124  return ss.str();
125 }
126 /**\brief Print the given map m
127  *
128  * \param[in] m Template map
129  */
130 template <class T1, class T2>
131 void printMap(const std::map<T1, T2>& m)
132 {
133  std::cout << getMapAsString(m) << "\n";
134 }
135 
136 /**\brief Deep clear for a std vector container
137  */
138 template <class CONTAINER>
139 void deep_clear(CONTAINER& c)
140 {
141  CONTAINER empty;
142  c.swap(empty);
143 }
144 
145 /** @} */ // end of grouping
146 } // namespace mrpt::containers
std::string getMapAsString(const std::map< T1, T2 > &m, const std::string &sep=" => ")
Return contents of map in a string representation.
STL namespace.
void printSTLContainer(const T &t)
Print the given vector t.
size_t find_in_vector(const T &value, const CONTAINER &vect)
Returns the index of the value "T" in the container "vect" (std::vector,std::deque,etc), or string::npos if not found.
std::list< T >::iterator erase_return_next(std::list< T > &cont, typename std::list< T >::iterator &it)
Calls the standard "erase" method of a STL container, but also returns an iterator to the next elemen...
bool empty() const
Definition: ts_hash_map.h:191
void printMap(const std::map< T1, T2 > &m)
Print the given map m.
void deep_clear(CONTAINER &c)
Deep clear for a std vector container.
void printSTLContainerOfContainers(const T &t)
Print the given STL container of containers t.
std::string getSTLContainerAsString(const T &t)
Return a STL container in std::string form.
double distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
Definition: geometry.cpp:1807



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020