문제

I want to iterate through the list1 after the list2 inside that block. please tell how to make that happen. i have a guess why thats not working but have no idea to make it happen

enum type {VALID,INVALID};
#define ADD_A(x,y) addtolist x(#x,VALID,y);
#define ADD_B(x,y) addtolist x(#x,INVALID,y);
class addtolist {
  public:
    std::string name;
    int val;
    std::list<int> list1;
    std::list<int> list2;
    std::list<int>::iterator v;
    std::list<int>::iterator w;
    addtolist(std::string name, type _val, int range);
};

class newlist {
  public:
    newlist(){
    ADD_A(ob1,5);
    ADD_B(ob2,5);
    }
};

addtolist::addtolist( std::string name, type _val,  int range ) {
    name = name;
    val = _val;
    int i;
    if (val==0){
        for(i=0;i<=range;i++){
            list1.push_back(i);
        }
        for (v = list1.begin(); v != list1.end(); ++v){
            std::cout <<"\nvalid list == "<<*v << std::endl;
        }
        std::cout<<"\n size of list1 == "<< list1.size()<<std::endl;
    }else if (val==1){
       for(i=0;i<=range;i++){
           list2.push_back(i);
       }
   for ( w = list2.begin(); w != list2.end(); ++w){
           std::cout <<"\nINVALID LIST == "<<*w<< std::endl;
       }
// i dont know why this gets zero and also the loop does not work and how to make this work
       std::cout<<"\n THIS BECOMES ZERO == "<< list1.size()<<std::endl;
       for (v = list1.begin(); v != list1.end(); ++v){
           std::cout <<"\nVALID LIST == "<<*v << std::endl;
       }
    }
}

int main()
{
    newlist a;
}

problem is in the part else if block ie. iterating the list1 and the list1.size(); ..

도움이 되었습니까?

해결책

The design reason for having two internal std::list containers is not clear. Only one of the list containers is filled, dependent on the enum type _val value passed to the

addtolist(std::string name, type _val, int b);

constructor. In order to have a non-empty list1 container for an INVALID type, the body of the constructor will have to change accordingly. In other words, the second object ob2 has elements for list2, because container number two is the only list that has been populated.

If in doubt, check the list1.empty() condition for both enum type values, which is not really necessary since the list1.size() value is already shown as zero for the INVALID type. Ultimately the for loop which traverses list1 for the INVALID type is behaving exactly as it supposed to for an empty container.

One way to always have list1 filled, is to move the list1 for loop above the if statement logic that checks for the enum type. This is only an example. The actual design modification is up to the OP.

addtolist::addtolist( std::string name, type _val,  int range ) 
{
   name = name;
   val = _val;
   int i;
   for(i=0;i<=range;i++)
   {
      list1.push_back(i);
   }

   if (val==VALID)
   {
      // 
      // for loop that prints list1 content and other code follows as before
      //
   .
   .
   .
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top