当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - boost date immutable - so how can use it in a loop?

问题描述:

boost pdate is immutable, so how can I use it in a loop? I need to make comparison with last-dealt-with date:

 date last(neg_infin);

for (int i = 0; i < tasks.size(); i++) {

if (tasks[i]->getTime().date() > last) {

cout << ":::" << tasks[i]->getTime().date() << ":::" << endl;

}

cout << "[" << i << "]" << '\t';

tasks[i]->print();

// i need to change the last date here

cout << endl;

}

ie. if the date part of current time is later than the previous, print the date, otherwise print only the time

网友答案:

Why do you need an explicit last element? Wont this work as well?

if (i == 0 || tasks[i]->getTime().date() > tasks[i-1]->getTime.date()) {
网友答案:

Well, since you're dealing with indexes into tasks already, instead store the "last" index, rather than the date.

int lastIndex = 0;
for (int i = 1; i < tasks.size(); i++) {
        if (tasks[i]->getTime().date() > tasks[lastIndex]->getTime.date()) {
            cout << ":::" << tasks[i]->getTime().date() << ":::" << endl;

        }
        cout << "[" << i << "]" << '\t';
        tasks[i]->print();

        lastIndex = i;

        cout << endl;
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: