| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1572 人关注过本帖
标题:对字符串分割后比较
只看楼主 加入收藏
w906414
Rank: 2
等 级:论坛游民
帖 子:75
专家分:76
注 册:2015-5-29
结帖率:81.82%
收藏
已结贴  问题点数:20 回复次数:3 
对字符串分割后比较
得到“1,2354,7985,9895;2,4674,4874,49455;3,17434,4687,46875”这样的一个字符串
格式是“编号1,数据a,数据b,数据c;编号2,数据a,数据b,数据c;编号3,数据a,数据b,数据c”
分别比较编号123的a数据,b数据,c数据。
如何实现。。。
我的想法声明一个char* array[3][4],arrary[0][0]指向编号1,array[0][1]指向编号1的数据a,array[0][2]指向数据b;依次类推。。。。
但是被自己绕晕了。。
程序代码:
#include<iostream>
#include<stdlib.h>
#include<string>
#include<string.h>
using namespace std;
int main()
{
    std::string const str00="1,1234,9678,2;2,3584,6842,1;3,8527,5746,3";
    std::string const &cellsInfo=str00;//以上是模拟的接口
    char *point=(char*)malloc(cellsInfo.length()); 
    //point=strcpy(point,cellsInfo); //strcpy为什么不能用 
    int length=0;
    for(;length<cellsInfo.length();length++) 
    {
        point[length]=cellsInfo[length];
    }
    char* array[3][4];
    int m,n=0;
    int index=1;
    for(length=0;length<cellsInfo.length();length++,index++)
    {
        cout<<"length="<<length<<"  index="<<index<<"  m="<<m<<"  n="<<n;
        cout<<"  cellsInfo["<<length<<"]="<<cellsInfo[length]<<"  point["<<index<<"]="<<point[index]<<endl;
        if(cellsInfo[length]==',')
        {        
            point[index-1]='\0';//试图用移动指针来分割字符串。 
            array[m][n]=point;
            n++;index=0;
            for(int i=1;i<index;i++)
            {
                point++;
            }//在移动指针的时候出现了问题 
        }
        if(cellsInfo[length]==';')//下面肯定是不对啊 
        {
            point[index-1]='\0';
            array[m][n]=point;
            m++;n=0;point++;index=0;
        }
    }
    while(m--)
    {
        while(n--)
        {
            free(array[m][n]);
        }
    }
    free(point);
    return 0;

 } 
搜索更多相关主题的帖子: 字符串 如何 
2016-03-22 22:58
w906414
Rank: 2
等 级:论坛游民
帖 子:75
专家分:76
注 册:2015-5-29
收藏
得分:0 
各位大神热情点
2016-03-23 00:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
你得告诉别人你想达成什么目的
程序代码:
#include <iostream>
#include <sstream>
#include <vector>

typedef std::vector< std::vector<int> >  TMatrix;

TMatrix split( const char* s )
{
    TMatrix ret;

    std::istringstream is(s);
    size_t r=0, c=0;
    for( unsigned n; is>>n; )
    {
        if( r >= ret.size() )
            ret.resize( r+1 );
        if( c >= ret.back().size() )
            ret.back().resize( c+1 );
        ret[r][c] = n;

        char ch;
        is >> std::ws >> ch;
        if( is && ch==',' )
            ++c;
        else if( is && ch==';' )
            ++r, c=0;
        else
            break;
    }

    return ret;
}

void printmatrix( const TMatrix& m )
{
    for( TMatrix::const_iterator r=m.begin(); r!=m.end(); ++r )
    {
        for( std::vector<int>::const_iterator c=r->begin(); c!=r->end(); ++c )
            std::cout << '\t' << *c;
        std::cout << '\n';
    }
}

int main( void )
{
    TMatrix a = split( "1,1234,9678,2;2,3584,6842,1;3,8527,5746,3" );
    printmatrix( a );

    std::cout << "-------------------------\n";

    TMatrix b = split( " 0 , 1 ; 2 , 3 , 4; 5,6,7,8" );
    printmatrix( b );

    return 0;

 }
输出
        1       1234    9678    2
        2       3584    6842    1
        3       8527    5746    3
-------------------------
        0       1
        2       3       4
        5       6       7       8

2016-03-23 09:11
w906414
Rank: 2
等 级:论坛游民
帖 子:75
专家分:76
注 册:2015-5-29
收藏
得分:0 
回复 3楼 rjsp
就是这个目的。。。今天太晚了 明天再看
2016-03-24 00:32
快速回复:对字符串分割后比较
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019029 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved