Boost 中最常用的部分

当我发现 boost::lexical_cast的时候,我对自己说: “为什么我不早点知道这个!”我讨厌写代码

stringstream ss;
ss << anIntVal;
mystring = ss.str();

现在我写作

mystring = boost::lexical_cast<string>(anIntVal);

Yesterday, on stackoverflow, I came across boost split (another gem that will save me writing code).

string stringtobesplit = "AA/BB-CC")
vector<string> tokens;


boost::split(tokens, stringtobesplit, boost::is_any_of("/-"));
// tokens now holds 3 items: AA BB CC

I am going to start looking through boost documentation looking for other functions that I will be able to use regularly, but I feel that it will be very easy to miss things.

你最常用的/最讨厌没有的增强功能是什么?

20137 次浏览

可能对我来说最常用的助推器是 : share _ ptr

我最常用的是 TR1:

  • 共享指针
  • 数组类

现在我还使用了池类和其他一些更具体的东西。

现在您明白了 Boost 对于大多数程序员来说是有用的,这就是为什么它是未来标准库的测试平台。

我喜欢升级: : Random 和升级: : asio 和升级: : 文件系统,但是升级: : bind、升级: : 循环缓冲区和升级: : 线程非常实用,智能指针也可以,但是我更喜欢 RAII 作为内存管理

我用了很多:

  • 信号
  • : share _ ptr
  • Lexical _ cast
  • boost::bind
  • 随机
  • 线程
  • 不可复制

其他如 Tuple、 Static Assert 和 Integer,如果你正在编写一个可以在各种平台上使用的库,它们是非常有用的。

像图和 Lambda 这样的东西更具体。

Talking about boost::lexical_cast, why isn't something like 'format' a static member in the std::string library?
几乎所有的 gui 库都有类似于 CString: : Format (“% i”)或 QString: : Number (“% i”)的东西,它们返回一个初始化的字符串。

我最喜欢的,顺序不分先后:

  • 正则表达式
  • 文件系统
  • 线
  • Lexical _ cast
  • Program _ options (太棒了!)
  • 测试(针对我所有的单元测试需求)。
  • 字符串算法
  • 字符串标记器
  • Format (类型安全的 printf 样式字符串格式)
  • 智能 PTRS

Boost was a massive help when I wrote my first cross-platform app - without it I really would have struggled.

没人提到升: : tuple? 真丢脸!

BOOST_STATIC_ASSERT

更新 (2011年10月) : C + + 11(C + + 0x)具有 static_assert < a href = “ http://www.2.research ch.att.com/~ bs/C + + 0xFAQ.html # static _ asserter”rel = “ nofollow norefrer”> http://www2.research.att.com/~bs/c++0xfaq.html#static_assert。

boost::shared_ptr is a requirement for modern C++ programming IMHO. That's why they added it to the standard with TR1. boost::program_options, boost::bind, and boost::signal are really nice if you know what they are for and how to use them. The last two tend to scare newcomers though.

我很惊讶竟然没有人提到 boost::optional。除了 shared_ptrscoped_ptr之外,我发现自己使用它的次数比 Boost 的任何部分都多。

我已经使用 share _ ptr 很多年了。它实在是太有用了,一个项目没理由没有它。

除此之外,我还将 Bind/Function/Lambda 用于通用回调机制(在测试时特别有用) ,并将 Format 用于通用的 sprintf 替换。

最后,就在前几天,我愤怒地使用 Variant 解决了一个问题(一个解析器可以用一小组固定的不相关的令牌类型来响应)。解决方案非常优雅,我对此非常满意。


几年过去了,时代变了,现在是更新的时候了。SharedPtr 和 Function 现在是标准的一部分,Bind 和 Lambda 被实际语言级的 Lambda 功能所淘汰。

我仍然使用 Variant (它也已经标准化了,但我还没有到那一步) ,Format 在很大程度上被 fmtlib 所取代(它也已经标准化了)。

我使用的 Boost 的主要部分是 Boost. Asio,它正在被标准化的过程中。

好吧,这是我发现的一个新问题:
我可以不使用 stricmp,而是使用 Boost 的 等于函数,并传入 is _ iequals 谓词
例如:
而不是

stricmp( "avalue", mystr.c_str() ) == 0

我可以利用

equals( "avalue", mystr, is_iequal() )

给定:

#include <boost/algorithm/string.hpp>
using namespace boost::algorithm;

没有人提到 多索引容器,所以我晚点再插话。您并不经常需要它们,但是如果没有升级,那么创建等价的数据结构就会非常痛苦,而且效率也会降低。我最近经常使用它们来创建查找2个键的容器。

BOOST _ FOREACH 让生命再次变得有价值。

(为什么没有人提到这个问题? 这个问题8个月前就问过了!)

我喜欢你可以为 shared_ptr提供自己的析构函数。
这意味着,例如,您可以将它与 FILE*一起使用,并让它为您关闭文件。
例句

void safeclose(FILE*fp) {
if(fp) {
fclose(fp);
}
}
void some_fn() {
boost::shared_ptr<FILE> fp( fopen(myfilename, "a+t"), safeclose );
//body of the function, and when ever it exits the file gets closed
fprintf( fp.get(), "a message\n" );
}

我最常用的一个不是 Boost 本身,而是构建在 Boost 之上的 Adobe 资源库(ASL)ーー具体来说,就是接受升级: : range 的标准算法的扩展,而不是单独的开始/结束迭代器。然后不打电话,说,

std::for_each(some_container.begin(), some_container.end(), do_something());

我只能说

adobe::for_each(some_container, do_something());

(I do hope these parts of ASL migrate to Boost eventually.)

您应该检查 ost: : program _ options,它使得命令行解析更加容易。

我们发现对于解析 ECMAScript 的业务解决方案来说,Spirit 非常有用!

我很惊讶还没有看到答案之间的 推进,穿线

我优先使用 Boost 指针容器,而不是使用 shared_ptr的 STL 容器。

我认为这个问题应该颠倒过来。你会使用 不想要的哪一部分?

根据我的经验,在每个问题领域,几乎所有这些都是有趣和有用的。

您应该花时间查看所有的提升文档,以找到您感兴趣的领域。

一个例外可能是 boost::numeric::ublas,这是做它的工作,但 Eigen做得非常好。

我的建议是:

  • Scope _ exit-不需要为了一次使用而定义 RAII 类
  • 任何
  • boost::variant
  • Boost 指针容器库(ptr _ Vector)
  • 提升泳池图书馆
  • 升级: : unorder _ map/升级: : unorder _ set

使用元组迭代映射,如下所示:

string key, value;
BOOST_FOREACH(tie(key, value), my_map) { ... }

使用提升分配,我可以像这样初始化一个地图:

map<string, string> my_map = map_list_of("key1", "value1")("key2", "value2")("key3", "value3");

使用范围适配器和管道(“ |”)操作符,我可以在映射的值上反向迭代(例如) :

BOOST_FOREACH(string value, my_multimap.equal_range("X") | map_values | reversed) { ... }

I use boost::icl quite a lot for text post-processing. Saved me quite a lot of time because otherwise I'd have to implement text-splitting myself...

BOOST_FOREACH在我的代码中无处不在:)

boost::functionboost::bind是绝对必须的。虽然现在他们是 std::functionstd::bind。这些确实有助于减少不必要的代码量,而且通常对我的设计(或我的幻觉)有好处。

我最近开始使用 boost::interprocess::message_queue,这也是一个很棒的工具。

我会使用更多,但 Qt 有很多天生的方法来做很多 Boost 做的事情。如果我需要编写纯 C + + 程序,我想我会成为一个 boost::junkie:)