Build Our Own Algorithm: Split
Learn to build our algorithm: split.
We'll cover the following...
The STL has a rich algorithm
library. Yet, on occasion we may find it missing something we need. One common need is a split
function.
A split
function splits a string on a character separator. For example, here's a Unix /etc/passwd
file from a standard Debian installation:
root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/dev:/usr/sbin/nologinsync:x:4:65534:sync:/bin:/bin/sync
Each field is separated by a colon :
character, where the fields are:
Login name
Optional encrypted password
User ID
Group ID
Username or comment
Home directory
Optional command interpreter
This is a standard file in POSIX-based operating ...