嵌套模板 C++ 的重载流运算符STL

发布于 2024-12-04 07:25:37 字数 1313 浏览 0 评论 0 原文

我有一个数据文件,它是由一系列嵌套的双精度数组成的单行,例如。

[[0.127279,0.763675,0.636396],[0.254558,0.890955,0.636396],
[0.127279,0.636396,0.763675],[0.254558,0.763675,0.763675],
[0.381838,0.890955,0.763675],[0.127279,0.509117,0.890955],
[0.254558,0.636396,0.890955],[0.509117,0.890955,0.890955]]

我希望能够将其读入 STL vector > 使用流运算符,该运算符是在 A 的内部类型中模板化的:

vector<vector<double> > A;
FIN >> A;

我已经找到了一种在向量未嵌套时执行此操作的方法,即。一个简单的 vector 如下:

template <class T>
istream& operator>>(istream& s, vector<T> &A){
  T x;
  string token; char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ',') ) {
   istringstream input(token);
   input >> x;
   A.push_back(x);
  }
  s >> blank; // Gobble the last ']'
  return s;
}

但是我在使用 istream& 时遇到了问题。 operator>>(istream& s, vector >&A) 部分,因为我似乎无法正确捕获内部 ] 。我确信 Boost 有办法做到这一点,但我希望看到一个用于教学目的的 STL 解决方案。

注意:我知道重载 vector 的流运算符可能会产生深远的不良后果,并且该实现应该包含在它自己的类中 - 我正在使用上面的示例就这样澄清了这个问题。

编辑: 我希望该方法足够强大,能够处理输入数组,其大小(和内部数组)大小事先不知道,而是从读取流中推断出来。

I've got a data file that is a single line consisting of a nested series of doubles eg.

[[0.127279,0.763675,0.636396],[0.254558,0.890955,0.636396],
[0.127279,0.636396,0.763675],[0.254558,0.763675,0.763675],
[0.381838,0.890955,0.763675],[0.127279,0.509117,0.890955],
[0.254558,0.636396,0.890955],[0.509117,0.890955,0.890955]]

I'd like to be able to read this into a STL vector<vector<double> > using the stream operator which is templated across the inner type of A:

vector<vector<double> > A;
FIN >> A;

I've figured out a way to do this when the vector is not nested, ie. a simple vector<T> as so:

template <class T>
istream& operator>>(istream& s, vector<T> &A){
  T x;
  string token; char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ',') ) {
   istringstream input(token);
   input >> x;
   A.push_back(x);
  }
  s >> blank; // Gobble the last ']'
  return s;
}

But I'm having problem with the istream& operator>>(istream& s, vector<vector<T> >&A) part as I can't seem to catch the inner ]'s properly. I'm sure that Boost has a way of doing this, but I'd like to see a solution with the STL for pedagogical purposes.

Note: I'm aware that overloading the stream operator for vector<T> can have far-reaching undesirable consequences and that the implementation should be wrapped up in its own class - I'm using this example above as it stands to clarify the question.

EDIT:
I'd like the method to be robust enough to handle a input array whose size (and inner array) size is not known in advance, but inferred from reading the stream.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小兔几 2024-12-11 07:25:37

实际上,您的代码的问题是您希望对两者使用相同的函数,当 T 为:

  • vector
  • double

但是需要将数据读入vectordouble的逻辑略有不同。所以你不能这样做,至少不能用那么简单的逻辑:

我宁愿编写两个函数来分别处理这两种情况。毕竟,即使在您的情况下,编译器也会为 T 的每个值生成两个不同的函数。

template <class T>
istream& operator>>(istream& s, vector<T> &A)
{
  T x;
  string token; char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ',') ) 
  {
   istringstream input(token);
   input >> x;
   A.push_back(x);
  }
// s >> blank; // Gobble the last ']'
  return s;
}

template <class T>
istream& operator>>(istream& s, vector<vector<T>> &A)
{
  vector<T> x;
  string token; 
  char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ']') ) 
  {
   istringstream input(token);
   input >> x;
   s >> blank; //read , after [...]
   A.push_back(x);
   x.clear();
  }
  s >> blank; // Gobble the last ']'
  return s;
}

测试代码:

int main() {
        vector<vector<double>> A;       
        cin >> A;
        for(size_t i = 0 ;i < A.size(); ++i)
        {
            for(size_t j = 0 ; j < A[i].size(); ++j)
                 cout << A[i][j] <<"   ";
            cout << endl;
        }
        return 0;
}

输入:

[[1,2,3],[4,5,6],
[7,8,9],[10,11,12],
[13,14,15],[16,17,18],
[19,20,21],[22,23,24]]

输出:

1   2   3   
4   5   6   
7   8   9   
10   11   12   
13   14   15   
16   17   18   
19   20   21   
22   23   24 

在线演示: http://ideone.com/iBbmw

Actually, the problem with your code that you want to use the same function for both, when T is:

  • vector<double>
  • double

But the logic which needs to read the data into vector and double is slightly different. So you cannot do that, at least not with that simple logic:

I would prefer to write two functions, to handle both cases separately. After all, even in your case, the compiler will generate two different functions for each value of T.

template <class T>
istream& operator>>(istream& s, vector<T> &A)
{
  T x;
  string token; char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ',') ) 
  {
   istringstream input(token);
   input >> x;
   A.push_back(x);
  }
// s >> blank; // Gobble the last ']'
  return s;
}

template <class T>
istream& operator>>(istream& s, vector<vector<T>> &A)
{
  vector<T> x;
  string token; 
  char blank;

  s >> blank; // Gobble the first '['
  while( getline(s, token, ']') ) 
  {
   istringstream input(token);
   input >> x;
   s >> blank; //read , after [...]
   A.push_back(x);
   x.clear();
  }
  s >> blank; // Gobble the last ']'
  return s;
}

Test code:

int main() {
        vector<vector<double>> A;       
        cin >> A;
        for(size_t i = 0 ;i < A.size(); ++i)
        {
            for(size_t j = 0 ; j < A[i].size(); ++j)
                 cout << A[i][j] <<"   ";
            cout << endl;
        }
        return 0;
}

Input:

[[1,2,3],[4,5,6],
[7,8,9],[10,11,12],
[13,14,15],[16,17,18],
[19,20,21],[22,23,24]]

Output:

1   2   3   
4   5   6   
7   8   9   
10   11   12   
13   14   15   
16   17   18   
19   20   21   
22   23   24 

Online demo : http://ideone.com/iBbmw

铁憨憨 2024-12-11 07:25:37

在您的特定示例中,这非常简单。

  • 将整行读入字符串。
  • 将所有 [ , ] 和 , 替换为空白字符。
  • 使用空格替换字符串创建一个简单的字符串流。

现在,在读取三个双精度数后,您可以有一个简单的循环

double x;
while( stringstreamp >> x )
{
}

和一些特殊的逻辑,将它们插入到一个新数组中。

In your particular example which is very simple.

  • Read the whole line into a string.
  • Replace all [ , ] and , with whitespace character.
  • Create a simple stringstream with whitespace replaced string.

Now you can have a a simple loop of

double x;
while( stringstreamp >> x )
{
}

And some special logic after reading three doubles to insert them them into a new array.

止于盛夏 2024-12-11 07:25:37

几年后,我在这里为同样的问题而苦苦挣扎。

根据您的贡献,我开发了原始模板的修改版本。它能够解析多维数组,即使它们分布在几行中。

template <class T>
istream& operator>>(istream& s, vector<T> &A){
  while(true){
    T x;
    char c = s.peek();

    if( c == '[' || c == ','){ 
        s.ignore();
        if(s >> x) A.push_back(x);
        else throw invalid_argument("Bad, bad JS array!");
        continue;
    }

    if( c == ']') {
        s.ignore();
        return s;
    }

    /* Ignore line-break */
    s.ignore();     
  }
  return s;
}

希望这对某人有用。

Some years later, and I was here struggling with the same problem.

Based on your contribution, I developed a modified version of the original template. This one is able to parse multidimensional arrays, even if they are spread across several lines.

template <class T>
istream& operator>>(istream& s, vector<T> &A){
  while(true){
    T x;
    char c = s.peek();

    if( c == '[' || c == ','){ 
        s.ignore();
        if(s >> x) A.push_back(x);
        else throw invalid_argument("Bad, bad JS array!");
        continue;
    }

    if( c == ']') {
        s.ignore();
        return s;
    }

    /* Ignore line-break */
    s.ignore();     
  }
  return s;
}

Hope this can be useful for someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文