Re: convert string into an array
Sudzzz wrote:
On Jul 3, 3:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Sudzzz wrote:
I'm trying to convert a string something like this
"{201,23,240,56,23,45,34,23}" into an array in C++
Please help.
We're happy to help. What have you done so far and what
are the problems? Read the FAQ, especially section 5.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Currently I have a string "{201,23,240,56,23,45,34,23}" which changes
frequently (possibly at 3Hz). The numbers can be any number having
less than 4 digits. I want to take this string and place it in an
array so that I can use for certain computational purposes. I've
looked through different possibilities one of which would involve an
extensive string comparison, atoi(), append to array sort of
algorithm. However I realized that when we input arrays c++ accepts it
in the same form as above and then I thought whether I could simulate
c ++ input this string when asked for an array.
Yes, you can simulate C++ input, and it will involve some character
comparison, atoi (or strtol, more like it), append to array, sort of
algorithm. How do you think the compiler does that?
I don't know where to start.
Start here:
#include <string>
#include <vector>
#include <cassert>
int main() {
std::string mystring("{201,23,240,56,23,45,34,23}");
std::vector<int> vint;
// write code to extract numbers from the 'mystring'
// and place them into 'vint'
assert(vint.size() == 8);
assert(vint[0] == 201);
assert(vint[1] == 23);
assert(vint[2] == 240);
assert(vint[3] == 56);
assert(vint[4] == 23);
assert(vint[5] == 45);
assert(vint[6] == 34);
assert(vint[7] == 23);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask