A basic Version is this one:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using std::string;
using std::cout;
void base_ansi_c()
{
const char * source = "one\ntwo\nthree\n";
char target[1024];
const char * c;
for (c = source; c && *c != 0 && *c != '\n'; ++c); //skip until first '\n'
if (c && *c == '\n') ++c; // skip '\n'
int n = 0;
for (; c && *c != 0 && *c != '\n' && n < 1024; ++c, ++n) //copy until second '\n'
{
target[n] = *c;
}
target[n] = 0;
cout << "'" << target << "'\n";
}
void base_cpp_sstream()
{
std::vector<string> cpp_array;
std::string test = "one\ntwo\nthree\n";
std::istringstream is(test);
std::string part;
while (std::getline(is, part, '\n'))
{
cpp_array.emplace_back(part);
std::cout << " " << part << " ";
}
std::cout << "\n";
std::cout << "The second element is: '" << cpp_array.at(1) << "'\n";
}
int main()
{
std::cout << "\nbase_cpp_sstream\n";
base_cpp_sstream();
std::cout << "\nbase_ansi_c\n";
base_ansi_c();
}
If it gets more complex you may want to switch to either boost::tokenizer
or boost::spirit::qi
.
Since you asked for a different version where the positions are more relevant to the algorithm I added an ansi-c style iterating through the string.
Edit: Some detail about the code above as you requested in the comment. I think explaining this line for (c = source; c && *c != 0 && *c != '\n'; ++c);
will be enough to get you started.
The statement c = source
just copies the pointer at the beginning of the loop.
At every iteration including the first the following is checked: c && *c != 0 && *c != '\n'
The detail here is that c
will be dereferenced so it can not be (char*)0
. 0
is false
, every other value is true
. If c is false the chained together values using &&
are not evaluated. So there is no risk of dereferencing a null pointer. *c != 0
checks if the end of the c-string is reached. In that case no \n
was found. *c != '\n'
checks if a new line character is reached.
At the end of an iteration the pointer is incremented. ++c
this moves the c
pointer to the next element of the array. Since c is a char the pointer is incremented by 1. sizeof(char)
evaluates to 1. (always). If the array elements were of a different type the increment would also move the pointer to the next element. (does not work with void*).
If '\n'
is found there is no increment afterwards. The for loop just ends. since c was declared outside of the loop the value remains preserved and can be used by the subsequent algorithms.