C++截取字符串中的数据

来源:百度知道 编辑:UC知道 时间:2024/06/06 02:41:46
例如字符串“444-43-2343-23432-33”
在C++中将此字符串,按照int* aa进行存储。形式为int类型的444 43 2343 23432 33
我知道思路不过程序不会写请求达人:
用for循环 以\n为结束符
用for循环可以解决
取够位数后,将它变成int类型,原来是string类型或者 char 字符数组类型。
还要记录有效位数。
也就是利用循环,截取了多少次,cout记录下。

#include <iostream>
#include <string>
using namespace std;

void display(const int *a)
{
for(int i=0;a[i];i++)
cout<<a[i]<<" ";
cout<<endl;
}

void cat(const string &str,int *temp,int &a)
{
int count=0,k=0;
unsigned int i=0;
char strtemp[255]={0};
while(i<str.size())
{
while(isdigit(str[i])&&i<str.size())
strtemp[k++]=str[i++];
strtemp[k]='\0';
temp[count++]=atoi(strtemp);
strtemp[0]='\0';
k=0;
i++;
}
temp[count]='\0';
a=count;
}

int main()
{
string temp="444-43-2343-23432-33";
int aa[255];
int a=0;
cout<<temp<<endl;
cat(temp,aa,a);
display(aa);
cout<<"共"<<a<<"次!"<<endl;
return 0;
}