for循环不会从i = 0开始

时间:2017-02-16 11:05:10

标签: c++ for-loop

//write_db_iec(writer_ob1.get_vector_db_timer()[0]);
  for (int i = 0; i < writer_ob1.get_vector_db_timer().size(); i++) {
     write_db_iec(writer_ob1.get_vector_db_timer()[i]);
  }

有没有可能,for循环直接从i = 1开始? 第一个元素(i = 0)是可访问的,包含元素。

[已编辑]:我希望循环从i = 0开始。但它失败了。

下面是函数write_db_iec:

void write_db_iec(db_iec db_timer) {
    writer_file.open(writer_file_location, std::ios::app);
    if (writer_file.is_open()) {
        //NOTICE
        std::cout << "Begin writing db timer-\n";

        //COMMENT
        writer_file << "//DATA BLOCK FOR TIMER -\n";
        writer_file << "//FOR " << db_timer.get_db_iec_timer().get_name() << "\n";

        //HEADER
        writer_file << "DATA_BLOCK " << db_timer.get_db_name() << "\n"
            << "{ S7_Optimized_Access := 'FALSE'}\n"
            << "NAME : " << db_timer.get_db_iec_timer().get_type() << "\n"
            << "VERSION : 0.1\n";

        //RELATED FB
        writer_file << "BEGIN\n"
            << "END_DATA_BLOCK\n\n\n";

        //CLOSE FILE
        writer_file.close();
    }
    else {
        writer_status = false;
        std::cout << "Error on writing timer DB\n";
        std::cout << "Timer DB Name : " << db_timer.get_db_iec_timer().get_name() << "\n";

    }


}

[已添加]: 等级ob1:

class ob1 {
private:
std::vector<std::string> ob1_var_temp;
std::vector<fb> ob1_fb;
std::vector<db> ob1_db;
std::vector<db_iec> ob1_db_timer;

//load var temp for ob1, should be called before writer
void load_var_temp() {
    //not important
}

public:
ob1() { load_var_temp(); }
ob1(fb fb1) {
    ob1_fb.push_back(fb1);
    load_var_temp();

}

std::vector<fb> &get_vector_fb() {
    return ob1_fb;
}

std::vector<db> &get_vector_db() {
    return ob1_db;
}

std::vector<db_iec> &get_vector_db_timer() {
    return ob1_db_timer;
}

std::vector<std::string> &get_vector_var_temp() {
    return ob1_var_temp;
}




//to generate db, should be called before writer
void generate_db(std::vector<fb> functionblock) {
    if (functionblock.size() > 0) {
        for (int i = 0; i < functionblock.size(); i++) {
            ob1_db.push_back(functionblock[i]);

        }
    }
}

//overloaded function from generate db
void generate_db() {
    std::vector<fb> functionblock = ob1_fb;
    if (functionblock.size() > 0) {
        for (int i = 0; i < functionblock.size(); i++) {
            ob1_db.push_back(functionblock[i]);

        }
    }
}

//to generate db timer, should be called before writer
void generate_db_timer(std::vector<fb> functionblock) {
    for (int i = 0; i < functionblock.size(); i++) {
        if (functionblock[i].get_timer().size() > 0) {
            ob1_db_timer.push_back(functionblock[i].get_timer()[i]);

        }
    }
}

//to generate db timer, should be called before writer, overloaded function
void generate_db_timer() {
    std::vector<fb> functionblock = ob1_fb;
    for (int i = 0; i < functionblock.size(); i++) {
        if (functionblock[i].get_timer().size() > 0) {
            for (int j = 0; j < functionblock[i].get_timer().size(); j++) {
                ob1_db_timer.push_back(functionblock[i].get_timer()[j]);
            }
        }
    }
}

//to add fb into ob1
void add_fb(fb functionblock) {
    ob1_fb.push_back(functionblock);
}

};

它有类db_iec的向量。这里是类db_iec:

// data block for iec timer
class db_iec {
private:
timer db_iec_timer;
std::string db_name;

public:
db_iec() {}
db_iec(timer iec_timer) : db_iec_timer{ iec_timer } {}

std::string &get_db_name() {
    return db_name;
}

timer &get_db_iec_timer() {
    return db_iec_timer;
}
};

最后,我在声明我的函数并在for循环中调用它

void analyse_ob1() {

    writer_file.open(writer_file_location);
    if (writer_file.is_open()) {
        writer_file << "///////////////////////////////////////////////////////////////////////////////////////\n";
        writer_file << "//This is an automatically generated code\n";
        time_t theTime = time(NULL);
        struct tm *aTime = localtime(&theTime);

        int day = aTime->tm_mday;
        int month = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
        int year = aTime->tm_year + 1900;
        int second = aTime->tm_sec;
        int minute = aTime->tm_min;
        int hour = aTime->tm_hour;
        writer_file << "//Date : " << day << "." << month << "." << year << "\n";
        writer_file << "//Time : " << hour << ":" << minute << ":" << second << "\n";
        writer_file << "///////////////////////////////////////////////////////////////////////////////////////\n\n\n";



    }


    //write_db_iec(writer_ob1.get_vector_db_timer()[0]);
    for (int i = 0; i < writer_ob1.get_vector_db_timer().size(); i++) {
        //writer_file.open(writer_file_location, std::ios::app);
        //writer_file << "DB_Number : " << i << "\n";

        write_db_iec(writer_ob1.get_vector_db_timer()[i]);
    }


    for (int i = 0; i < writer_ob1.get_vector_fb().size(); i++) {
            write_fb(writer_ob1.get_vector_fb()[i]);
            write_db(writer_ob1.get_vector_db()[i]);
    }



    write_ob1();

}


};

[求助]:在调用函数write_db_iec()之前,我必须先关闭文件(writer_file.close())。谢谢你的所有答案。

1 个答案:

答案 0 :(得分:1)

问题不在于for循环,这是肯定的。除非writer_ob1.get_vector_db_timer().size() == 0,否则循环体将以i = 0执行。否则循环体不会被执行。

确保从writer_ob1.get_vector_db_timer()返回的向量确实包含您期望的元素。