Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在while条件下执行for循环?_C++_Vector - Fatal编程技术网

C++ 如何在while条件下执行for循环?

C++ 如何在while条件下执行for循环?,c++,vector,C++,Vector,我必须从循环中通过向量得到一个结果作为条件,然后对该结果执行一项任务 do{result} 我的数据是连续的入站json数据,以7个一组的形式除去json属性。 我需要验证第[2]项,然后将第[3]项发送到另一个向量 我的情况是 (for(int b = 2; b < buydat.size(); b+=7); buydat[b] == "Buy") 我的结果可以显示为 for(int d = 0; d < pricedat.size(); ++d){ cout <

我必须从循环中通过向量得到一个结果作为条件,然后对该结果执行一项任务

do{result}
我的数据是连续的入站json数据,以7个一组的形式除去json属性。 我需要验证第[2]项,然后将第[3]项发送到另一个向量

我的情况是

(for(int b = 2; b < buydat.size(); b+=7);
buydat[b] == "Buy")
我的结果可以显示为

for(int d = 0; d < pricedat.size(); ++d){
    cout << pricedat[d] << " " << endl;
}
如何将for循环结果作为一个条件?
通过data->validate to item[2]->将该系列中的item[3]发送到向量,循环7次。

从注释中,代码应该是:

vector<std::string> buydat;
vector<std::string> pricedat;
for(int b = 2; b < buydat.size(); b+=7){
    if ( buydat[b] == "Buy" ) { 
      pricedat.push_back(buydat[b+1]); 
    }
}
vector-buydat;
向量价格;
对于(int b=2;b

您的原始代码有一个无限循环,并且对buydat数组使用了错误的索引

你的情况不会终止
buydat[b]
do
-
while
循环的主体中未被修改。当前有两个嵌套循环;只要
buydat[b]==“Buy”
为真,内部循环就永远不存在,因此向量的内存就用完了。我很难理解你想做什么,也许你的意思是
if(buydat[b]==“Buy”){pricedat.push_back(buydat[3])}
?在任何情况下,更新你的帖子解释你的代码试图实现什么都是有用的。它应该每7个就终止一次,然后在下一个集合中再次执行。每7个就执行一次。捕获该集合结果中的一个项目pricedat。push_back(buydat[3]);
vector<std::string> buydat;
vector<std::string> pricedat;
for(int b = 2; b < buydat.size(); b+=7){
    do{
        pricedat.push_back(buydat[3]);
    }
    while(
        //(int b = 2; b < buydat.size(); b+=7;)
        buydat[b] == "Buy"
    );
}
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted
vector<std::string> buydat;
vector<std::string> pricedat;
for(int b = 2; b < buydat.size(); b+=7){
    if ( buydat[b] == "Buy" ) { 
      pricedat.push_back(buydat[b+1]); 
    }
}