Skip to content

Instantly share code, notes, and snippets.

@jasonbeach
Created April 18, 2020 19:40
Show Gist options
  • Save jasonbeach/d21cf7a131173c4d1fb8346f7049ce43 to your computer and use it in GitHub Desktop.
Save jasonbeach/d21cf7a131173c4d1fb8346f7049ce43 to your computer and use it in GitHub Desktop.
Parse two lists of numbers and zip them into a vector of pairs
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
struct Pos{
float x = 0;
float y = 0;
};
int main()
{
std::string x{"1.1 2.2 3.3 4.4 5.5"};
std::string y{"5.5 4.4 3.3 2.2 1.1"};
std::vector<Pos> nums;
std::istringstream iss_x(x);
std::istringstream iss_y(y);
std::transform(std::istream_iterator<float>(iss_x), std::istream_iterator<float>(), std::istream_iterator<float>(iss_y), std::back_inserter(nums),
[](float x, float y)-> Pos {
Pos pos{x,y};
return pos;
});
std::for_each(nums.begin(), nums.end(), [](const Pos& pos){std::cout << "(" << pos.x << ", " << pos.y << "), ";});
std::cout << std::endl;
}
@jasonbeach
Copy link
Author

Only thing not really desirable is there's not a readily apparent way to check or flag if y ends up being shorter than x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment