util.h
951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef UTIL_H
#define UTIL_H
#include <string>
template < class ContainerT >
void tokenize(const std::string& str, ContainerT& tokens,
const std::string& delimiters = " ", bool trimEmpty = false)
{
std::string::size_type pos, lastPos = 0, length = str.length();
using value_type = typename ContainerT::value_type;
using size_type = typename ContainerT::size_type;
while(lastPos < length + 1)
{
pos = str.find_first_of(delimiters, lastPos);
if(pos == std::string::npos)
{
pos = length;
}
if(pos != lastPos || !trimEmpty)
tokens.push_back(value_type(str.data()+lastPos,
(size_type)pos-lastPos ));
lastPos = pos + 1;
}
}
template <class T>
std::pair<T,T> minmax(T tab[],unsigned int size){
T min = tab[0];
T max = tab[0];
for(unsigned int i=0; i < size; i++){
min = (min > tab[i]) ? tab[i] : min;
max = (max < tab[i]) ? tab[i] : max;
}
return std::pair<T,T>(min,max);
}
#endif // UTIL_H