mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-04 14:04:32 +00:00 
			
		
		
		
	function to read std::vector from a string (blank separated values)
This commit is contained in:
		@@ -32,6 +32,22 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
 | 
			
		||||
#include <type_traits>
 | 
			
		||||
 | 
			
		||||
namespace Grid {
 | 
			
		||||
  // helper function to read space-separated values
 | 
			
		||||
  template <typename T>
 | 
			
		||||
  std::vector<T> strToVec(const std::string s)
 | 
			
		||||
  {
 | 
			
		||||
    std::istringstream sstr(s);
 | 
			
		||||
    T                  buf;
 | 
			
		||||
    std::vector<T>     v;
 | 
			
		||||
    
 | 
			
		||||
    while(!sstr.eof())
 | 
			
		||||
    {
 | 
			
		||||
      sstr >> buf;
 | 
			
		||||
      v.push_back(buf);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return v;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  class Serializable {};
 | 
			
		||||
  
 | 
			
		||||
 
 | 
			
		||||
@@ -140,4 +140,18 @@ int main(int argc,char **argv)
 | 
			
		||||
    std::cout << "Loaded (txt) -----------------" << std::endl;
 | 
			
		||||
    std::cout << copy3 << std::endl << veccopy3 << std::endl;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  std::vector<int> iv = strToVec<int>("1 2 2 4");
 | 
			
		||||
  std::vector<std::string> sv = strToVec<std::string>("bli bla blu");
 | 
			
		||||
  
 | 
			
		||||
  for (auto &e: iv)
 | 
			
		||||
  {
 | 
			
		||||
    std::cout << e << " ";
 | 
			
		||||
  }
 | 
			
		||||
  std::cout << std::endl;
 | 
			
		||||
  for (auto &e: sv)
 | 
			
		||||
  {
 | 
			
		||||
    std::cout << e << " ";
 | 
			
		||||
  }
 | 
			
		||||
  std::cout << std::endl;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user