strtok()为长度为0的令牌返回的值? 我有以下C++代码: string dots="..."; char *points=(char *)malloc(sizeof(char)*20); strcpy(points,dots.c_str()); points=strtok(points,"."); while(points!=NULL) { cout<<points<<endl; points=strtok(NULL,"."); } string dots=“…”; 字符*点=(字符*)malloc(字符大小)*20); strcpy(points,dots.c_str()); 点数=strtok(点数“.”); while(点!=NULL) { cout
您的代码有未定义的行为,您没有为strtok()为长度为0的令牌返回的值? 我有以下C++代码: string dots="..."; char *points=(char *)malloc(sizeof(char)*20); strcpy(points,dots.c_str()); points=strtok(points,"."); while(points!=NULL) { cout<<points<<endl; points=strtok(NULL,"."); } string dots=“…”; 字符*点=(字符*)malloc(字符大小)*20); strcpy(points,dots.c_str()); 点数=strtok(点数“.”); while(点!=NULL) { cout,c++,string,strtok,C++,String,Strtok,您的代码有未定义的行为,您没有为点分配内存,访问它会调用UB 更新时,我可能只在可能的情况下使用字符串和STL函数编写validateIP。混合C/C++不利于维护 #include <sstream> int to_int(const std::string& s) { int i(0); std::stringstream ss(s); ss >> i; return i; } bool isValidIp(const st
点
分配内存,访问它会调用UB
更新时,我可能只在可能的情况下使用字符串和STL函数编写validateIP
。混合C/C++不利于维护
#include <sstream>
int to_int(const std::string& s)
{
int i(0);
std::stringstream ss(s);
ss >> i;
return i;
}
bool isValidIp(const std::string& IP)
{
if (std::count(IP.begin(), IP.end(), '.') != 3)
{
return false;
}
std:stringstream ss(IP);
int token;
std::string s;
while(std::getline(ss, s, '.'))
{
int token = to_int(s);
if (token < 0 || token > 255)
{
return false;
}
}
return true;
}
#包括
int到_int(const std::string&s)
{
int i(0);
标准::stringstream ss(s);
ss>>i;
返回i;
}
bool isValidIp(const std::string和IP)
{
if(std::count(IP.begin(),IP.end(),'.')!=3)
{
返回false;
}
std:stringstream ss(IP);
int标记;
std::字符串s;
while(std::getline(ss,s,'.'))
{
int token=to_int(s);
如果(令牌<0 | |令牌>255)
{
返回false;
}
}
返回true;
}
那么你叫它:
if (isValidIp(IP))
{
std::cout << "Valid IP" << std::endl;
}
else
{
std::cout << "Invalid IP" << std::endl;
}
if(isValidIp(IP))
{
std::cout您的代码有未定义的行为,您没有为点分配内存
,访问它会调用UB
更新时,我可能只在可能的情况下使用字符串和STL函数编写validateIP
。混合C/C++不利于维护
#include <sstream>
int to_int(const std::string& s)
{
int i(0);
std::stringstream ss(s);
ss >> i;
return i;
}
bool isValidIp(const std::string& IP)
{
if (std::count(IP.begin(), IP.end(), '.') != 3)
{
return false;
}
std:stringstream ss(IP);
int token;
std::string s;
while(std::getline(ss, s, '.'))
{
int token = to_int(s);
if (token < 0 || token > 255)
{
return false;
}
}
return true;
}
#包括
int到_int(const std::string&s)
{
int i(0);
标准::stringstream ss(s);
ss>>i;
返回i;
}
bool isValidIp(const std::string和IP)
{
if(std::count(IP.begin(),IP.end(),'.')!=3)
{
返回false;
}
std:stringstream ss(IP);
int标记;
std::字符串s;
while(std::getline(ss,s,'.'))
{
int token=to_int(s);
如果(令牌<0 | |令牌>255)
{
返回false;
}
}
返回true;
}
那么你叫它:
if (isValidIp(IP))
{
std::cout << "Valid IP" << std::endl;
}
else
{
std::cout << "Invalid IP" << std::endl;
}
if(isValidIp(IP))
{
std::coutstrtok
函数返回由给定字符分隔的给定字符串的子字符串。IMO(待测试)如果您的字符串仅包含分隔字符,strtok
函数将在第一次调用时返回NULL
(无更多令牌)
此外,在代码段中,您将字符串复制到未初始化的指针。将对strcpy
的调用替换为在复制之前对要分配的底层内存的strdup
的调用。编辑:您在我回答时修改了问题strtok
函数返回给定字符串的子字符串IMO(待测试)如果字符串仅包含分隔字符,strtok
函数将在第一次调用时返回NULL
(无更多标记)
此外,在代码段中,您将字符串复制到未初始化的指针。在复制之前,将对strcpy
的调用替换为对要分配的底层内存的strdup
的调用。编辑:您在我回答时修改了问题,如果字符串仅包含分隔字符,strok
returnNULL
你可能想要这个:
int main()
{
string dots=". . ."; //Notice space
char *points=(char *)malloc(sizeof(char)*20);
char *p; // Use a char pointer
strcpy(points,dots.c_str());
p=strtok(points,".");
while(p!=NULL)
{
cout<<points<<endl;
p=strtok(NULL,".");
}
/* Free Memory */
free(points);
}
intmain()
{
string dots=“…”;//注意空格
字符*点=(字符*)malloc(字符大小)*20);
char*p;//使用char指针
strcpy(points,dots.c_str());
p=strtok(点“.”);
while(p!=NULL)
{
cout如果字符串仅包含分隔字符,strok
返回NULL
你可能想要这个:
int main()
{
string dots=". . ."; //Notice space
char *points=(char *)malloc(sizeof(char)*20);
char *p; // Use a char pointer
strcpy(points,dots.c_str());
p=strtok(points,".");
while(p!=NULL)
{
cout<<points<<endl;
p=strtok(NULL,".");
}
/* Free Memory */
free(points);
}
intmain()
{
string dots=“…”;//注意空格
字符*点=(字符*)malloc(字符大小)*20);
char*p;//使用char指针
strcpy(points,dots.c_str());
p=strtok(点“.”);
while(p!=NULL)
{
coutstrtok用于标记字符串。例如,我有一个字符串“abc.def.ghi.jkl”,然后我们可以使用strtok来获得位于分隔符上的标记
char a[]="abc.def.ghi.jkl";
char tmp=strtok(a, ".");
if (tmp != NULL) //Required because strtok will return null if it failes find the delimiter
printf("\n value is [%s]", tmp); //out put is abc
因此,在您的例子中,“…”是字符串,“.”是分隔符,它导致空字符串,因为第一个字符和分隔符之间没有字符
对于所有sttok函数调用,您的代码将返回空字符串,比如“”作为输出
其次,您必须为points变量分配内存,如
char points[dots.length()+1];
strtok用于标记字符串。比如说,我有一个字符串“abc.def.ghi.jkl”,然后我们可以使用strtok来获得位于分隔符上的标记
char a[]="abc.def.ghi.jkl";
char tmp=strtok(a, ".");
if (tmp != NULL) //Required because strtok will return null if it failes find the delimiter
printf("\n value is [%s]", tmp); //out put is abc
因此,在您的例子中,“…”是字符串,“.”是分隔符,它导致空字符串,因为第一个字符和分隔符之间没有字符
对于所有sttok函数调用,您的代码将返回空字符串,比如“”作为输出
其次,您必须为points变量分配内存,如
char points[dots.length()+1];
谷歌为“Maloc”,你需要为指针分配内存。(它是写代码的一部分,不是和平的)。我胡忙了。现在它已经分配了。MEM。但是还是这样?我认为你的代码是C,如果是C++,你应该使用NeX.@ E.TiNeN:这里是完整的代码。谷歌对于“Maloc”,你需要为指针分配内存。胡:我写的是“代码”而不是“和平”。我现在很忙。分配了MME。但是仍然?我认为你的代码是C,如果是C++,你应该使用NeX.@ E.Tyne:这里是完整的代码。好了,现在我已经完成了,所以Stotok()返回的长度为0的令牌,现在我已经完成了,所以Strutok()返回长度为0的令牌不要忘记免费;-)不要忘记免费;-)