how can read data from file for vector<Mat> variable

I have used one sample code from the link below:
http://www.cplusplus.com/forum/general/221958/

I can save and load data perfectly for "vector<int>" type.
But for "vector<Mat>", i can only write data,but could not read data.

Regards,
Kihan Patel.
Is Mat primitive data type?
It contains data as below:

[128, 128, 128, 128, 128, 86, 81, 74, 69, 66, 63, 60, 57, 56, 51, 50, 48, 48, 51, 56, 60, 65, 70, 74 .........]
[128, 128, 128, 128, 128, 86, 81, 74, 69, 66, 63, 60, 57, 56, 51, 50, 48, 48, 51, 56, 60, 65, 70, 74........]

This type of data.
One way is to overload the >> operator for your Mat type.
Once you have done this you can read it like this:
Mat mat; cin >> mat
Okay. How can i got the number of Mat.
Here one mat would contain data between '[' ']'.
And second Mat would contain data between another '[' ']'.
So, how can i read it like this method.
A very crude demo - don't have time for sth. more elaborate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

struct Mat
{
  // some other data
  vector<int> numbers;
};

istream& operator>>(istream& is, Mat& mat)
{
  char delim;
  int num;
  // TODO add error checking
  is >> delim; // read '['
  while (is >> num >> delim) // until last ']' assumes end of stream
    mat.numbers.push_back(num);

  return is;
}
int main(int argc, char *argv[])
{
  istringstream iss("[128, 128, 128, 128, 128, 86, 81, 74, 69, 66, 63, 60, 57, 56, 51, 
50, 48, 48, 51, 56, 60, 65, 70, 74]");

  Mat mat;
  iss >> mat;

  for (int num: mat.numbers)
    cout << num << ' ';
}
Topic archived. No new replies allowed.