How to extract a string of numbers into a vector of integers in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>

using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
	string s = "23 87 976 5291 8";

	std::istringstream iss(s);
	vector<int> tokens{ std::istream_iterator<int>{iss},
		                std::istream_iterator<int>{} };

	for (int n : tokens)
		cout << n << endl;

	return 0;
}

/*
run:

23
87
976
5291
8

*/

 



answered May 26, 2018 by avibootz
...