I am currently at hand have a function at hand within a class, User::User(string id, string ratings). I am attempting to currently store the string "1 5 5 3 0 -5 0 5" (which is the parameter for ratings, id is already dealt with) into a vector which cannot be changed called vector(int) <--integers ratings as a vector of 8 integers seen in the string. What is the best way to iterate through this string and store the individual values(negatives included)?
Cannot convert a string of integers to integers in C++
143 views Asked by Sam Anoff At
        	2
        	
        There are 2 answers
0
                 On
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                This problem is a canonical use-case for stream iterators
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
int main()
{
  std::istringstream iss{ "1 5 5 3 0 -5 -5" };
  std::vector< int > v;
  std::copy( std::istream_iterator< int >{ iss },
             std::istream_iterator< int >{},
             std::back_inserter( v ) );
  std::copy( std::begin( v ),
             std::end( v ),
             std::ostream_iterator< int >( std::cout, " " ) );
}