Let me describe this code the way I see it:
1 2 3 4 5 6 7 8 9
|
vector<int> octets;
stringstream ss(ip);
string temp;
while (getline(ss,temp,'.'))
octets.push_back(atoi(temp.c_str()));
for (unsigned i=0; i< octets.size(); i++)
cout << octets[i] << endl;
|
Line 1. Define an empty vector called
octets
. This is like an array which can dynamically resize as and when required. It will hold values of type int.
Line 2. Define a stringstream called
ss
. Load into it the existing string
ip
.
Line 3. Define an empty string called
temp
.
That was just the preparation. Now things get more interesting.
Line 5.
getline(ss,temp,'.')
read a line of text from the stream
ss
into the string
temp
. The end of each line is delimited by the
'.'
character or the end of the stream.
The
while
loop will repeat until all the lines have been read from the stream.
Line 6.
temp.c_str()
convert the std::string into a null-terminated c-string
atoi()
convert the contents of the c-string into an integer.
octets.push_back()
add the value to the vector.
Lines 8 and 9 simply output each entry in the vector.