Changeset 1499
- Timestamp:
- Aug 27, 2012, 11:54:38 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/0.9-stable/yat/utility.h
r1490 r1499 2 2 #define _theplu_yat_utility_utility_ 3 3 4 // $Id: utility.h 2 273 2010-06-15 02:11:47Z peter $4 // $Id: utility.h 2673 2011-12-03 00:30:12Z peter $ 5 5 6 6 /* … … 8 8 Copyright (C) 2006 Jari Häkkinen 9 9 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 10 Copyright (C) 2009, 2010 Peter Johansson10 Copyright (C) 2009, 2010, 2011 Peter Johansson 11 11 12 12 This file is part of the yat library, http://dev.thep.lu.se/yat … … 35 35 #include "Exception.h" 36 36 37 #include <algorithm> 38 #include <cctype> 37 39 #include <cmath> 40 #include <functional> 38 41 #include <limits> 42 #include <locale> 39 43 #include <istream> 40 44 #include <string> … … 62 66 OutputIterator result); 63 67 68 /** 69 \brief convert T to a string 70 71 T is supposed to be a numerical type. 72 73 \since new in yat 0.8 74 */ 75 template<typename T> 76 std::string convert(T input); 64 77 65 78 /** … … 72 85 73 86 /** 74 \brief check if string is convertible to (numerical) type 87 \brief check if string is convertible to (numerical) type \c T 75 88 76 89 \since New in yat 0.5 … … 259 272 260 273 // template implementations 274 template<typename T> 275 std::string convert(T input) 276 { 277 std::ostringstream ss; 278 ss << input; 279 return ss.str(); 280 } 281 282 261 283 template<typename T> 262 284 T convert(const std::string& s) … … 300 322 if (rectangle && nof_columns && v.size()!=nof_columns) { 301 323 std::ostringstream s; 302 s << "load data fileerror: "324 s << "load stream error: " 303 325 << "line " << matrix.size() << " has " << v.size() 304 326 << " columns; expected " << nof_columns << " columns."; … … 313 335 314 336 template<typename T> 315 void load(std::istream& is, std::vector<T>& vec, char sep ='\0')337 void load(std::istream& is, std::vector<T>& vec, char sep) 316 338 { 317 339 detail::VectorPusher<T> pusher; … … 334 356 bool convert(const std::string& s, T& result) 335 357 { 358 if (!std::numeric_limits<T>::is_signed) { 359 // first non-whitespace character 360 std::string::const_iterator iter = s.begin(); 361 while (iter!=s.end() && std::isspace(*iter)) 362 ++iter; 363 // unsigned int cannot start with a '-' and with some compilers 364 // operation ss >> result won't fail so catch it like this instead. 365 if (iter==s.end() || *iter=='-') 366 return false; 367 } 336 368 std::istringstream ss(s); 337 369 ss >> result;
Note: See TracChangeset
for help on using the changeset viewer.