📅  最后修改于: 2023-12-03 14:39:38.092000             🧑  作者: Mango
splitlines()
is a built-in function in C++ that can be used to split a string into a vector of substrings based on a specified delimiter. This function is part of the standard library in C++, so no additional installation is required.
The prototype of splitlines()
is as follows:
vector<string> splitlines(string str, char delimiter = '\n');
The function takes in two parameters:
str
: The string to be split.delimiter
: The delimiter to be used. By default, it is set to newline (\n
).The function returns a vector<string>
that contains all the substrings split from the original string.
Here is an example of how to use splitlines()
:
#include <iostream>
#include <vector>
using namespace std;
vector<string> splitlines(string str, char delimiter = '\n') {
vector<string> result;
string line;
for (char c : str) {
if (c == delimiter) {
result.push_back(line);
line = "";
} else {
line += c;
}
}
if (line.length() > 0) {
result.push_back(line);
}
return result;
}
int main() {
string str = "Hello\nWorld\nHow are you?";
vector<string> lines = splitlines(str);
for (string line : lines) {
cout << line << endl;
}
return 0;
}
The above code will output:
Hello
World
How are you?
In this example, we created our own implementation of splitlines()
that splits the string based on the delimiter. We then used this function to split a string into separate lines and output each line on a new line.
splitlines()
is a useful built-in function in C++ that makes it easy to split a string into substrings based on a delimiter. With this function, you can easily process and manipulate text data in your programs.