帮助string到int [英] help with string to int

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

问题描述

#include< stdio.h>

#include< string.h>

#include< stdlib.h>

#define N 33

void StringToIntArray(const char * s1);

void takeinteger(int i);

int main()

{

char string [N];

printf(输入一个不超过32位的字符串:;);

scanf("%s",string);

StringToIntArray(string);


返回0;

}

void StringToIntArray(const char * s1)

{

int array [N];

int b,i;

for(b = 0; s1 [b]!=''\ 0''; ++ b)

{

i = 0;

i = atoi(s1 [b]);

数组[++ b] = i;

}


}

//我想把字符串'的第一个字符放到

数组的第一个元素然后到下一个等等

两件事阻碍了我的表现,演员,我的全球

变量和访问中提琴tions,如果有更好的方法或提示ID

欣赏它,替代方法,提示可能有时模糊

但任何帮助都会很棒
错误包括无法将参数1从''const char''转换为''const

char *

解决方案
<先生********* @ yahoo.com>写道:

#include< stdio.h>
#include< string.h>
#include< stdlib.h>
#define N 33
void StringToIntArray(const char * s1);
void takeinteger(int i);
int main()
{char / string char [N] ;
printf(输入一串不超过32位的字符串:);
scanf("%s",string);
StringToIntArray(string);

返回0;
}
void StringToIntArray(const char * s1)
{int int> [N];
int b,i ;
for(b = 0; s1 [b]!=''\ 0''; ++ b)
{
i = 0;
i = atoi( S1并[b]);


该函数需要const char *。但是s1 [b]是*元素一个数组,而不是
数组。所以尝试atoi(s1);

注意b没有已知值,所以你可能不喜欢结果。

array [++ b] = i;
}

}
//我想把字符串'的第一个字符放到
数组的第一个元素然后到下一个等等
两个东西阻碍了我的表现,演员表,我的全局变量和访问违规行为,如果有更好的方法或提示ID
欣赏它,替代方法也是如此,提示可能会模糊不清
但任何帮助都会很棒
错误包括无法将参数1从''const char''转换为''const
char *



< blockquote>你试图这样做的任何特殊原因?这个程序

不是非常c ++ - ish。除非你的意图是在这里写C,否则我建议你更喜欢使用std :: cout& std :: cin为你输入/输出。

同样更喜欢使用std :: stringstream进行atoi转换,

等...

也就是说,将字符串转换为int的简单版本可以完成如下(我留给你修改使用

数组):


....

std :: string str(" 2345");

std :: istringstream conv(str);

int x;

conv>> x;

....


文章< 11 ************ *********@o13g2000cwo.googlegroups。 com>,
mr*********@yahoo.com 写道:

#include< stdio.h>
#include< string.h>
#include< stdlib.h>
#define N 33
void StringToIntArray(const char * s1);
void takeinteger(int i);
int main()
{char / string [N];
printf("输入一个不超过32位的字符串:);
scanf("%s",string);
StringToIntArray(string);

返回0;
}
void StringToIntArray(const char * s1)
{array int [N];
int b,i;
for(b = 0; s1 [b]!=''\ 0''; ++ b)
{
i = 0;
i = atoi(s1 [ b]);
数组[++ b] = i;
}

}
//我想把字符串'的第一个字符放到第一个
数组的元素然后到下一个等




模板< typename T,typename U>

T lexical_cast(const U& u){

stringstream ss;

T t;

if(!(ss<< u& ss>> t)))抛出bad_cast();

返回t;


struct TransCharToInt {

int operator()(char c)const {

return lexical_cast< int>(c) ;

}

};


int main()

{

cout<< 输入一些数字:;

string str;

cin>> str;

vector< int> vec;

transform(str.begin(),str.end(),back_inserter(vec),

TransCharToInt());

copy(vec.begin(),vec.end(),ostream_iterator< int>(cout,","));

}


我试着这样做:


transform(str.begin(),str.end(),back_inserter(vec),

& lexical_cast< int> );


但它说''lexical_cast< int>''是一种未知类型。是否有一些

方式我可以强制模板实例化而不首先实际调用

函数?


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 33
void StringToIntArray(const char *s1);
void takeinteger(int i);
int main()
{
char string[N];
printf("enter a string of bits no more than 32 bits long: ");
scanf("%s", string);
StringToIntArray(string);

return 0;
}
void StringToIntArray(const char *s1)
{
int array[N];
int b, i;
for(b = 0; s1[b] != ''\0''; ++b)
{
i = 0;
i = atoi(s1[b]);
array[++b] = i;
}

}
// i am trying to get string''s first character into first element of
array and then to the next etc
two things which have hinder my performance, casting, my global
variable, and access violations, if there is a better way or a hint id
appreciate it, alternative methods as well, hints can be vague sometime
but any help would be great
errors include cannot convert parameter 1 from ''const char'' to ''const
char *

解决方案

<mr*********@yahoo.com> wrote:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 33
void StringToIntArray(const char *s1);
void takeinteger(int i);
int main()
{
char string[N];
printf("enter a string of bits no more than 32 bits long: ");
scanf("%s", string);
StringToIntArray(string);

return 0;
}
void StringToIntArray(const char *s1)
{
int array[N];
int b, i;
for(b = 0; s1[b] != ''\0''; ++b)
{
i = 0;
i = atoi(s1[b]);
The function wants const char*. But s1[b] is an *element" of an array, not
the array. So try atoi(s1);
Note that b has no known value so you may not like the result.
array[++b] = i;
}

}
// i am trying to get string''s first character into first element of
array and then to the next etc
two things which have hinder my performance, casting, my global
variable, and access violations, if there is a better way or a hint id
appreciate it, alternative methods as well, hints can be vague sometime
but any help would be great
errors include cannot convert parameter 1 from ''const char'' to ''const
char *



Any particular reason why your trying to do it like this? This program
isn''t very c++-ish. Unless your intent is to write C here, I recommend
that you prefer using std::cout & std::cin for you input/output.
Likewise prefer using std::stringstream for your conversions over atoi,
etc...
That said, the simple version of converting string to int can be
accomplished as follows (I leave it to you to modify for use with
array):

....
std::string str("2345");
std::istringstream conv(str);
int x;
conv >> x;
....


In article <11*********************@o13g2000cwo.googlegroups. com>,
mr*********@yahoo.com wrote:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 33
void StringToIntArray(const char *s1);
void takeinteger(int i);
int main()
{
char string[N];
printf("enter a string of bits no more than 32 bits long: ");
scanf("%s", string);
StringToIntArray(string);

return 0;
}
void StringToIntArray(const char *s1)
{
int array[N];
int b, i;
for(b = 0; s1[b] != ''\0''; ++b)
{
i = 0;
i = atoi(s1[b]);
array[++b] = i;
}

}
// i am trying to get string''s first character into first element of
array and then to the next etc



template < typename T, typename U >
T lexical_cast( const U& u ) {
stringstream ss;
T t;
if ( !( ss << u && ss >> t ) ) throw bad_cast();
return t;
}

struct TransCharToInt {
int operator()( char c ) const {
return lexical_cast<int>( c );
}
};

int main()
{
cout << "Enter some numbers: ";
string str;
cin >> str;
vector<int> vec;
transform( str.begin(), str.end(), back_inserter(vec),
TransCharToInt() );
copy( vec.begin(), vec.end(), ostream_iterator<int>(cout, ", ") );
}

I tried to do:

transform( str.begin(), str.end(), back_inserter(vec),
&lexical_cast<int> );

but it says that ''lexical_cast<int>'' is an unknown type. Is there some
way I can force template instantiation without actually calling the
function first?


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

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