Created
February 26, 2016 19:19
-
-
Save doudou/8097789a79011e39e7bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <vector> | |
#include <stdexcept> | |
#include <iostream> | |
#include <boost/lexical_cast.hpp> | |
using namespace std; | |
using boost::lexical_cast; | |
vector<double> readMatrix(istream& stream, string const& name) | |
{ | |
string actual_name; | |
int row_count, col_count; | |
stream >> actual_name >> row_count >> col_count; | |
if (!stream) | |
throw std::runtime_error("error reading vector " + name); | |
else if (actual_name != name) | |
throw std::runtime_error("expected to get vector " + name + " but got " + actual_name); | |
size_t count = row_count * col_count; | |
vector<double> values; | |
values.reserve(row_count * col_count); | |
for (int i = 0; i < count; ++i) | |
{ | |
double v; | |
stream >> v; | |
if (!stream) | |
throw std::runtime_error("not enough values for " + name + " expected " + lexical_cast<string>(count) + " but got only " + lexical_cast<string>(i + 1)); | |
values.push_back(v); | |
} | |
return values; | |
} | |
int main(int argc, char** argv) | |
{ | |
ifstream stream(argv[1]); | |
readMatrix(stream, "matrix_a"); | |
readMatrix(stream, "matrix_b"); | |
readMatrix(stream, "matrix_c"); | |
readMatrix(stream, "matrix_d"); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matrix_a | |
2 3 | |
0 1 2 3 4 5 | |
matrix_b | |
4 2 | |
0 1 2 3 4 5 6 7 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment