字符串:“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]

解决方案 »

  1.   

    如果格式是固定的可以考虑用sscanf
      

  2.   

    用_分割字符串,然后用atoi转化
      

  3.   

    CString pArr = _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");
    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);
    }; 
      

  4.   

    主要就是分割_, 用sscanf可以分割出来,然后一个个循环放入数组
      

  5.   

    学习一下 strchr strtok isdigit
      

  6.   

    以下代码经过测试,直接用就可以了。
    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;
    }
      

  7.   

    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”, strTemp;
    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);
    }
      

  8.   

    BEGIN_MESSAGE_MAP(CReadFileApp, CWinApp)
    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()
    试一试
      

  9.   

    已通过vc6。0编译,另外需要添加#include <vector> 
    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);
    }
      

  10.   

    上面贴错了,不好意思,是这个
    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);
    }
      

  11.   

    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”;for(int j=0,i=0;i<(int)strlen(pc);i++){
      if(pc[i]!='_')
       pc[j++]=pc[i];
    }
      

  12.   


    需要包含头文件
    #include <afxwin.h>选择静态MFC链接不固定是指从外面输入?那么把输入的字符串赋给strTrigger就可以了。
      

  13.   

    看错了,更正
    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是新数组的数量
      

  14.   


    #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);