Main MRPT website > C++ reference for 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
17 {
18 namespace containers
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
36  * iterator to the next element in the container (or ::end if none) */
37 template <class T>
39  std::list<T>& cont, typename std::list<T>::iterator& it)
40 {
41  return cont.erase(it);
42 }
43 //! \overload
44 template <class K, class V>
46  std::map<K, V>& cont, typename std::map<K, V>::iterator& it)
47 {
48  typename std::map<K, V>::iterator itRet = it;
49  ++itRet;
50  cont.erase(it);
51  return itRet;
52 }
53 //! \overload
54 template <class K, class V>
56  std::multimap<K, V>& cont, typename std::multimap<K, V>::iterator& it)
57 {
58  typename std::multimap<K, V>::iterator itRet = it;
59  ++itRet;
60  cont.erase(it);
61  return itRet;
62 }
63 //! \overload
64 template <class T>
66  std::set<T>& cont, typename std::set<T>::iterator& it)
67 {
68  typename std::set<T>::iterator itRet = it;
69  ++itRet;
70  cont.erase(it);
71  return itRet;
72 }
73 
74 /**\brief Return a STL container in std::string form.
75  *
76  * \param[in] t Template STL container (e.g. vector)
77  * \return String form of given STL container
78  */
79 template <class T>
81 {
82  using namespace std;
83  stringstream ss;
84  for (typename T::const_iterator it = t.begin(); it != t.end(); ++it)
85  {
86  ss << *it << ", ";
87  }
88  return ss.str();
89 }
90 /**\brief Print the given vector t.
91  *
92  * \param[in] t Template vector
93  */
94 template <class T>
95 void printSTLContainer(const T& t)
96 {
97  using namespace std;
98  cout << getSTLContainerAsString(t) << endl;
99 }
100 /**\brief Print the given STL container of containers t.
101  *
102  * \param[in] t Template STL container (containing other containers)
103  */
104 template <class T>
106 {
107  using namespace std;
108 
109  int i = 0;
110  for (typename T::const_iterator it = t.begin(); it != t.end(); ++i, ++it)
111  {
112  cout << "List " << i + 1 << "/" << t.size() << endl << "\t";
113  printSTLContainer(*it);
114  }
115 }
116 /**\brief Return contents of map in a string representation
117  *
118  * \param[in] m Template map
119  * \param[in] sep String that seperates visually each key and its value.
120  * Defaults to " => "
121  * \return std::string representation of map
122  * */
123 template <class T1, class T2>
125  const std::map<T1, T2>& m, const std::string& sep = " => ")
126 {
127  using namespace std;
128  stringstream ss("");
129 
130  for (typename map<T1, T2>::const_iterator it = m.begin(); it != m.end();
131  ++it)
132  {
133  ss << it->first << " => " << it->second << endl;
134  }
135 
136  return ss.str();
137 }
138 /**\brief Print the given map m
139  *
140  * \param[in] m Template map
141  */
142 template <class T1, class T2>
143 void printMap(const std::map<T1, T2>& m)
144 {
145  std::cout << getMapAsString(m) << std::endl;
146 }
147 
148 /** @} */ // end of grouping
149 }
150 }
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.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019