0

I'm trying to find a way to split a string based on space, but haven't been able to find any simple or working solutions online. In python, we had a .split() function that worked easily, but not in C++.

For example, I'm trying convert this string:

"I love 29 pies"

to this vector:

{"I", "love", "29", "pies"}

Thanks in advance!

  • 3
    Did you try `std::istringstream` and `operator >>`? It is literally three lines of code. You also didn't state what you tried. – PaulMcKenzie Jan 28 '20 at 00:10
  • If you're looking for something elegant like python, then you're in the wrong language. There just isn't that sort of library to C++. Maybe you could find some library that someone made but otherwise you need to write that function yourself. But that's always great too! That's how you get to learn how these seemingly simple things work. Python is written in C. So if you try writing it yourself, you might get to understand how that split function works. – Warpstar22 Jan 28 '20 at 00:10
  • You could use [boost's string library](https://theboostcpplibraries.com/boost.stringalgorithms). It contains `boost::algorithm::split()` – Thomas Sablik Jan 28 '20 at 07:57

0 Answers0