重载 operator>> 以从字符串创建数组

huangapple go评论74阅读模式
英文:

Overloading operator>> to create array from string

问题

我想从一个std::string创建一个std::array

为此,我想要重载operator>>

我有以下测试案例:

std::istream& operator>>(std::istream& is, const std::array<double, 3>& a)
{
    char p1, p2;

    is >> p1;
    // 如果失败,则警告用户

    for (unsigned int i = 1; i < a.size(); ++i) 
    {
        // 一些用于忽略/检查是否为数字的内容
    }

    is >> p2;
    // 如果失败,则警告用户
    
    return is;
}

int main()
{
    std::string a = "[1 2 3]";
    std::array<double, 3> arr;
    std::istringstream iss (a);
    iss >> arr;
    
    return 0;
}

我希望该操作符检查字符[]是否在正确的位置,并使用括号内的元素构建数组。

如何检查提取是否成功?如何检查括号内的字符串是否为数字,如果是的话如何从中构建我的数组?

Kind regards

英文:

I would like to create an std::array from a std::string.

For this, I would like to overload the operator&gt;&gt;.

I have the following test case:

std::istream&amp; operator&gt;&gt;(std::istream&amp; is, const std::array&lt;double, 3&gt;&amp; a)
{
    char p1, p2;

    is &gt;&gt; p1;
    // if fail warn the user

    for (unsigned int i = 1; i &lt; a.size(); ++i) 
    {
        // something to ignore/ check if numeric
    }

    is &gt;&gt; p2;
    // if fail warn the user
    
    return is;
}

int main()
{
    std::string a = &quot;[1 2 3]&quot;;
    std::array&lt;double, 3&gt; arr;
    std::istringstream iss (a);
    iss &gt;&gt; arr;
    
    return 0;
}

I would like for the operator to check if the characters [and ] are in the correct place and to construct the array with the elements inside.

How can I do checks if the extraction was successfull? How can I check the string between the parenthesis is numeric and if so construct my array from it?

Kind regards

答案1

得分: 2

我会翻译代码部分:

我会翻译代码部分:

I'd add a helper class that checks for a certain character in the stream and removes it if it's there. If it's not, sets the failbit.

我会添加一个辅助类,该类在流中检查特定字符是否存在,并在存在时将其移除。如果不存在,将设置 failbit

Example:
示例:

```c++
#include <cctype>
#include <istream>

template <char Ch, bool SkipWhitespace = false>
struct eater {
    friend std::istream& operator>>(std::istream& is, eater) {
        if /*constexpr*/ (SkipWhitespace) { // constexpr since C++17
            is >> std::ws;
        }
        if (is.peek() == Ch) // if the expected char is there, remove it
            is.ignore();
        else                 // else set the failbit
            is.setstate(std::ios::failbit);
        return is;
    }
};

And it could then be used like this:
然后可以像这样使用它:

std::istream& operator>>(std::istream& is, std::array<double, 3>& a) {
    // use the `eater` class template with '[' and ']' as template parameters:
    return is >> eater<'['>{} >> a[0] >> a[1] >> a[2] >> eater<']'>{};
}

int main() {
    std::string a = "[1 2 3]";
    std::array<double, 3> arr;
    std::istringstream iss (a);
    // iss.exceptions(std::ios::failbit); // if you want exceptions on failure

    if(iss >> arr) {
        std::cout << "success\n";
    } else {
        std::cout << "fail\n";
    }
}

Demo where ',' is used as separator in the input.
演示 中在输入中使用了 ',' 作为分隔符。


<details>
<summary>英文:</summary>

I&#39;d add a helper class that checks for a certain character in the stream and removes it if it&#39;s there. If it&#39;s not, sets the failbit.

Example:
```c++
#include &lt;cctype&gt;
#include &lt;istream&gt;

template &lt;char Ch, bool SkipWhitespace = false&gt;
struct eater {
    friend std::istream&amp; operator&gt;&gt;(std::istream&amp; is, eater) {
        if /*constexpr*/ (SkipWhitespace) { // constexpr since C++17
            is &gt;&gt; std::ws;
        }
        if (is.peek() == Ch) // if the expected char is there, remove it
            is.ignore();
        else                 // else set the failbit
            is.setstate(std::ios::failbit);
        return is;
    }
};

And it could then be used like this:

std::istream&amp; operator&gt;&gt;(std::istream&amp; is, std::array&lt;double, 3&gt;&amp; a) {
    // use the `eater` class template with `[` and `]` as template parameters:
    return is &gt;&gt; eater&lt;&#39;[&#39;&gt;{} &gt;&gt; a[0] &gt;&gt; a[1] &gt;&gt; a[2] &gt;&gt; eater&lt;&#39;]&#39;&gt;{};
}

int main() {
    std::string a = &quot;[1 2 3]&quot;;
    std::array&lt;double, 3&gt; arr;
    std::istringstream iss (a);
    // iss.exceptions(std::ios::failbit); // if you want exceptions on failure

    if(iss &gt;&gt; arr) {
        std::cout &lt;&lt; &quot;success\n&quot;;
    } else {
        std::cout &lt;&lt; &quot;fail\n&quot;;
    }
}

Demo where , is used as separator in the input.

huangapple
  • 本文由 发表于 2023年1月8日 21:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048273-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定