~ubuntu-branches/debian/sid/libvcflib/sid

« back to all changes in this revision

Viewing changes to src/join.h

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2016-09-16 15:52:29 UTC
  • Revision ID: package-import@ubuntu.com-20160916155229-24mxrntfylvsshsg
Tags: upstream-1.0.0~rc1+dfsg
ImportĀ upstreamĀ versionĀ 1.0.0~rc1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __JOIN_H
 
2
#define __JOIN_H
 
3
 
 
4
// functions to split a string by a specific delimiter
 
5
#include <string>
 
6
#include <vector>
 
7
#include <sstream>
 
8
#include <string.h>
 
9
 
 
10
// join a vector of elements by a delimiter object.  ostream<< must be defined
 
11
// for both class S and T and an ostream, as it is e.g. in the case of strings
 
12
// and character arrays
 
13
template<class S, class T>
 
14
std::string join(std::vector<T>& elems, S& delim) {
 
15
    std::stringstream ss;
 
16
    typename std::vector<T>::iterator e = elems.begin();
 
17
    ss << *e++;
 
18
    for (; e != elems.end(); ++e) {
 
19
        ss << delim << *e;
 
20
    }
 
21
    return ss.str();
 
22
}
 
23
 
 
24
// same for lists
 
25
template<class S, class T>
 
26
std::string join(std::list<T>& elems, S& delim) {
 
27
    std::stringstream ss;
 
28
    typename std::list<T>::iterator e = elems.begin();
 
29
    ss << *e++;
 
30
    for (; e != elems.end(); ++e) {
 
31
        ss << delim << *e;
 
32
    }
 
33
    return ss.str();
 
34
}
 
35
 
 
36
#endif