これでは処理が煩雑になりすぎている気がするのですが、これしか方法はないのでしょうか?
いや、いうほど煩雑じゃないのでは?
$ , * を区切り文字とし、区切り文字の左と右に切り分けるサンプル↓
C++
1#include <string>
2#include <iostream>
3#include <iomanip>
4
5int main(){
6 std::string input = "$GPRMC,085120.307,A,3541.1493,N,13945.3994,E,000.0,240.3,181211,,,A*6A";
7
8 std::string::size_type pos;
9 while ( (pos = input.find_first_of("$,*")) != std::string::npos ) {
10 char delim = input[pos];
11 std::string token = input.substr(0, pos);
12 input = input.substr(pos+1);
13 std::cout << std::setw(16) << ("[" + token + "] ") << delim << " [" << input << "]\n";
14 }
15}
実行結果:
[] $ [GPRMC,085120.307,A,3541.1493,N,13945.3994,E,000.0,240.3,181211,,,A*6A]
[GPRMC] , [085120.307,A,3541.1493,N,13945.3994,E,000.0,240.3,181211,,,A*6A]
[085120.307] , [A,3541.1493,N,13945.3994,E,000.0,240.3,181211,,,A*6A]
[A] , [3541.1493,N,13945.3994,E,000.0,240.3,181211,,,A*6A]
[3541.1493] , [N,13945.3994,E,000.0,240.3,181211,,,A*6A]
[N] , [13945.3994,E,000.0,240.3,181211,,,A*6A]
[13945.3994] , [E,000.0,240.3,181211,,,A*6A]
[E] , [000.0,240.3,181211,,,A*6A]
[000.0] , [240.3,181211,,,A*6A]
[240.3] , [181211,,,A*6A]
[181211] , [,,A*6A]
[] , [,A*6A]
[] , [A*6A]
[A] * [6A]