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
|
# include <iostream>
# include <sstream>
# include <algorithm>
# include <string>
# include <vector>
using NMEAPair = std::pair<std::string, std::vector<std::string>>;
int main()
{
std::istringstream in {"$GPGLL,5425.32,N,107.11,W,82319*65"};
std::string firstWord{}, secondWord{}, thirdWord{}, fourthWord{}, fifthWord{};
//perhaps you want to use a bit more imaginative variable names!
std::vector<NMEAPair> myVec{};
while (in)
{
getline(in, firstWord, ',')
&& getline(in, secondWord, ',')
&& getline(in, thirdWord, ',')
&& getline(in, fourthWord, ',')
&& getline(in, fifthWord, '*');
//short-circuit evaluation: https://en.wikipedia.org/wiki/Short-circuit_evaluation
firstWord = firstWord.substr(1);
//removing the '$'
if(in)
//check stream still valid
{
myVec.emplace_back(std::make_pair(firstWord, std::vector<std::string>{secondWord, thirdWord, fourthWord, fifthWord}));
}
}
for (const auto& elem : myVec)
{
std::cout << elem.first << "\n";
for (const auto& elemI : elem.second)
{
std::cout << elemI << " ";
}
}
}
|