string_vectorio - TextIO Derivative using vector<string>

Introduction

This package is very similar to the stringio package. It allows redirection of output or sourcing of input from a vector of strings. It follows the convention that each string in the vector is a line of text. This means that when output is redirected to a string vector, the result will be a vector with one string per line of output. Similarly, when a string vector is used as input, a newline is inserted at the end of each string in the vector. This means that the test eoln() has meaning.

Output String Vectors

Usage

The way of using string vector output devices is to used the derived class osvtext. This is a derivation of otext and can be used wherever otext would be used. For example:

osvtext my_output;

Once an output string device has been opened, it can be used as the base class otext device, giving access to all of the output chevron operators (<<). See the TextIO Documentation for details.

For example:

my_output << "Hello World" << endl << "I'm here" < endl;

This fills in the vector with two strings. The first string contains the text "Hello World", whilst the second contains the text "I'm here".

The internal vector which receives the output text can be accessed by the get_vector function.

vector<string>& result = my_output.get_vector();

Input String Vector

Usage

Input string vectors are used in a very similar way to outputs. The main difference is that, with input devices, the device needs to be initialised with the vector contents by passing a vector<string> to the constructor.

For example:

vector<string> data;
data.push_back("Hello World");
data.push_back("I'm here");
isvtext my_input(data);

This device can now be used as an input to a chevron expression to extract the elements of the strings. For example, the >> operator for a string gets the next non-whitespace sequence from a device. Thus the words can be split very easily:

string first_word;
my_input >> first_word;
string second_word;
my_input >> second_word;
string third_word;
my_input >> third_word;
string fourth_word;
my_input >> fourth_word;

The input string device has the concept of end-of-file in that the device eof() function returns true if all of the string has been read. It also supports the concept of end-of-line such that the eoln() function returns true if the next read would get the '\n' character - which is returned when the input device steps from one string in the vector to the next string.