Tip of the Week #36: New Join API

Tip of the Week #36: New Join API

Originally published as totw/36 on 2013-03-21
By Greg Miller ([email protected])
Updated 2018-01-24
“I got a good mind to join a club and beat you over the head with it.” – Groucho Marx

你们许多人要求提供新的Join API,并且我们也听到你的声音。我们现在有一个Join的函数来替换他们所有。它拼写为absl::StrJoin,你只需要给它一个要加入的对象和一个分隔符串的集合,剩下的就完成了。它将与std::stringabsl::string_viewintdouble的集合一起工作。任何absl::StrCat所支持的类型absl::StrJoin都支持,对于不支持的类型可以通过提供一个定制的Formatter来支持;我们将在下面看到如何使用Formatter来让我们很好地加入对map类型的支持

现在来举一些简单的例子:

std::vector<std::string> v = {"a", "b", "c"};
std::string s = absl::StrJoin(v, "-");
// s == "a-b-c"

std::vector<absl::string_view> v = {"a", "b", "c"};
std::string s = absl::StrJoin(v.begin(), v.end(), "-");
// s == "a-b-c"

std::vector<int> v = {1, 2, 3};
std::string s = absl::StrJoin(v, "-");
// s == "1-2-3"

const int a[] = {1, 2, 3};
std::string s = absl::StrJoin(a, "-");
// s == "1-2-3"

下面的例子通过添加一个Formatter函数并使用了一个不通的分隔符来格式化map类型,这使得输出结果更好并且可读。

std::map<std::string, int> m = {{"a", 1}, {"b", 2}, {"c", 3}};
std::string s = absl::StrJoin(m, ";", absl::PairFormatter("="));
// s == "a=1;b=2;c=3"

你也可以传递一个C++的lambda表达式作为Formatter

std::vector<Foo> foos = GetFoos();

std::string s = absl::StrJoin(foos, ", ", [](std::string* out, const Foo& foo) {
  absl::StrAppend(out, foo.ToString());
});

请参考 absl/strings/str_join.h来获取更多细节。

猜你喜欢

转载自blog.csdn.net/zhangyifei216/article/details/79795996
TIP
36