std::string str="0,0,mirro;0,1,david";如何遍历string解析出来赋值给Gird的结构体?

mirroatl187 2014-11-27 02:26:27

typedef struct GridStu{
int row;
int col;
string Context;
}Grid;

string str="0,0,mirro;0,1,david";俩段内容

...全文
138 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2014-11-27
  • 打赏
  • 举报
回复
string 没有方便的函数,和 CSrtring 一样.
mirroatl187 2014-11-27
  • 打赏
  • 举报
回复
用这个stirng类函数进行读取啊。。。
引用 1 楼 schlafenhamster 的回复:
sscanf() - 从一个字符串中读进与指定格式相符的数据.
赵4老师 2014-11-27
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
char s[]="123 ab 4";
char *p;
int v,n,k;
void main() {
    p=s;
    while (1) {
        k=sscanf(p,"%d%n",&v,&n);
        printf("k,v,n=%d,%d,%d\n",k,v,n);
        if (1==k) {
            p+=n;
        } else if (0==k) {
            printf("skip char[%c]\n",p[0]);
            p++;
        } else {//EOF==k
            break;
        }
    }
    printf("End.\n");
}
//k,v,n=1,123,3
//k,v,n=0,123,3
//skip char[ ]
//k,v,n=0,123,3
//skip char[a]
//k,v,n=0,123,3
//skip char[b]
//k,v,n=1,4,2
//k,v,n=-1,4,2
//End.
或者
strtok, wcstok, _mbstok
Find the next token in a string.

char *strtok( char *strToken, const char *strDelimit );

wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );

unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );

Routine Required Header Compatibility 
strtok <string.h> ANSI, Win 95, Win NT 
wcstok <string.h> or <wchar.h> ANSI, Win 95, Win NT 
_mbstok <mbstring.h> Win 95, Win NT 


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value

All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.

Parameters

strToken

String containing token(s)

strDelimit

Set of delimiter characters

Remarks

The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
_tcstok strtok  _mbstok  wcstok  


On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.

Warning   Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function.  However, calling this function simultaneously from multiple threads does not have undesirable effects.

Example

/* STRTOK.C: In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

void main( void )
{
   printf( "%s\n\nTokens:\n", string );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}


Output

A string   of ,,tokens
and some  more tokens

Tokens:
 A
 string
 of
 tokens
 and
 some
 more
 tokens


String Manipulation Routines

See Also   strcspn, strspn, setlocale
schlafenhamster 2014-11-27
  • 打赏
  • 举报
回复
sscanf() - 从一个字符串中读进与指定格式相符的数据.
描述: "Mirro For Delphi7" 是一个速度极快的抓取屏幕数据的单元.它的速度与 "Remote Administrator"(www.radmin.com)不相上下.可以运行于所有基于Windows NT核心的操作系统上(Windows NT/Windows 2000/Windows XP/Windows 2003).============================================================================函数:1:function InstallMirroDriver: Boolean;功能:安装Mirro驱动到你的电脑.注意:每次使用前应先调用该函数.参数:无返回:成功返回True,失败返回False.2:function UninstallMirroDriver: Boolean;功能:卸载Mirro驱动.参数:无返回:成功返回True,失败返回False.3:function MirrorStart(dwSetColor: DWORD; pnWidth, pnHeight: Pinteger;bCapMouse:Bool): DWORD;功能:开始Mirro参数:dwSetColor---设置返回位图的颜色值 pnWidth,pnHeight---返回分辨率 bCapMouse---是否捕获鼠标返回:成功返回0.否则返回错误代码4:function MirrorGetChangeInfo(ThePRect: PRect; pBuf: PByte): DWORD;功能:获得变化信息参数:ThePRect--变化矩形,已经是32位对齐,并且是可靠的。 pBuf---位图缓冲,调用者分配,需保证缓冲长度>=屏幕宽度*屏幕高度*颜色位数/8返回:变化类型5:function MirrorGetFullScreen(ThePRect: PRect; pBuf: PByte): DWORD;功能:得到全屏位图,参数同MirrorGetChangeInfo6:function MirrorStop: DWORD;功能:停止Mirro参数:无返回:成功返回0.否则返回错误代码7:function MirrorVersion:string;功能:Mirro版本信息参数:无返回:返回信息============================================================================例子 代码附带了一个本地回显的例子和一个互联网传输的简陋例子.注意:两个例子的图像都只是简单的Draw出来,并没有处理重画消息,所以当有其它窗口遮挡显示窗口时,需要重绘图像.其中互联网传输的例子为了方便只是简单的使用了Delphi本身的Jpeg单元进行压缩,请使用者自行更换压缩算法.该例子可以在互联网上测试(如果不知道自己的IP,可以打开http://www.138soft.com/getip.asp进行查询).============================================================================版权所有 该单元的版权归作者(陈经韬)所有.你可以无偿无限制的使用它,但是在你的软件帮助里面最好声明使用了该单元.============================================================================联系方式 Web:http:www.138soft.com EMail&Msn:lovejingtao@21cn.com 2005-11-12

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧