A Deeper Look at the std::string Class

Splitting a String into a Vector

How can I split a std::string into a vector of substrings based on a delimiter?

Abstract art representing computer programming

Splitting a string into substrings is a common task in text processing. While C++ doesn't have a built-in split() function like some other languages, we can implement one using the standard library.

Here's an efficient way to split a std::string into a std::vector of substrings based on a delimiter:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

std::vector<std::string> split(
  const std::string& s, char delimiter) {
  std::vector<std::string> tokens;
  std::string token;
  std::istringstream tokenStream(s);
  while (std::getline(
    tokenStream, token, delimiter
  )) {
    if (!token.empty()) {
  return tokens;

int main() {
  std::string text{"Hello,World,C++,Programming"};
  char delimiter{','};

  std::vector<std::string> result{
      split(text, delimiter)};  

  std::cout << "Original string: " << text << '\n';
  std::cout << "Substrings:\n";
  for (const auto& str : result) {
    std::cout << "  " << str << '\n';
Original string: Hello,World,C++,Programming

Let's break down the split() function:

  1. We create a std::vector<std::string> to store our substrings.
  2. We use a std::istringstream to treat our input string as a stream of characters.
  3. We use std::getline() with our chosen delimiter to extract substrings.
  4. We check if the extracted substring is not empty before adding it to our vector.

This method is efficient because it avoids manual string manipulation and uses the stream extraction capabilities of C++. It handles empty substrings (like between consecutive delimiters) gracefully by ignoring them.

For more complex splitting needs, you might consider using regular expressions (<regex> header). However, for simple delimiter-based splitting, this method is usually more than sufficient and often more performant.

Remember, if you're working with very large strings or need to split strings frequently, you might want to consider passing the vector by reference to avoid copying:

void split(
  const std::string& s,
  char delimiter,
  std::vector<std::string>& tokens
) {
  // Same implementation as before,
  // but using tokens directly

This can be more efficient as it avoids creating and copying the vector when returning from the function.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved