DISPLACE  1.0
A spatial model of fisheries to help sustainable fishing and maritime spatial planning
vectorsdata.h
Go to the documentation of this file.
1 #ifndef VECTORSDATA_H
2 #define VECTORSDATA_H
3 
4 #include <formatexception.h>
5 
6 #include <vector>
7 #include <sstream>
8 
9 #include <boost/algorithm/string.hpp>
10 #include <boost/lexical_cast.hpp>
11 
12 namespace displace {
13 namespace formats {
14 namespace utils {
15 
16 template <typename ResultType>
17 std::vector<ResultType> stringToVector(std::string string, const char *separator = " ") {
18  std::vector<ResultType> c;
19 
20  boost::trim(string);
21  if (string.empty())
22  return c;
23 
24  std::vector<std::string> sr;
25  boost::split(sr, string, boost::is_any_of(separator));
26 
27  for (auto x : sr) {
28  try {
29  ResultType v = boost::lexical_cast<ResultType>(x);
30  c.push_back(v);
31  } catch (boost::bad_lexical_cast &) {
32  std::stringstream ss;
33  ss << "Bad format for value '" << x << "'";
34  throw FormatException(ss.str());
35  }
36  }
37 
38  return c;
39 }
40 
41 }
42 }
43 }
44 
45 
46 #endif // VECTORSDATA_H
Definition: decisiontreemanager.h:13
Container & split(Container &result, const typename Container::value_type &s, const typename Container::value_type &delimiters, splitX::empties_t empties=splitX::empties_ok)
Definition: myutils.h:170
Definition: formatexception.h:10
Definition: prettyprint.h:19
std::vector< ResultType > stringToVector(std::string string, const char *separator=" ")
Definition: vectorsdata.h:17