Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 清理字符串以便打印到终端的惯用方法?_C++_String_Output_Sanitization_Idioms - Fatal编程技术网

C++ 清理字符串以便打印到终端的惯用方法?

C++ 清理字符串以便打印到终端的惯用方法?,c++,string,output,sanitization,idioms,C++,String,Output,Sanitization,Idioms,我在内存中有这个字符串,我想将它打印到终端或日志文件中,而不会得到垃圾/不可打印的字符,这会限制我的风格。因此,与其 my_output_stream << my_string; 是否有一些惯用的/标准的工具来实现这一点?惯用的方法是过滤非打印内容 我知道没有现成的方法,但写一个很容易:查看 #include <cctype> #include <iostream> #include <sstream> #include <string&g

我在内存中有这个字符串,我想将它打印到终端或日志文件中,而不会得到垃圾/不可打印的字符,这会限制我的风格。因此,与其

my_output_stream << my_string;

是否有一些惯用的/标准的工具来实现这一点?

惯用的方法是过滤非打印内容

我知道没有现成的方法,但写一个很容易:查看

#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

template <typename It>
std::string sanitize(It f, It l) { 
    std::ostringstream oss;
    for (;f!=l;++f)
    {
        if((std::isgraph(*f) || std::isspace(*f)) && *f != '\r' && *f != '\n')
            oss << *f;
        else
            oss << "%" << std::hex << std::setw(2) << std::setfill('0') << 
                static_cast<unsigned>(static_cast<unsigned char>(*f));
    }
    return oss.str();
}

template <typename C>
std::string sanitize(C const& c) { 
    using std::begin;
    using std::end;

    return sanitize(begin(c), end(c));
}

int main()
{
    std::cout << sanitize("Hello\tworld\r\n. §1.3 \b") << "\n";
}
#包括
#包括
#包括
#包括
#包括
模板
字符串清理(It f,It l){
std::ostringstream oss;
对于(;f!=l;++f)
{
如果((std::isgraph(*f)| std::isspace(*f))&&&*f!='\r'&&&*f!='\n')

osscctype库有一个isprint()函数,如果字符可打印,则返回true。您可以使用该函数检查字符串中的字符是否可打印,如果可打印,则让它打印该字符,如果不可打印,则不执行任何操作。

如何获取字符串中的垃圾值?是否最好事先检查其中的内容。对您进行清理可能需要检查可打印字符,并将其替换为s.th.人类可读字符(如转义字符表示)。@πάνταῥεῖ: 我可以把它们放在我的字符串中,因为有各种各样的bug:-)对于logger类或诸如此类的类来说是绝对有意义的,但正如我在回答中另外提到的,你可能会给出一个可打印的表示,而不是“扔掉”不可打印的Chanracter。马克:谢谢,我知道,但我不希望“滚我自己的”字符串消毒剂。此外,如果字符不可打印,您不清楚应该打印什么。@einpoklum事实上,您没有清楚如果字符不可打印,应该打印什么(明白吗?由您决定要求)。我的答案-随机-选择“url编码”样式,例如
%08
\b
sanitize_to(my_output_stream, my_string);
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

template <typename It>
std::string sanitize(It f, It l) { 
    std::ostringstream oss;
    for (;f!=l;++f)
    {
        if((std::isgraph(*f) || std::isspace(*f)) && *f != '\r' && *f != '\n')
            oss << *f;
        else
            oss << "%" << std::hex << std::setw(2) << std::setfill('0') << 
                static_cast<unsigned>(static_cast<unsigned char>(*f));
    }
    return oss.str();
}

template <typename C>
std::string sanitize(C const& c) { 
    using std::begin;
    using std::end;

    return sanitize(begin(c), end(c));
}

int main()
{
    std::cout << sanitize("Hello\tworld\r\n. §1.3 \b") << "\n";
}