简体   繁体   English

动态C字符串类的重载>>运算符

[英]Overloading >> operator for dynamic c string class

I need to overloading the cin >> operator for my c string class. 我需要为我的c字符串类重载cin >>运算符。 I have overloaded the operator before but don't understand how to do this dynamically without having the size before hand to create the c string. 我之前已经重载了运算符,但不了解如何动态地执行此操作,而又没有事先创建c字符串的大小。

This is for homework and I must not use the string class. 这是用于家庭作业,我不能使用字符串类。 I also have to use dynamic allocation. 我还必须使用动态分配。

This is what I have so far... I know it's probably very poorly written, forgive me I'm a beginner. 到目前为止,这就是我所拥有的...我知道它的写作可能很差,请原谅我是一个初学者。

istream& operator>> (istream& is, MyString& s1) 
{   
    MyString temp;
    int size = 0;
    int i = 0;
    int j = 0;

    while (isspace(temp.data[i]) == true) {
        is.get(temp.data[i]);
        i++;
    }

    while (isspace(temp.data[j]) != true) {
        size++;
        temp.grow(size);
        is >> temp.data[j];
        j++;
    }

    return is;  
}

Without seeing exactly how your MyString class is implemented, we can only speculate about how best to implement streaming into it, but typically you should implement your custom operator>> something like this: 在没有确切了解如何实现MyString类的情况下,我们只能推测如何最好地实现向其中流式传输,但通常应实现自定义operator>>东西:

istream& operator>> (istream& is, MyString& str) 
{   
    istream::sentry s(is, false); // prepare the stream for input (flush output, skip leading whitespaces, error checking, etc)
    if (s) // is the stream ready?
    {
        // clear str as needed

        streamsize N = is.width();
        if (N == 0) N = ... ; // set to max size of str, or numeric_limits<size_t>::max()

        char ch;
        while (is.get(ch)) // while not EOF or failure
        {
            // append ch to str, growing its capacity as needed

            if (--N == 0) break; // max width reached?
            if (!is.peek(ch)) break; // EOF reached?
            if (isspace(ch, is.getloc()) break; // trailing whitespace detected?
        }
    }

    is.width(0); // reset effect of std::setw()
    return is;  
}

The STL's built-in operator>> implementation for std::string is a little bit more complicated (use of traits and facets, direct access to the istream read buffer, etc), but this is the jist of it, based on the following information from CppReference.com : STL对std::string的内置operator>>实现稍微复杂一点(使用特征和构面,直接访问istream读取缓冲区等),但这是它的主要内容,基于以下内容来自CppReference.com的信息:

operator<<,>>(std::basic_string) 运算符<<,>>(std :: basic_string)

 template <class CharT, class Traits, class Allocator> std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::basic_string<CharT, Traits, Allocator>& str); 

Behaves as an FormattedInputFunction . 表现为FormattedInputFunction After constructing and checking the sentry object, which may skip leading whitespace, first clears str with str.erase() , then reads characters from is and appends them to str as if by str.append(1, c) , until one of the following conditions becomes true: 在构造并检查了可能跳过前导空格的哨兵对象之后,首先使用str.erase()清除str,然后从is中读取字符并将它们附加到str上,就像通过str.append(1, c) ,直到其中一个满足以下条件:
- N characters are read, where N is is.width() if is.width() > 0, otherwise N is str.max_size() -读取N个字符,如果is.width()> 0,则N为is.width(),否则N为str.max_size()
- the end-of-file condition occurs in the stream is -文件结束条件发生在流中
- std::isspace(c,is.getloc()) is true for the next character c in is (this whitespace character remains in the input stream). std::isspace(c,is.getloc())对于in中的下一个字符c为true(此空白字符保留在输入流中)。

If no characters are extracted then std::ios::failbit is set on is, which may throw std::ios_base::failure . 如果未提取任何字符,则将std::ios::failbit设置为on,这可能会引发std :: ios_base :: failure

Finally, calls os.width(0) to cancel the effects of std::setw , if any. 最后,调用os.width(0)取消std :: setw的影响(如果有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM