免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1795 | 回复: 5
打印 上一主题 下一主题

vector操作的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-20 15:48 |只看该作者 |倒序浏览
for(int i = 0; i < ModulerIP.size(); i++)
{
   
  
  sprintf(temp, "(%s:%s)", ModulerIP.NodeId, ModulerIP.modulerId);
  strcat(query, temp);
  
  
}

strcat(query, "(");
for(int i = 0; i < nodestate.size(); i++)
{
  
  sprintf(temp, "%d:", nodestate.state);
  strcat(query, temp);
  
  
}
query[strlen(query)-1] = '\0';
strcat(query, ")");

ModulerIP是个vector,结构是这样的vector <Modu>
Modu是个结构体,struct Modu{char NodeId[4];char modulerId[5]};

这段代码执行完后会输出:(N1:DSS)(N1:SCD)(N3:DSS)(1:0:0:0)
现在想把输出的格式换成:(N1:DSS,SCD)(N3:DSS)(1:0:0:0)
该怎么改这段代码啊?

[ 本帖最后由 cuijxin 于 2008-8-20 16:19 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-08-20 15:51 |只看该作者
发代码请用“插入程序代码”的标签

论坛徽章:
0
3 [报告]
发表于 2008-08-20 16:16 |只看该作者
for(int i = 0; i < ModulerIP.size(); i++)
        {
                char *p = ModulerIP[i].NodeId;
               
               
                sprintf(temp, "(%s:%s)", ModulerIP[i].NodeId, ModulerIP[i].modulerId);
                strcat(query, temp);

               
               
        }
       
        strcat(query, "(");
        for(int i = 0; i < nodestate.size(); i++)
        {
               
                sprintf(temp, "%d:", nodestate[i].state);
                strcat(query, temp);
               
               
        }
        query[strlen(query)-1] = '\0';
        strcat(query, ")");

[[i] 本帖最后由 cuijxin 于 2008-8-20 16:18 编辑 [/i]]

论坛徽章:
0
4 [报告]
发表于 2008-08-20 16:33 |只看该作者
sprintf(temp, "%d:", nodestate.state);
改成
sprintf(temp, "%d:", nodestate.state);

论坛徽章:
0
5 [报告]
发表于 2008-08-20 19:33 |只看该作者

回复 #1 cuijxin 的帖子

你就是想合并NodeId相同得?
可以往前多看一个,相同的话就一起输出

论坛徽章:
0
6 [报告]
发表于 2008-08-20 20:09 |只看该作者
原帖由 cuijxin 于 2008-8-20 15:48 发表
for(int i = 0; i < ModulerIP.size(); i++)
{
   
  
  sprintf(temp, "(%s:%s)", ModulerIP.NodeId, ModulerIP.modulerId);
  strcat(query, temp);
  
  
}

strcat(query, "(");
for(int  ...

for(int i = 0; i < ModulerIP.size(); i++)
{
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;sprintf(temp, "(%s:", ModulerIP[i].NodeId);
&nbsp;&nbsp;&nbsp;&nbsp;strcat(query, temp);
&nbsp;&nbsp;&nbsp;&nbsp;while (i < ModulerIP.size()-1 && ModulerIP[i].modulerId == ModulerIP[i+1].modulerId)
&nbsp;&nbsp;{//相同的NodeId

&nbsp;&nbsp;&nbsp;&nbsp;  sprintf(temp, ",%s", ModulerIP[i].NodeId);
&nbsp;&nbsp;&nbsp;&nbsp;  strcat(query, temp);
&nbsp;&nbsp;&nbsp;&nbsp;  i++;
&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;sprintf(temp, ",%s)", ModulerIP[i].NodeId);
&nbsp;&nbsp;&nbsp;&nbsp;strcat(query, temp);
&nbsp;&nbsp;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP