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()) {
tokens.push_back(token);
}
}
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
Substrings:
Hello
World
C++
Programming
Let's break down the split()
 function:
std::vector<std::string>
to store our substrings.std::istringstream
to treat our input string as a stream of characters.std::getline()
with our chosen delimiter to extract substrings.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.
std::string
ClassA detailed guide to std::string
, covering the most essential methods and operators