You can redefine how it std::ifstream
interprets commas but saying that they too
are just white space. Once that is done the rest falls easily into place.
#include <cassert>
#include <fstream>
#include <iostream>
#include <iterator>
#include <locale>
#include <vector>
namespace
{
struct CommaIsWhitespaceToo : std::ctype<char>
{
static auto makeTable()
{
static std::vector<mask> v(classic_table(), classic_table() + table_size);
v[','] |= space;
return v.data();
}
CommaIsWhitespaceToo() : ctype(makeTable()) {}
};
} // namespace
int main()
{
int const kRows = 73;
int const kCol = 74496;
std::vector<double> data;
data.reserve(kRows * kCol);
std::ifstream file("NAimg_20101026_145727.csv");
file.imbue(std::locale(file.getloc(), new CommaIsWhitespaceToo));
data.assign(
std::istream_iterator<double>(file), std::istream_iterator<double>());
assert(data.size() == kRows * kCol); // Just make sure we got them all. Not
// too many and not too few
std::cout << data[(1 * kCol) + 1] << std::endl;
return 0;
}
An alternative body where the data
type uses []
access can be done
like:
int main()
{
constexpr int kRows = 73;
constexpr int kCol = 74496;
using Row = std::array<double, kCol>;
using My2D = std::array<Row, kRows>;
My2D data;
std::ifstream file("NAimg_20101026_145727.csv");
file.imbue(std::locale(file.getloc(), new CommaIsWhitespaceToo));
for(auto& row : data)
for(auto& col : row)
file >> col;
std::cout << data[1][1] << std::endl;
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…