cpp split string by space 1

cpp split string by space

std::vector string_split(const std::string& str) {
	std::vector result;
	std::istringstream iss(str);
	for (std::string s; iss >> s; )
		result.push_back(s);
	return result;
}

Here is what the above code is Doing:
1. We’re defining a function called string_split that takes a string as an argument.
2. We’re creating a vector of strings called result.
3. We’re creating an istringstream object called iss that takes the string we passed to the function as an argument.
4. We’re creating a for loop that iterates over the iss object.
5. We’re pushing each word in the iss object to the result vector.
6. We’re returning the result vector.

Similar Posts