Created
April 18, 2020 19:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.