[#absl-StrJoin-080]
= xref:absl.adoc[absl]::StrJoin
:relfileprefix: ../
:mrdocs:

Joins a range of elements and returns the result as a std&colon;&colon;string&period;

== Synopsis

Declared in `&lt;absl&sol;strings&sol;str&lowbar;join&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;
    typename Iterator,
    typename Formatter&gt;
std::string
StrJoin(
    Iterator start,
    Iterator end,
    std::string&lowbar;view sep,
    Formatter&& fmt);
----

== Description

`absl&colon;&colon;StrJoin()` takes a range, a separator string to use between the elements joined, and an optional Formatter responsible for converting each argument in the range to a string&period;

If omitted, the default `AlphaNumFormatter()` is called on the elements to be joined&period;

Example 1&colon; &sol;&sol; Joins a collection of strings&period; This pattern also works with a collection &sol;&sol; of `absl&colon;&colon;string&lowbar;view` or even `const char&ast;`&period; std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v &equals; &lcub;&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;&rcub;&semi; std&colon;&colon;string s &equals; absl&colon;&colon;StrJoin(v, &quot;&hyphen;&quot;)&semi; EXPECT&lowbar;EQ(s, &quot;foo&hyphen;bar&hyphen;baz&quot;)&semi;

Example 2&colon; &sol;&sol; Joins the values in the given `std&colon;&colon;initializer&lowbar;list&lt;&gt;` specified using &sol;&sol; brace initialization&period; This pattern also works with an initializer&lowbar;list &sol;&sol; of ints or `absl&colon;&colon;string&lowbar;view` &hyphen;&hyphen; any `AlphaNum`&hyphen;compatible type&period; std&colon;&colon;string s &equals; absl&colon;&colon;StrJoin(&lcub;&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;&rcub;, &quot;&hyphen;&quot;)&semi; EXPECT&lowbar;EQs, &quot;foo&hyphen;bar&hyphen;baz&quot;)&semi;

Example 3&colon; &sol;&sol; Joins a collection of ints&period; This pattern also works with floats, &sol;&sol; doubles, int64s &hyphen;&hyphen; any `StrCat()`&hyphen;compatible type&period; std&colon;&colon;vector&lt;int&gt; v &equals; &lcub;1, 2, 3, &hyphen;4&rcub;&semi; std&colon;&colon;string s &equals; absl&colon;&colon;StrJoin(v, &quot;&hyphen;&quot;)&semi; EXPECT&lowbar;EQ(s, &quot;1&hyphen;2&hyphen;3&hyphen;&hyphen;4&quot;)&semi;

Example 4&colon; &sol;&sol; Joins a collection of pointer&hyphen;to&hyphen;int&period; By default, pointers are &sol;&sol; dereferenced and the pointee is formatted using the default format for &sol;&sol; that type&semi; such dereferencing occurs for all levels of indirection, so &sol;&sol; this pattern works just as well for `std&colon;&colon;vector&lt;int&ast;&ast;&gt;` as for &sol;&sol; `std&colon;&colon;vector&lt;int&ast;&gt;`&period; int x &equals; 1, y &equals; 2, z &equals; 3&semi; std&colon;&colon;vector&lt;int&ast;&gt; v &equals; &lcub;&amp;x, &amp;y, &amp;z&rcub;&semi; std&colon;&colon;string s &equals; absl&colon;&colon;StrJoin(v, &quot;&hyphen;&quot;)&semi; EXPECT&lowbar;EQ(s, &quot;1&hyphen;2&hyphen;3&quot;)&semi;

Example 5&colon; &sol;&sol; Dereferencing of `std&colon;&colon;unique&lowbar;ptr&lt;&gt;` is also supported&colon; std&colon;&colon;vector&lt;std&colon;&colon;unique&lowbar;ptr&lt;int&gt;&gt; v v&period;emplace&lowbar;back(new int(1))&semi; v&period;emplace&lowbar;back(new int(2))&semi; v&period;emplace&lowbar;back(new int(3))&semi; std&colon;&colon;string s &equals; absl&colon;&colon;StrJoin(v, &quot;&hyphen;&quot;)&semi; EXPECT&lowbar;EQ(s, &quot;1&hyphen;2&hyphen;3&quot;)&semi;

Example 6&colon; &sol;&sol; Joins a `std&colon;&colon;map`, with each key&hyphen;value pair separated by an equals &sol;&sol; sign&period; This pattern would also work with, say, a &sol;&sol; `std&colon;&colon;vector&lt;std&colon;&colon;pair&lt;&gt;&gt;`&period; std&colon;&colon;map&lt;std&colon;&colon;string, int&gt; m &equals; &lcub; &lcub;&quot;a&quot;, 1&rcub;, &lcub;&quot;b&quot;, 2&rcub;, &lcub;&quot;c&quot;, 3&rcub;&rcub;&semi; std&colon;&colon;string s &equals; absl&colon;&colon;StrJoin(m, &quot;,&quot;, absl&colon;&colon;PairFormatter(&quot;&equals;&quot;))&semi; EXPECT&lowbar;EQ(s, &quot;a&equals;1,b&equals;2,c&equals;3&quot;)&semi;

Example 7&colon; &sol;&sol; These examples show how `absl&colon;&colon;StrJoin()` handles a few common edge &sol;&sol; cases&colon; std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v&lowbar;empty&semi; EXPECT&lowbar;EQ(absl&colon;&colon;StrJoin(v&lowbar;empty, &quot;&hyphen;&quot;), &quot;&quot;)&semi;

std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v&lowbar;one&lowbar;item &equals; &lcub;&quot;foo&quot;&rcub;&semi; EXPECT&lowbar;EQ(absl&colon;&colon;StrJoin(v&lowbar;one&lowbar;item, &quot;&hyphen;&quot;), &quot;foo&quot;)&semi;

std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v&lowbar;empty&lowbar;string &equals; &lcub;&quot;&quot;&rcub;&semi; EXPECT&lowbar;EQ(absl&colon;&colon;StrJoin(v&lowbar;empty&lowbar;string, &quot;&hyphen;&quot;), &quot;&quot;)&semi;

std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v&lowbar;one&lowbar;item&lowbar;empty&lowbar;string &equals; &lcub;&quot;a&quot;, &quot;&quot;&rcub;&semi; EXPECT&lowbar;EQ(absl&colon;&colon;StrJoin(v&lowbar;one&lowbar;item&lowbar;empty&lowbar;string, &quot;&hyphen;&quot;), &quot;a&hyphen;&quot;)&semi;

std&colon;&colon;vector&lt;std&colon;&colon;string&gt; v&lowbar;two&lowbar;empty&lowbar;string &equals; &lcub;&quot;&quot;, &quot;&quot;&rcub;&semi; EXPECT&lowbar;EQ(absl&colon;&colon;StrJoin(v&lowbar;two&lowbar;empty&lowbar;string, &quot;&hyphen;&quot;), &quot;&hyphen;&quot;)&semi;

Example 8&colon; &sol;&sol; Joins a `std&colon;&colon;tuple&lt;T&period;&period;&period;&gt;` of heterogeneous types, converting each to &sol;&sol; a std&colon;&colon;string using the `absl&colon;&colon;AlphaNum` class&period; std&colon;&colon;string s &equals; absl&colon;&colon;StrJoin(std&colon;&colon;make&lowbar;tuple(123, &quot;abc&quot;, 0&period;456), &quot;&hyphen;&quot;)&semi; EXPECT&lowbar;EQ(s, &quot;123&hyphen;abc&hyphen;0&period;456&quot;)&semi;

== Return Value

The joined string&period;

== Parameters

[cols="1,4"]
|===
| Name| Description
| *start*
| Iterator to the first element to join&period;
| *end*
| Iterator past the last element to join&period;
| *sep*
| The separator placed between joined elements&period;
| *fmt*
| The formatter used to convert each element to a string&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
