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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020