Remove extra white spaces in C++

前端 未结 12 1935
日久生厌
日久生厌 2021-02-05 12:01

I tried to write a script that removes extra white spaces but I didn\'t manage to finish it.

Basically I want to transform abc sssd g g sdg gg gf into

12条回答
  •  走了就别回头了
    2021-02-05 12:13

    Since you are writing c-style, here's a way to do what you want. Note that you can remove '\r' and '\n' which are line breaks (but of course that's up to you if you consider those whitespaces or not).

    This function should be as fast or faster than any other alternative and no memory allocation takes place even when it's called with std::strings (I've overloaded it).

    char temp[] = " alsdasdl   gasdasd  ee";
    remove_whitesaces(temp);
    printf("%s\n", temp);
    
    int remove_whitesaces(char *p)
    {
        int len = strlen(p);
        int new_len = 0;
        bool space = false;
    
        for (int i = 0; i < len; i++)
        {
            switch (p[i])
            {
            case ' ': space = true;  break;
            case '\t': space = true;  break;
            case '\n': break; // you could set space true for \r and \n
            case '\r': break; // if you consider them spaces, I just ignore them.
            default:
                if (space && new_len > 0)
                    p[new_len++] = ' ';
                p[new_len++] = p[i];
                space = false;
            }
        }
    
        p[new_len] = '\0';
    
        return new_len;
    }
    
    // and you can use it with strings too,
    
    inline int remove_whitesaces(std::string &str)
    {
        int len = remove_whitesaces(&str[0]);
        str.resize(len);
        return len; // returning len for consistency with the primary function
                    // but u can return std::string instead.
    }
    
    // again no memory allocation is gonna take place,
    // since resize does not not free memory because the length is either equal or lower
    

    If you take a brief look at the C++ Standard library, you will notice that a lot C++ functions that return std::string, or other std::objects are basically a wrapper to a well written extern "C" function. So don't be afraid to use C functions in C++ applications, if they are well written and you can overload them to support std::strings and such.

    For example, in Visual Studio 2015, std::to_string is written exactly like this:

    inline string to_string(int _Val)
        {   // convert int to string
        return (_Integral_to_string("%d", _Val));
        }
    
    inline string to_string(unsigned int _Val)
        {   // convert unsigned int to string
        return (_Integral_to_string("%u", _Val));
        }
    

    and _Integral_to_string is a wrapper to a C function sprintf_s

    template inline
        string _Integral_to_string(const char *_Fmt, _Ty _Val)
        {   // convert _Ty to string
        static_assert(is_integral<_Ty>::value,
            "_Ty must be integral");
        char _Buf[_TO_STRING_BUF_SIZE];
        int _Len = _CSTD sprintf_s(_Buf, _TO_STRING_BUF_SIZE, _Fmt, _Val);
        return (string(_Buf, _Len));
        }
    

提交回复
热议问题