string - > INT [] [英] string --> int[]

查看:63
本文介绍了string - > INT []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好NG!


虽然我根本不是编程的新手,但我是C ++的新手。


得到了以下问题/问题的行为:


- 从键盘读取数字*任意位数*(完成)

- 然后计算平均值和各种各样的数字


第二个也没问题,我想。


问题如下:


如何从每个元素的读取字符串创建一个int []

表示字符串中的*一个*数字?


i尝试了这个:

-------------------------------------- -

#include< iostream>

#include< stdlib.h>

#include< math.h>

#include< String.h>


使用命名空间std;


int main(int argc,char * argv [])

{


string a;


cout<< Beliebige Ziffernfolge eingeben << endl;

cin>> a;


int b [sizeof(a)-1];


for(byte i = 0; i< sizeof(a )-1; i ++){

b [i] = atoi(& a [i]);

}


for(byte i = 0; i< sizeof(a)-1; i ++){

cout<< b [i]<< endl;

}


cout<< endl;

system(PAUSE);

返回0;

}

---- ------------------------------------


doesn'工作!对于a =123,我得到:

b [0] = 123

b [1] = 23

b [2] = 3


而不是(我需要的):

b [0] = 1

b [1] = 2

b [2] = 3

我的失败是什么?


问候,

弗兰克

Hello NG!

Though I′m not new to programming at all, i′m new to C++.

got the following question/problem with it′s behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
- then calculate average value and variety of digits

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <String.h>

using namespace std;

int main(int argc, char *argv[])
{

string a;

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];

for (byte i = 0; i < sizeof(a)-1; i++) {
b[i] = atoi(&a[i]);
}

for (byte i = 0; i < sizeof(a)-1; i++) {
cout << b[i] << endl;
}

cout << endl;
system("PAUSE");
return 0;
}
----------------------------------------

doesn′t work! for a="123" i get:
b[0] = 123
b[1] = 23
b[2] = 3

instead of (what i would need):
b[0] = 1
b[1] = 2
b[2] = 3
what′s my failure?

greetings,
Frank

推荐答案

2003年10月15日星期三23:03:13 +0200,Frank Wagner

< ne ************* ****@nofusion.de>写道:
On Wed, 15 Oct 2003 23:03:13 +0200, Frank Wagner
<ne*****************@nofusion.de> wrote:
Hello NG!

虽然我根本不是编程新手,但我是C ++的新手。
<得到了以下问题/问题?行为:

- 从键盘读取数字*任意位数*(完成)
- 然后计算平均值和种类我觉得第二个也没问题。

问题如下:

我怎样才能创建一个int []从我的读取字符串中每个元素
表示*一个*来自字符串的数字?

我试过这个:
------------- ---------------------------
#include< iostream>
#include< stdlib.h>
#include< math.h>
#include< String.h>

使用命名空间std;

int main(int argc,char * argv [])
{

字符串a;

cout<< Beliebige Ziffernfolge eingeben << endl;
cin>> a;

int b [sizeof(a)-1];

for(byte i = 0; i< sizeof(a)-1; i ++){
b [i] = atoi(& a [i]);
}
for(byte i = 0; i< sizeof(a)-1; i ++) {
cout<< b [i]<< endl;
}

cout<< endl;
system(PAUSE);
返回0;
}
-------------------- --------------------

不起作用!对于a =123,我得到:
b [0] = 123
b [1] = 23
b [2] = 3

而不是(我需要的):
b [0] = 1
b [1] = 2
b [2] = 3

什么?我的失败?


atoi接受const char *并在遇到非数字时结束转换

字符,


转换字符到整数 - 你应该只使用表达式数字 - ''0''

b [i] = a [i] - ''0'';

而不是atoi in上面的循环。


问候,
弗兰克
Hello NG!

Though I?′m not new to programming at all, i?′m new to C++.

got the following question/problem with it?′s behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
- then calculate average value and variety of digits

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <String.h>

using namespace std;

int main(int argc, char *argv[])
{

string a;

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];

for (byte i = 0; i < sizeof(a)-1; i++) {
b[i] = atoi(&a[i]);
}

for (byte i = 0; i < sizeof(a)-1; i++) {
cout << b[i] << endl;
}

cout << endl;
system("PAUSE");
return 0;
}
----------------------------------------

doesn?′t work! for a="123" i get:
b[0] = 123
b[1] = 23
b[2] = 3

instead of (what i would need):
b[0] = 1
b[1] = 2
b[2] = 3
what?′s my failure?
atoi takes const char* and ends conversion when it encounters non digit
character,

To convert char to integer - you should just use expression digit -''0''
b[i] = a[i]-''0'';
instead of atoi in the loop above.


greetings,
Frank




-

grzegorz



--
grzegorz


Frank Wagner写道:
Frank Wagner wrote:
Hello NG!

虽然我是根本不是编程新手,我是C ++的新手。

对它的行为有以下疑问/问题:

- 用*任意数字读取数字数字*来自键盘(完成)
- 然后计算平均值和各种数字

第二个也不成问题,我认为。

问题如下:

如何从我的读取字符串中创建一个int [],每个元素
表示来自字符串的*一个*数字?

我试过这个:
--------------------------------- -------
#include< iostream>
#include< stdlib.h>
#include< math.h>
#include< String .h>

使用命名空间std;

{

字符串a ;

cout<< Beliebige Ziffernfolge eingeben << endl;
cin>> a;

int b [sizeof(a)-1];

for(byte i = 0; i< sizeof(a)-1; i ++){
b [i] = atoi(& a [i]);
Hello NG!

Though I′m not new to programming at all, i′m new to C++.

got the following question/problem with it′s behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
- then calculate average value and variety of digits

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <String.h>

using namespace std;

int main(int argc, char *argv[])
{

string a;

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];

for (byte i = 0; i < sizeof(a)-1; i++) {
b[i] = atoi(&a[i]);



你的失败就在这里。


假设一个std :: string将其字符保存在一个字节数组中(

我不知道 - 专家?)...你正在做的是传递atoi你的

整个字符串减去第一个''i'字符。因此,您的第一个

迭代传递123,您的第二个23,您的第三个3......然后您获得

您正在查看的结果。


您可以:


a)假设您正在使用ASCII和b [i] = a [i] - ' '0''" ;.这个

将在使用0-9不并发的字符集的系统上中断。

b)创建一个char [2]数组并用[i]填充, ''\'''并传递给

atoi ....这将永远有效。


NR


Your failure is here.

Assuming that a std::string keeps its characters in a byte array (which
I don''t know - experts?)...What you are doing is passing atoi your
entire string minus the first ''i'' characters. Therefor your first
iteration passes "123", your second "23", your third "3"...and you get
the results you are seing.

You could:

a) Assume that you are working with ASCII and "b[i] = a[i] - ''0''". This
will break on systems that use a character set where 0-9 is not concurrent.
b) create a char[2] array and fill with a[i], ''\0'' and pass this to
atoi....this will always work.

NR


2003年10月15日星期三23:03:13 +0200,Frank Wagner< ne ***************** @nofusion。德>写道:
On Wed, 15 Oct 2003 23:03:13 +0200, Frank Wagner <ne*****************@nofusion.de> wrote:
Hello NG!

虽然我根本不是编程新手,但我是C ++的新手。
得到以下问题/问题的行为:

- 从键盘读取数字*任意位数*(完成)


专注于数字,这是一串字符。


- 然后计算平均值和各种数字


品种?
<我觉得第二个也没问题。

问题如下:

如何从我的读取字符串创建一个int []每个元素
表示*一个*来自字符串?



std :: vector< int> v;

for(std :: size_t i = 0; i< s.size(); ++ i)

{

v.push_back(s [i] - ''\''');

}


i试过这个:
---- ------------------------------------
#include< iostream>
#include< stdlib.h>


使用

#include< cstdlib>

#include< math.h>


使用

#include< cmath>


#include< String.h>


< string.h>与< string>


使用命名空间std不同的标题;

int main(int argc,char * argv [])
{

字符串a;


如果这个编译你很不走运(它可能会取决于< vector> drags

in)。
cout<< Beliebige Ziffernfolge eingeben << endl;
cin>> a;

int b [sizeof(a)-1];


你需要a.size(),而不是sizeof(a)。前者告诉你字符串中存储的字符数

,后者是容器变量的大小(以字节为单位)

(通常只保存指向字符缓冲区的指针) ,加上一些其他的

值,例如size()成员函数返回的当前长度。

for(byte i = 0; i< ; sizeof(a)-1; i ++){


标准C ++中没有预定义的数据类型''byte'。


另外,使用''++ i'',而不是''i ++''。

b [i] = atoi(& a [i]);


''atoi''期望一个零终止的数字字符串,你给它

它,即a [i]和所有跟随字符直到终止零...

}

for(byte i = 0; i< sizeof(a)-1; i ++){


见上文。


cout<< b [i]<< endl;
}

cout<< endl;
system(PAUSE);


这只适用于带有PAUSE的系统。命令(例如Windows)和

它会使自动测试程序稍微困难,或者在自动化中使用它来获得


返回0; $ b $b¨

好​​的,但你不需要它(''main''的一个特殊功能是它默认返回0

)。

}
------------------------------------- ---

不起作用!对于a =123,我得到:
b [0] = 123
b [1] = 23
b [2] = 3

而不是(我需要的):
b [0] = 1
b [1] = 2
b [2] = 3

我的失败是什么?
Hello NG!

Though I′m not new to programming at all, i′m new to C++.

got the following question/problem with it′s behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
Concentrate on the digits, which is a string of characters.

- then calculate average value and variety of digits
Variety?

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

std::vector<int> v;
for( std::size_t i = 0; i < s.size(); ++i )
{
v.push_back( s[i] - ''\0'' );
}

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
Use
#include <cstdlib>
#include <math.h>
Use
#include <cmath>

#include <String.h>
<string.h> is not the same header as <string>


using namespace std;

int main(int argc, char *argv[])
{

string a;
You''re unlucky if this compiles (it might, depending on what <vector> drags
in).

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];
You need a.size(), not sizeof(a). The former tells you the number of characters
stored in the string, the latter the size in bytes of the container variable
(which typically holds just a pointer to the character buffer, plus a few other
values such as the current length returned by the size() member function).


for (byte i = 0; i < sizeof(a)-1; i++) {
There''s no predefined data type ''byte'' in standard C++.

Also, use ''++i'', not ''i++''.
b[i] = atoi(&a[i]);
''atoi'' expects a zero-terminated string of digits, which you''re giving
it, namely a[i] and all following characters until the terminating zero...
}

for (byte i = 0; i < sizeof(a)-1; i++) {
See above.

cout << b[i] << endl;
}

cout << endl;
system("PAUSE");
This will only work on a system with a "PAUSE" command (e.g. Windows), and
it will make it slightly more difficult to test the program automatically, or
to use it in automation.

return 0; ¨
OK, but you don''t need it (a special feature of ''main'' is that it returns 0
by default).
}
----------------------------------------

doesn′t work! for a="123" i get:
b[0] = 123
b[1] = 23
b[2] = 3

instead of (what i would need):
b[0] = 1
b[1] = 2
b[2] = 3
what′s my failure?




那个,我不知道...... ;-)



That, I don''t know... ;-)


这篇关于string - &gt; INT []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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