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
This is a discussion on Splitting string in chunks of 2 characters within the C++ Programming forums, part of the General Programming Boards category; I have a string of type std::string, and I want to split it so that 210100 would become 21 01 ...
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)); } }
I never put signature, but I decided to make an exception.
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.
I never put signature, but I decided to make an exception.
It should be:
Code:void split(const std::string& str, std::vector<std::string>& out)
I never put signature, but I decided to make an exception.
Is there a way to make a vector an array of strings?
Thanks
vector IS an array of string
I never put signature, but I decided to make an exception.
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.
I never put signature, but I decided to make an exception.