I have a string of type std::string, and I want to split it so that 210100 would become 21 01 00 in an array of strings of type:
However, I have no idea of how to do this in C++Code:std::string name[] = {"21","01","00"};
Thanks in advance
I have a string of type std::string, and I want to split it so that 210100 would become 21 01 00 in an array of strings of type:
However, I have no idea of how to do this in C++Code:std::string name[] = {"21","01","00"};
Thanks in advance
Code:void split(const std::string str, std::vector<std::string>& out) { out.clear(); out.reserve(str.length() / 2 + str.length() % 1); for (size_t i = 0; i < str.length(); i += 2) { out.push_back(std::string(str, i, 2)); } }
Thanks much, I have a few questions though.
This function works with two arguments, the original string and the final string (result). It seems to be like the out string is of type vector, but what it is. I also want to know if there's any way I can make it a std::string.
Thanks much
You want to have an array of strings, vector is one.
It should be:
Code:void split(const std::string& str, std::vector<std::string>& out)
Is there a way to make a vector an array of strings?
Thanks
vector IS an array of string
Is this a vector?Code:std::string greetings[] = {"hey","hi","hello"};
This is NOT a vector. This is an array, but it's something different. This is a built-in array type and it has fixed size. Vector is an array too, but it's a dynamic one, which can grow and shrink in size. You could split your string to this kind of array too, but you will have to set limit for length.