C++语言字符串转化成整型数组,急!!!

cailaoyang 2006-12-02 11:40:27
怎样将一段字符串转化为整型数组呢?例如
string str="78 89 a bc d 98 87 54 ";转化后的结果是
array[i]={78,89,98,87,54};
...全文
941 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
stone_lan 2006-12-06
  • 打赏
  • 举报
回复
不错,受益.
boland_zh 2006-12-05
  • 打赏
  • 举报
回复
请问个问题 怎样在发帖子啊
owlling 2006-12-05
  • 打赏
  • 举报
回复
呵呵,你的意思是你不给分了,我们被耍了?哈哈哈

==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
cailaoyang 2006-12-05
  • 打赏
  • 举报
回复
谢谢大家了,由于分数有限,请大家多多原谅了!
owlling 2006-12-02
  • 打赏
  • 举报
回复
a7这种页可以在if部分设定下。思路都一样,我就不罗嗦了

==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
owlling 2006-12-02
  • 打赏
  • 举报
回复
哦,如果bc代表两个数字的话(他们没有空白字符分割),else部分可以改为
else
{
for(int i=0;i < tmp.length();++i)
coll.push_back(tmp[i]);
}

==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
lei001 2006-12-02
  • 打赏
  • 举报
回复
请问lz:
如果遇到string str="78 89 a7 bc d 98 87 54 "
结果是
string str="78 89 7 98 87 54 "
还是
string str="78 89 98 87 54 ?"
owlling 2006-12-02
  • 打赏
  • 举报
回复
如果要考虑上面的bc是连在一起的,没有空白字符分割,可以修改如下
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
int main(int argc, char* argv[])
{
std::string str="78 89 a bc d 98 87 54",tmp;
std::istringstream in(str);
std::vector<int> coll;
while(in >> tmp)
{
if(isdigit(tmp[0]))
coll.push_back(atoi(tmp.c_str()));
else
{
int tsum=0;
for(int i=0;i < tmp.length();++i)
tsum += tmp[i];

coll.push_back(tsum);
}
}
std::vector<int>::iterator it = coll.begin();
for(;it != coll.end(); ++it)
std::cout << *it << std::endl;
}



==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
owlling 2006-12-02
  • 打赏
  • 举报
回复
stream是忽视空白字符的

==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
owlling 2006-12-02
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
int main(int argc, char* argv[])
{
std::string str="78 89 a b c d 98 87 54",tmp;
std::istringstream in(str);
std::vector<int> coll;
while(in >> tmp)
{
if(isdigit(tmp[0]))
coll.push_back(atoi(tmp.c_str()));
else
coll.push_back(tmp[0]);
}
std::vector<int>::iterator it = coll.begin();
for(;it != coll.end(); ++it)
std::cout << *it << std::endl;
}



==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
jixingzhong 2006-12-02
  • 打赏
  • 举报
回复
string str="78 89 a bc d 98 87 54 ";
可以是其他的字符串,
不过,
如果里面的 数值 超过了 5 个,
那么 array[5]={0}; ==》 array[N]={0};
N 是数值的个数 ...
jixingzhong 2006-12-02
  • 打赏
  • 举报
回复
#include <sstream>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
string str="78 89 a bc d 98 87 54 ";
int i, j=0, array[5]={0};

for(i=0; i<str.length(); i++)
{
if(isdigit(str[i]))
{
int k=i+1;
if(isdigit(str[k]))k++;

istringstream tmp(str.substr(i, k-i));
tmp >> array[j++];
i = k;
}
}
for(i=0; i<5; i++)
cout<<array[i]<<"\t";
system("pause");
return 0;
}
owlling 2006-12-02
  • 打赏
  • 举报
回复
先把非数字和数字分开,然后数字用atoi,非数字cast成int
比如
int a = atoi("57")
int b = (int)'a' // 其实不cast也行的

==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
jixingzhong 2006-12-02
  • 打赏
  • 举报
回复
由于里面有 非数字字符,
处理起来比较麻烦,
需要先对 string 预处理 ...
jixingzhong 2006-12-02
  • 打赏
  • 举报
回复
用 C++ String Streams
todototry 2006-12-02
  • 打赏
  • 举报
回复
是地
jacknes009 2006-12-02
  • 打赏
  • 举报
回复
按照你的意思,,首先分离字符串,然后用atoi()函数转换就可以了,,
todototry 2006-12-02
  • 打赏
  • 举报
回复
atoi()函数
longjing_g 2006-12-02
  • 打赏
  • 举报
回复
一个字符一个字符的读,遇到' '存入array中,是0-9字符就相应转换成数字,然后得到需要的数字 x= x*10 + y ; 否则,无操作
abblly 2006-12-02
  • 打赏
  • 举报
回复
不能直接转吧,你把你的字符串里面的数字找出来,根据数字的个数new一个数据,然后用找出来的的数字初始化这个数组,自己看看string的find相关方法吧。

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧