Main MRPT website > C++ reference for MRPT 1.5.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-2017, 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
17 {
18  namespace utils
19  {
20  /** \addtogroup stlext_grp
21  * @{ */
22 
23  /** Returns the index of the value "T" in the container "vect"
24  * (std::vector,std::deque,etc), or string::npos if not found.
25  */
26  template <class T,class CONTAINER>
27  size_t find_in_vector(const T &value, const CONTAINER &vect)
28  {
29  typename CONTAINER::const_iterator last = vect.end();
30  for (typename CONTAINER::const_iterator i=vect.begin();i!=last;++i)
31  if (*i==value) return std::distance(vect.begin(),i);
32  return std::string::npos;
33  }
34 
35  /** Calls the standard "erase" method of a STL container, but also returns an 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(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>
43  inline typename std::map<K,V>::iterator erase_return_next(std::map<K,V> &cont, typename std::map<K,V>::iterator &it)
44  {
45  typename std::map<K,V>::iterator itRet = it; ++itRet;
46  cont.erase(it);
47  return itRet;
48  }
49  //! \overload
50  template <class K,class V>
51  inline typename std::multimap<K,V>::iterator erase_return_next(std::multimap<K,V> &cont, typename std::multimap<K,V>::iterator &it)
52  {
53  typename std::multimap<K,V>::iterator itRet = it; ++itRet;
54  cont.erase(it);
55  return itRet;
56  }
57  //! \overload
58  template <class T>
59  inline typename std::set<T>::iterator erase_return_next(std::set<T> &cont, typename std::set<T>::iterator &it)
60  {
61  typename std::set<T>::iterator itRet = it; ++itRet;
62  cont.erase(it);
63  return itRet;
64  }
65 
66  /**\brief Return a STL container in std::string form.
67  *
68  * \param[in] t Template STL container (e.g. vector)
69  * \return String form of given STL container
70  */
71  template<class T>
73  using namespace std;
74  stringstream ss;
75  for (typename T::const_iterator it = t.begin(); it != t.end(); ++it) {
76  ss << *it << ", ";
77  }
78  return ss.str();
79  }
80  /**\brief Print the given vector t.
81  *
82  * \param[in] t Template vector
83  */
84  template<class T>
85  void printSTLContainer(const T& t) {
86  using namespace std;
87  cout << getSTLContainerAsString(t) << endl;
88  }
89  /**\brief Print the given STL container of containers t.
90  *
91  * \param[in] t Template STL container (containing other containers)
92  */
93  template<class T>
95  using namespace std;
96 
97  int i = 0;
98  for (typename T::const_iterator it = t.begin();
99  it != t.end(); ++i, ++it) {
100  cout << "List " << i+1 << "/" << t.size() << endl << "\t";
101  printSTLContainer(*it);
102  }
103  }
104  /**\brief Return contents of map in a string representation
105  *
106  * \param[in] m Template map
107  * \param[in] sep String that seperates visually each key and its value. Defaults to " => "
108  * \return std::string representation of map
109  * */
110  template<class T1, class T2>
111  std::string getMapAsString(const std::map<T1, T2>& m,
112  const std::string& sep=" => ") {
113  using namespace std;
114  stringstream ss("");
115 
116  for (typename map<T1, T2>::const_iterator it = m.begin();
117  it != m.end();
118  ++it) {
119 
120  ss << it->first << " => " << it->second << endl;
121  }
122 
123  return ss.str();
124  }
125  /**\brief Print the given map m
126  *
127  * \param[in] m Template map
128  */
129  template<class T1, class T2>
130  void printMap(const std::map<T1, T2>& m) {
131  using namespace std;
132 
133  cout << getMapAsString(m) << endl;
134  }
135 
136 
137 
138  /** @} */ // end of grouping
139  }
140 }
141 
GLdouble GLdouble t
Definition: glext.h:3610
Scalar * iterator
Definition: eigen_plugins.h:23
std::string getSTLContainerAsString(const T &t)
Return a STL container in std::string form.
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
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:3919
void printSTLContainerOfContainers(const T &t)
Print the given STL container of containers t.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void printSTLContainer(const T &t)
Print the given vector t.
void printMap(const std::map< T1, T2 > &m)
Print the given map m.
std::string getMapAsString(const std::map< T1, T2 > &m, const std::string &sep=" => ")
Return contents of map in a string representation.
GLsizei const GLfloat * value
Definition: glext.h:3929
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.
double BASE_IMPEXP distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
Definition: geometry.cpp:1504



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020