字符串:“21_28_36_45_55_55_56_59_63_71_80_79_79_85_91_95_100_93_87_76_64_68_72_60_50_31_13_9_5_3_0_0”如何变为如下
数组:[21,28,36,45,55,55,56,59,63,71,80,79,79,85,91,95,100,93,87,76,64,68,72,60,50,31,13,9,5,3,0,0]
数组:[21,28,36,45,55,55,56,59,63,71,80,79,79,85,91,95,100,93,87,76,64,68,72,60,50,31,13,9,5,3,0,0]
CString resToken;
int curPos = 0;
resToken= pArr.Tokenize(_T("_"),curPos);
vector<int> v;
while (resToken != _T(""))
{
v.push_back( _ttoi( resToken ) );
resToken = pArr.Tokenize(_T("_"), curPos);
};
int _tmain(int argc, _TCHAR* argv[])
{
CString strTrigger="21_28_36_45_55_55_56_59_63_71_80_79_79_85_91_95_100_93_87_76_64_68_72_60_50_31_13_9_5_3_0_0";
int data[32] = {0};
int i = 0;
int curpos = 0;
CString strRes;
while((strRes = strTrigger.Tokenize("_", curpos)) != "")
{
//cout<<strRes.GetBuffer()<<endl;
data[i] = atoi(strRes.GetBuffer());
i++;
} for(int j = 0; j < i; j++)
{
cout<<data[j]<<" ";
}
cout<<endl;
return 0;
}
strSource += L"_";
std::vector<int> vDest;while(!strSource.IsEmpty())
{
int iFind = strSource.Find(L"_");
strTemp = strSource.Left(iFind);
int iTemp = _wtoi(strTemp.GetBuffer());
vDest.push_back(iTemp);
strSource = strSource.Right(strSource.GetLength() - iFind - 1);
}
ON_COMMAND(ID_APP_ABOUT, &CReadFileApp::OnAppAbout)
// 基于文件的标准文档命令
ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
// 标准打印设置命令
ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
试一试
CString strSource = L"21_28_36_45_55_55_56_59_63_71_80_79_79_85_91_95_100_93_87_76_64_68_72_60_50_31_13_9_5_3_0_0";
CString strTemp;
strSource += L"_";
std::vector<int> vDest;
while(!strSource.IsEmpty())
{
int iFind = strSource.Find(L'_');
strTemp = strSource.Left(iFind);
int iTemp = _wtoi((const unsigned short *)strTemp.GetBuffer(strTemp.GetLength()));
vDest.push_back(iTemp);
strSource = strSource.Right(strSource.GetLength() - iFind - 1);
}
CString strSource = L"21_28_36_45_55_55_56_59_63_71_80_79_79_85_91_95_100_93_87_76_64_68_72_60_50_31_13_9_5_3_0_0";
CString strTemp;
strSource += L"_";
std::vector<int> vDest;
while(!strSource.IsEmpty())
{
int iFind = strSource.Find(L'_');
strTemp = strSource.Left(iFind);
int iTemp = atoi(strTemp.GetBuffer(strTemp.GetLength()));
vDest.push_back(iTemp);
strSource = strSource.Right(strSource.GetLength() - iFind - 1);
}
if(pc[i]!='_')
pc[j++]=pc[i];
}
需要包含头文件
#include <afxwin.h>选择静态MFC链接不固定是指从外面输入?那么把输入的字符串赋给strTrigger就可以了。
char *pc=“21_28_36_45_55_55_56_59_63_71_80_79_79_85_91_95_100_93_87_76_64_68_72_60_50_31_13_9_5_3_0_0”;
struct S{
char c[4];
};
int i,j,index,len=strlen(pc);
S*ps=new s[len];
for(index=j=i=0;i<(int)strlen(pc);i++)
{
if(pc[i]!='_')
{
int n=i-index;
memcpy(ps[j].pc,pc+index,n);
ps[j++].c[n]=0;
index=i+1;
}
}
//j是新数组的数量
#include <vector>
using namespace std;TCHAR szText[] = _T("21_28_36_45_55_55_56_59_63_71_80_79_79_85_91_95_100_93_87_76_64_68_72_60_50_31_13_9_5_3_0_0");
int nCount = 0;
int nSize = _tcslen(szText);
vector<int> vec;
TCHAR szTmp[10] = {0}; while(TRUE)
{
sscanf(szText+nCount, _T("%[^_]"), szTmp); vec.push_back(_ttoi(szTmp));
nCount += _tcslen(szTmp) + _tcslen(_T("_"));
if(nCount > nSize)
{
break;
}
} CString strText(_T(""));
CString strTmp(_T(""));
for(vector<int>::const_iterator iter = vec.begin(); iter != vec.end(); iter++)
{
strTmp.Format(_T("%d\r\n"), *iter);
strText += strTmp;
}
AfxMessageBox(strText);