-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01-basic.cpp
More file actions
58 lines (45 loc) · 1.36 KB
/
Copy path01-basic.cpp
File metadata and controls
58 lines (45 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Use nonstd::string's split():
#include "nonstd/string.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#if string_CPP11_000
template< typename T >
std::string contents(std::vector<T> const & coll)
{
// using nonstd::to_string() for nonstd::std17::string_view:
using nonstd::to_string;
std::stringstream os;
for ( auto const & elem : coll )
os << "'" << to_string(elem) << "', ";
return os.str();
}
#else
template< typename T >
std::string contents(std::vector<T> const & coll)
{
// using nonstd::to_string() for nonstd::std17::string_view:
using nonstd::to_string;
typename std::vector<T>::const_iterator pos = coll.begin();
typename std::vector<T>::const_iterator end = coll.end();
std::stringstream os;
for ( ; pos != end; ++pos )
os << "'" << to_string(*pos) << "', ";
return os.str();
}
#endif
template< typename T >
std::ostream & operator<<(std::ostream & os, std::vector<T> const & coll )
{
return os << "[" << contents(coll) << "]";
}
int main()
{
std::cout << nonstd::split("Hello, world", ", ");
}
// cl -nologo -EHsc -I../include 01-basic.cpp && 01-basic.exe
// clang-cl -EHsc -D_CRT_SECURE_NO_WARNINGS -I../include 01-basic.cpp && 01-basic.exe
// g++ -std=c++11 -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe
// Output:
// ['Hello', 'world', ]