C++ 如果(!buydat.empty())验证失败(始终为true)

C++ 如果(!buydat.empty())验证失败(始终为true),c++,validation,vector,C++,Validation,Vector,我有一个始终为真的“if validation”,即使我删除它正在接收的入站数据以确保它为空,我仍然在不应该的时候到达我的cout语句 代码: void buymngr(){ 载体buydat; 向量标记; 向量价格; buydat=getmyData(); 如果(!buydat.empty()){ cout您的if语句检查bIf(!buydat[0].empty()){,这总是正确的(在buydat.size之前)。你用什么来擦除向量?你试过了吗?我没有试图擦除向量。你说的是!buydat.e

我有一个始终为真的“if validation”,即使我删除它正在接收的入站数据以确保它为空,我仍然在不应该的时候到达我的cout语句

代码:

void buymngr(){
载体buydat;
向量标记;
向量价格;
buydat=getmyData();
如果(!buydat.empty()){

cout您的
if
语句检查
b
。您可以使用此语句在向量末尾之外建立索引:

if ( buydat[b] == "Buy" ){
    pricedat.push_back(buydat[b+1]);
}
编辑:我所说的“您可以索引…”是指“您可以索引…”。

这是一个观察,而不是一个建议:您正在将
buydat[b+1]
添加到
pricedat
向量中。如果在测试中
(b==(buydat.size()-1))
,您将
推回(buydat[buydat.size()]
-无效。您发布的代码没有任何测试来确保
buydat.size()
小于
b+N
(无论
N
是什么-我根据您的
循环猜测7)

太长,无法发表评论,因此请继续回答: 然后您的问题出现在
getMyData()
中。请考虑:

#include <vector>
#include <string>
std::vector<std::string> getmyData()
{
    std::vector<std::string> vrecords;
    vrecords.push_back("anything.");
    vrecords.push_back("anything 2.");
    // push some more data here so the result -looks- like
    // whatever you expect the output of your PHP to be.

    // also, your original version of this function never closes mydfp

    return vrecords;
}

void buymngr(){
    std::vector<std::string> buydat;
    std::vector<std::string> markdat;
    std::vector<std::string> pricedat;
    buydat = getmyData();
    if(!buydat.empty()){
        std::cout << "You 'do' have buy string match data!" << std::endl;
    }
}

int main()
{
    buymngr();
    return 0;
}
#包括
#包括
std::vector getmyData()
{
std::向量向量向量记录;
vrecords.向后推(“任何东西”);
vrecords.push_back(“任何东西2”);
//将更多数据推送到此处,使结果看起来像
//无论您希望PHP的输出是什么。
//此外,此函数的原始版本永远不会关闭mydfp
返回vrecords;
}
void buymngr(){
std::向量buydat;
std::向量markdat;
std::向量价格DAT;
buydat=getmyData();
如果(!buydat.empty()){

如果我这样做,那么我最终会在for()中包含几个函数循环//因为代码的其余部分。实时代码还有几百行。如果可能的话,我希望事先进行验证。但非常感谢你的想法。我不关心buda.size。该函数执行它应该执行的操作,但它只应该使用有效数据执行。我的失败是->If(!buydat[0].empty()){,这总是正确的(在buydat.size之前)。你用什么来擦除
向量
?你试过了吗?我没有试图擦除向量。你说的是
!buydat.empty()的测试用例
在清除向量进行测试后,空向量上的返回值仍然为真,除非我有误解?删除所有向量数据的方法可能有问题,因为我相对确定
empty()
没有问题。此代码工作正常。这是原始数据
if ( buydat[b] == "Buy" ){
    pricedat.push_back(buydat[b+1]);
}
#include <vector>
#include <string>
std::vector<std::string> getmyData()
{
    std::vector<std::string> vrecords;
    vrecords.push_back("anything.");
    vrecords.push_back("anything 2.");
    // push some more data here so the result -looks- like
    // whatever you expect the output of your PHP to be.

    // also, your original version of this function never closes mydfp

    return vrecords;
}

void buymngr(){
    std::vector<std::string> buydat;
    std::vector<std::string> markdat;
    std::vector<std::string> pricedat;
    buydat = getmyData();
    if(!buydat.empty()){
        std::cout << "You 'do' have buy string match data!" << std::endl;
    }
}

int main()
{
    buymngr();
    return 0;
}