trim不是标准c / c ++库的一部分? [英] trim is not part of the standard c/c++ library?

查看:114
本文介绍了trim不是标准c / c ++库的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是我还是在c或c ++库中没有标准修剪函数?有没有任何单一的功能,作为一个修剪?如果没有,任何人都可以告诉我为什么修剪不是标准库的一部分? (我知道修剪是在增强)

Is it me or are there no standard trim functions in the c or c++ library? is there any single function that acts as a trim? If not can anyone tell me Why trim is not part of the standard library? (i know trim is in boost)

我的修剪代码是

std::string trim(const std::string &str)
{
    size_t s = str.find_first_not_of(" \n\r\t");
    size_t e = str.find_last_not_of (" \n\r\t");

    if(( string::npos == s) || ( string::npos == e))
        return "";
    else
        return str.substr(s, e-s+1);
}

test:cout< trim(\\\
\r\r\\\
\r\\\
text here\\\
with return \\\
\r\r\\\
\r\\\
);
-edit-
我最想知道为什么它不在标准库,BobbyShaftoe答案是伟大的。 http://stackoverflow.com/questions/479080/ trim-is-not-part-of-the-standard-cc-library#479091

test: cout << trim(" \n\r\r\n \r\n text here\nwith return \n\r\r\n \r\n "); -edit- i mostly wanted to know why it wasnt in the standard library, BobbyShaftoe answer is great. http://stackoverflow.com/questions/479080/trim-is-not-part-of-the-standard-c-c-library#479091

推荐答案

trim()不在标准库中是,当最后一个标准制定时,他们必须在形式化当前行为(添加没有新的,只是稳定已有的)和添加新的功能之间取得平衡。一般来说,他们喜欢不添加功能,除非它1)否则不可能,或2)使用第三方库有重大缺点。进行过多更改

The reason trim() isn't in the standard library is that when the last standard was made, they had to strike a balance between formalizing current behavior (adding nothing new, just stabilizing what already existed), and adding new functionality. In general, they preferred not to add a feature unless it either 1) would be impossible otherwise, or 2) there were significant drawbacks to using third-party libraries instead. Making too many changes would


  • 打破与现有代码的兼容性(可能已定义了自己的trim()

  • 为编译器编写者添加更多的工作(他们之前已经有大量工作)

  • 使语言更复杂以学习和使用。

对于trim(),没有重大的互操作性问题。只要第三方trim()实现接受一个字符串并返回一个字符串,

With trim(), there are no major interoperability issues. As long as your third-party trim() implementation takes a string and returns a string, we don't really care where it's defined. So it's not really necessary In the standard library. It can be easily supplied by other libraries.

通过对比,我们可以很容易地在标准库中提供它。 ,类似字符串类或向量,是标准库必须提供的类,因为如果你使用自定义字符串类,那么只有来自该库的字符串操作才能工作。使用标准库字符串,第三方库可以定位这个公共

By contrast, something like the string class or vector, are classes that the standard library must supply, because if you use a custom string class, only string operations from that library will work. With a standard library string, third-party libraries can target this common string definition, and everyone wins.

当最后一个标准出来时,Herb Sutter写了一篇很好的文章这里

When the last standard came out, Herb Sutter wrote a post describing this very well here

当然,有一个trim()函数会更好,但他们有更大的鱼炸鱼。他们必须首先标准化所有的基础知识。我不认为C ++ 0x将添加一个修剪功能,但它会添加很多其他方便的实用程序,回到'98被认为是不必要的或太专业。

Of course, it would be nice to have a trim() function, but they had bigger fish to fry. They had to standardize all the basics first. I don't think C++0x will add a trim function, but it will add a lot of other convenience utilities that back in '98 were considered "unnecessary" or too specialized.

这篇关于trim不是标准c / c ++库的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆