rcpputils  master
C++ API providing common utilities and data structures.
join.hpp
Go to the documentation of this file.
1 // Copyright 2019 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #ifndef RCPPUTILS__JOIN_HPP_
20 #define RCPPUTILS__JOIN_HPP_
21 
22 #include <algorithm>
23 #include <iterator>
24 #include <sstream>
25 #include <string>
26 
27 namespace rcpputils
28 {
29 
31 
40 template<
41  typename CharT, typename ValueT, typename AllocatorT,
42  template<typename T, class A> class ContainerT>
44 join(const ContainerT<ValueT, AllocatorT> & container, const CharT * delim)
45 {
47  std::copy(container.begin(), container.end(), std::ostream_iterator<ValueT, CharT>(s, delim));
48  std::basic_string<CharT> result = s.str();
49  if (delim) {
50  result = result.substr(0, result.length() - strlen(delim));
51  }
52  return result;
53 }
54 
55 } // namespace rcpputils
56 
57 #endif // RCPPUTILS__JOIN_HPP_
std::basic_string
rcpputils
Definition: asserts.hpp:37
rcpputils::join
std::basic_string< CharT > join(const ContainerT< ValueT, AllocatorT > &container, const CharT *delim)
Join values in a container turned into strings by a given delimiter.
Definition: join.hpp:44
std::basic_string::length
T length(T... args)
std::copy
T copy(T... args)
std::basic_string::substr
T substr(T... args)
std::basic_ostringstream
std::ostream_iterator
std::basic_ostringstream::str
T str(T... args)