【发布时间】:2016-03-07 14:20:55
【问题描述】:
我需要将一个字符串搜索到一个字符串数组中。特别是,我有一个带有名称的字符串变量,我需要将此名称搜索到一个字符串数组中,但我无法解决我的问题。
这是我的代码:
connessionesocket(sock,server);
int result=0,f=0;
bool trovato=false;
do{
if(result=(recv(sock, estratto, sizeof(estratto),0))>0)
{
string appo(estratto);
cout << "Appo: "<< appo<< endl;
if ( std::find( std::begin( numeri ), std::end( numeri ), appo ) != std::end( numeri ) )
{
cout << "Correct" << endl;
}
f++;
}
if(result==0)
{
cout<< "Fine";
}
}
while(result>0);
close(sock);
有什么问题?为什么我没有看到cout << "Correct" <<endl;
这是我的结果:
I need, so the result is the same of the matrix to see the "Correct" message
【问题讨论】:
-
首先如果
names[x]和appo是std::strings 摆脱strcmp并使用names[x] == appo -
在给出当前上下文的情况下很容易回答 -
names中的字符串都不匹配appo。 -
@NathanOliver 我试着照你说的做,但是,我的程序是一个客户端/服务器,当我做
names[x]==appo我的服务器停止工作.. -
@SilviaB 那么您的代码中还有一些其他问题,例如您的 15 的任意限制不正确
-
那我觉得你有更大的问题。将您的代码更改为
names[x]==appo应该不会对代码的工作方式产生明显的影响,希望它可以清楚地说明您在源代码中所做的事情。
标签: c++ arrays string algorithm