mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-04 11:15:55 +01:00
function to read std::vector from a string (blank separated values)
This commit is contained in:
parent
6aa000176f
commit
ba09cbae3e
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user