将字符串转换为数组 [英] convert string into an array

查看:108
本文介绍了将字符串转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试转换类似这样的字符串

" {201,23,240,56,23,45,34,23} "用C ++写成数组

请帮忙。

谢谢,

Sudzzz

Hi,
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.
Thanks,
Sudzzz

推荐答案

Sudzzz写道:
Sudzzz wrote:

我正在尝试转换类似这样的字符串

" {201,23,240,56,23,45,34,23}"用C ++编译成数组

请帮助。
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.



我们很乐意提供帮助。到目前为止你做了什么以及

是什么问题?阅读常见问题解答,尤其是第5节。


V

-

请在回复时删除资金''A'通过电子邮件

我没有回复最热门的回复,请不要问

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


7月3日,3:41下午,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Jul 3, 3:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Sudzzz写道:
Sudzzz wrote:

我'我试图转换像这样的字符串

" {201,23,240,56,23,45,34,23}"用C ++编译成数组

请帮助。
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.



我们很乐意提供帮助。到目前为止你做了什么以及

是什么问题?阅读常见问题解答,尤其是第5节。


V

-

请在回复时删除资金''A'通过电子邮件

我没有回复顶部回复,请不要问


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



目前我有一个字符串" {201,23,240,56,23,45,34,23}"它频繁地改变了b $ b(可能是3Hz)。这些数字可以是任何数字,其中
小于4位数。我想把这个字符串放在一个

数组中,以便我可以用于某些计算目的。我已经通过不同的可能性来查看
,其中一个将涉及

广泛的字符串比较,atoi(),附加到数组类型

算法。然而我意识到,当我们输入数组时,c ++以与上面相同的形式接受它,然后我想是否可以模拟c

++输入此字符串时一个数组。

我不知道从哪里开始。

谢谢,

Sudzzz

http://sudeeppillai.blogspot.com

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.
I don''t know where to start.
Thanks,
Sudzzz

http://sudeeppillai.blogspot.com

< br>

Sudzzz写道:
Sudzzz wrote:

7月3日下午3:41,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Jul 3, 3:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

> Sudzzz写道:
>Sudzzz wrote:

>>我正在尝试转换类似这样的字符串
" {201,23,240,56,23,45,34,23}"用C ++编译成数组
请帮忙。
>>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.


我们很乐意提供帮助。到目前为止你做了什么,问题是什么?阅读常见问题解答,尤其是第5节。

V
-
请在通过电子邮件回复时删除资金A
我不回复要回复最多,请不要问


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



目前我有一个字符串{201,23,240,56,23,45,34,23} "它频繁地改变了b $ b(可能是3Hz)。这些数字可以是任何数字,其中
小于4位数。我想把这个字符串放在一个

数组中,以便我可以用于某些计算目的。我已经通过不同的可能性来查看
,其中一个将涉及

广泛的字符串比较,atoi(),附加到数组类型

算法。但是我意识到,当我们输入数组时,c ++以与上面相同的形式接受它,然后我想是否可以模拟

c ++输入这个字符串阵列。


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.



是的,你可以模拟C ++输入,它会涉及一些角色

比较,atoi(或strtol,更喜欢它),附加到数组,类似于
算法。您认为编译器如何做到这一点?

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.



从这里开始:


#include< string>

#include< vector> ;

#include< cassert>


int main(){

std :: string mystring(" {201 ,23,240,56,23,45,34,23}");

std :: vector< intvint;


//编写要提取的代码来自''mystring''的数字

//并将它们放入''vint''

断言(vint.size()== 8) ;

断言(vint [0] == 201);

断言(vint [1] == 23);

断言(vint [2] == 240);

断言(vint [3] == 56);

断言(vint [4] == 23);

断言(vint [5] == 45);

断言(vint [6] == 34);

断言(vint [7] == 23);

}


V

-

请删除大写''A'通过电子邮件回复

我没有回复最热门的回复,请不要问

Start here:

#include <string>
#include <vector>
#include <cassert>

int main() {
std::string mystring("{201,23,240,56,23,45,34,23}");
std::vector<intvint;

// 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


这篇关于将字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆