| 1 |
#include <fstream> |
| 2 |
#include <iostream> |
| 3 |
#include <string> |
| 4 |
#include <algorithm> |
| 5 |
#include <iterator> |
| 6 |
|
| 7 |
using namespace std; |
| 8 |
|
| 9 |
string *nStrings(size_t size, char const *fname) |
| 10 |
{ |
| 11 |
static thread_local ifstream in; |
| 12 |
|
| 13 |
struct Xstr: public string |
| 14 |
{ |
| 15 |
Xstr() |
| 16 |
: |
| 17 |
string(nextLine()) |
| 18 |
{} |
| 19 |
static string nextLine() |
| 20 |
{ |
| 21 |
string line; |
| 22 |
|
| 23 |
getline(in, line); |
| 24 |
return line; // copy elision turns this |
| 25 |
} // into Xstr's base class string |
| 26 |
}; |
| 27 |
in.open(fname); |
| 28 |
string *sp = new Xstr[size]; |
| 29 |
in.close(); |
| 30 |
|
| 31 |
return sp; |
| 32 |
} |
| 33 |
|
| 34 |
int main() |
| 35 |
{ |
| 36 |
string *sp = nStrings(10, "nstrings.cc"); |
| 37 |
copy(sp, sp + 10, ostream_iterator<string>{ cout, "\n" }); |
| 38 |
} |