There is a template to fill vector <T>
from the file:
template<typename T, typename A>
void fill_vector_from_file(const std::string &filePath, std::vector<T, A> & target)
{
std::ifstream is(filePath, std::ifstream::in);
is.seekg(0, std::ifstream::end);
std::size_t size = is.tellg();
is.seekg(0, std::ifstream::beg);
target.reserve(size);
std::string line;
while (std::getline(is, line))
{
std::istringstream line_in(line);
while (line_in)
{
T val = 0;
if (line_in >> val)
{
target.push_back(val);
}
}
}
is.close();
Data in files can be int or binary and stored one number per line , for example:
For int:
2
-3
4
and for binary:
010
111
001
When I checked template with std::vector<int> v1
for integers
and std::vector<unsigned char> v2
, result of v2[0]
was 0
instead of 010
.
(I supposed, that I should use unsigned char for to store binary)
Question: Is there any way to modify template , so the result of v2[0]
will be as expected (010
).