我必须从通过 vector 的循环中获得结果作为条件,然后对该结果执行任务。
do{result}
我的数据是连续的入站 json 数据,以 7 组的形式剥离了它的 json 属性。 我需要验证 item[2],然后将 item[3] 发送到不同的 vector 。
我的条件是
(for(int b = 2; b < buydat.size(); b+=7);
buydat[b] == "Buy")
我的行动是
do{
pricedat.push_back(buydat[3]);
}
我的结果可以显示为
for(int d = 0; d < pricedat.size(); ++d){
cout << pricedat[d] << " " << endl;
}
我的代码:
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
如何将 for 循环结果作为条件? 通过数据循环 7 次 -> 验证到 item[2] -> 将该系列中的 item[3] 发送到 vector 。
最佳答案
根据评论,代码应该是:
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]);
}
}
您的原始代码有一个无限循环,并且还使用了错误的 buydat 数组索引。
关于c++ - 你如何做一个 for 循环作为 while 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27138903/