...

/

Build Our Own Algorithm: Split

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/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync

Each field is separated by a colon : character, where the fields are:

  1. Login name

  2. Optional encrypted password

  3. User ID

  4. Group ID

  5. Username or comment

  6. Home directory

  7. Optional command interpreter

This is a standard file in POSIX-based operating ...