xtools.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xerror.h"
4 #include "xunits.h"
5 #include <fstream>
6 #include <gsl/gsl_math.h>
7 #include <stdexcept>
8 #include <vector>
9 
10 namespace nmx {
11 
12 struct Tools {
19  inline static auto get_range(double xmin, double xmax, double dx) {
20  std::vector<double> v;
21  double x = xmin;
22  while (x <= xmax) {
23  v.push_back(x);
24  x += dx;
25  }
26  return v;
27  }
28 
32  auto open_file(const std::string &fname) {
33  std::ofstream fout{ fname };
34  // wurde die Datei zum Schreiben geƶffnet?
35  if (!fout) {
36  throw std::ios_base::failure{ fname };
37  }
38  return fout;
39  }
40 
46  auto split(const std::string &s, char delim) {
47  std::vector<std::string> tokens;
48  std::stringstream sstream{ s };
49  std::string token;
50  while (std::getline(sstream, token, delim)) {
51  tokens.push_back(token);
52  }
53  return tokens;
54  }
55 
61  auto split(const std::string &s, const std::string &delim) {
62  size_t beg, pos = 0;
63  std::vector<std::string> tokens;
64  while ((beg = s.find_first_not_of(delim, pos)) != std::string::npos) {
65  pos = s.find_first_of(delim, beg + 1);
66  tokens.push_back(s.substr(beg, pos - beg));
67  }
68  return tokens;
69  }
70 };
71 
72 } // namespace nmx
auto open_file(const std::string &fname)
ex1 Ausgabe von Funktionswerten in eine Datei
Definition: xtools.h:32
static auto get_range(double xmin, double xmax, double dx)
get_range Hilfsfunktion erzeugt eine
Definition: xtools.h:19
auto split(const std::string &s, const std::string &delim)
split
Definition: xtools.h:61
auto split(const std::string &s, char delim)
split
Definition: xtools.h:46